commit - fdee30e5088a4e0de1b1fbaef1ad69756788e770
commit + 2fa019fcdb07a810253f02630b7d43244b8e6ae3
blob - ff0c655b56381b26f0a591de623b3f6b7b324439
blob + 1bf6fe2619da6fc3a1ee01b3f73411f81e6701c3
--- apple/Shared/Client.swift
+++ apple/Shared/Client.swift
-//
-// Client.swift
-// mailmux
-//
-// Created by Oliver Lowe on 4/7/2022.
-//
-
import Foundation
+import SwiftUI
+
+enum MailmuxError: Error {
+ case badURL
+}
+
+struct APIError: Hashable, Codable {
+ var error: String
+}
+
+func setCredentials(user: String, password: String) {
+ let creds = URLCredential(
+ user: user,
+ password: password,
+ persistence: .permanent
+ )
+ let space = URLProtectionSpace(
+ host: "mx1.mailmux.net",
+ port: 80,
+ protocol: "http",
+ realm: "mailmux",
+ authenticationMethod: NSURLAuthenticationMethodHTTPBasic
+ )
+ URLCredentialStorage.shared.setDefaultCredential(creds, for: space)
+ print("set creds")
+}
+
+func getAliases() async throws -> [Alias] {
+ guard let url = URL(string: "http://mx1.mailmux.net/api/v1/aliases") else {
+ throw MailmuxError.badURL
+ }
+ let (data, resp) = try await URLSession.shared.data(from: url)
+ if let resp = resp as? HTTPURLResponse {
+ if resp.statusCode != 200 {
+ let err = try JSONDecoder().decode(APIError.self, from: data)
+ print("server status code \(resp.statusCode): \(err.error)")
+ }
+ }
+ let faliases = try JSONDecoder().decode([fAlias].self, from: data)
+ var aliases: [Alias] = []
+ for fa in faliases {
+ let a = Alias(
+ id: fa.Recipient,
+ recipient: fa.Recipient,
+ destination: fa.Destination,
+ expiry: parseRFC3339(s: fa.Expiry),
+ note: fa.Note
+ )
+ aliases.append(a)
+ }
+ return aliases
+}