// Fiskon Relay Terminal SDK for Swift (iOS 15+, macOS 12+, async/await). No dependencies. // // let relay = Relay(apiKey: "YOUR_SECRET_KEY") // servers / trusted code only // let device = Relay(publicKey: "pk_...") // safe inside your app // _ = try await device.registerDevice(["token": fcmToken, "platform": "ios", "external_id": "user-42"]) import Foundation public struct RelayError: Error, CustomStringConvertible { public let message: String public let code: String? public let status: Int public let data: Any? public var description: String { "RelayError(\(code ?? "error"), \(status)): \(message)" } } public final class Relay { public static let defaultBase = "https://relay.naweriindustries.com/api/v1" private let apiKey: String? private let publicKey: String? private let baseUrl: String private let session: URLSession public init(apiKey: String? = nil, publicKey: String? = nil, baseUrl: String = Relay.defaultBase, session: URLSession = .shared) { precondition(apiKey != nil || publicKey != nil, "Provide an apiKey or a publicKey") self.apiKey = apiKey self.publicKey = publicKey self.baseUrl = baseUrl.hasSuffix("/") ? String(baseUrl.dropLast()) : baseUrl self.session = session } private func request(_ method: String, _ path: String, _ body: [String: Any]? = nil, idempotencyKey: String? = nil, client: Bool = false) async throws -> [String: Any] { guard let url = URL(string: baseUrl + path) else { throw RelayError(message: "Bad URL", code: "bad_url", status: 0, data: nil) } var req = URLRequest(url: url, timeoutInterval: 30) req.httpMethod = method req.setValue("application/json", forHTTPHeaderField: "Accept") if client, let pk = publicKey, apiKey == nil { req.setValue(pk, forHTTPHeaderField: "X-Public-Key") } else if let k = apiKey { req.setValue("Bearer \(k)", forHTTPHeaderField: "Authorization") } else { throw RelayError(message: "This call needs the secret apiKey", code: "no_key", status: 0, data: nil) } if let idem = idempotencyKey { req.setValue(idem, forHTTPHeaderField: "Idempotency-Key") } if let body = body { req.setValue("application/json", forHTTPHeaderField: "Content-Type") req.httpBody = try JSONSerialization.data(withJSONObject: body) } let (data, resp) = try await session.data(for: req) let status = (resp as? HTTPURLResponse)?.statusCode ?? 0 let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? ["error": 1, "code": "bad_response", "message": "HTTP \(status)"] if status >= 400 || (json["error"] as? Int ?? 0) != 0 { throw RelayError(message: json["message"] as? String ?? "Request failed", code: json["code"] as? String, status: status, data: json["data"]) } return json } public func sendEmail(_ p: [String: Any], idempotencyKey: String? = nil) async throws -> [String: Any] { try await request("POST", "/email", p, idempotencyKey: idempotencyKey) } public func sendSms(_ p: [String: Any], idempotencyKey: String? = nil) async throws -> [String: Any] { try await request("POST", "/sms", p, idempotencyKey: idempotencyKey) } public func sendWhatsapp(_ p: [String: Any], idempotencyKey: String? = nil) async throws -> [String: Any] { try await request("POST", "/whatsapp", p, idempotencyKey: idempotencyKey) } public func sendPush(_ p: [String: Any], idempotencyKey: String? = nil) async throws -> [String: Any] { try await request("POST", "/push", p, idempotencyKey: idempotencyKey) } public func registerDevice(_ p: [String: Any]) async throws -> [String: Any] { try await request("POST", "/push/devices", p, client: true) } public func removeDevice(_ p: [String: Any]) async throws -> [String: Any] { try await request("DELETE", "/push/devices", p, client: true) } public func sendTransactional(_ p: [String: Any], idempotencyKey: String? = nil) async throws -> [String: Any] { try await request("POST", "/transactional/email", p, idempotencyKey: idempotencyKey) } public func templates() async throws -> [String: Any] { try await request("GET", "/templates") } public func sendOtp(_ p: [String: Any]) async throws -> [String: Any] { try await request("POST", "/otp", p) } public func verifyOtp(_ p: [String: Any]) async throws -> [String: Any] { try await request("POST", "/otp/verify", p) } public func balance() async throws -> [String: Any] { try await request("GET", "/balance") } public func status(_ channel: String, _ id: Int) async throws -> [String: Any] { try await request("GET", "/messages/\(channel)/\(id)") } }