|
1 |
| -public struct functions_swift { |
2 |
| - public private(set) var text = "Hello, World!" |
| 1 | +import Foundation |
3 | 2 |
|
4 |
| - public init() { |
| 3 | +public final class FunctionsClient { |
| 4 | + let url: URL |
| 5 | + var headers: [String: String] |
| 6 | + |
| 7 | + private let session: URLSession = .shared |
| 8 | + |
| 9 | + public init(url: URL, headers: [String: String] = [:]) { |
| 10 | + self.url = url |
| 11 | + self.headers = headers |
| 12 | + self.headers["X-Client-Info"] = "functions-swift/\(version)" |
| 13 | + } |
| 14 | + |
| 15 | + /// Updates the authorization header. |
| 16 | + /// - Parameter token: the new JWT token sent in the authorization header |
| 17 | + public func setAuth(token: String) { |
| 18 | + headers["Authorization"] = "Bearer \(token)" |
| 19 | + } |
| 20 | + |
| 21 | + /// Invokes a function. |
| 22 | + /// - Parameters: |
| 23 | + /// - functionName: the name of the function to invoke. |
| 24 | + public func invoke<Response>( |
| 25 | + functionName: String, |
| 26 | + invokeOptions: FunctionInvokeOptions = .init(), |
| 27 | + decode: (Data, HTTPURLResponse) throws -> Response |
| 28 | + ) async throws -> Response { |
| 29 | + let (data, response) = try await rawInvoke( |
| 30 | + functionName: functionName, |
| 31 | + invokeOptions: invokeOptions |
| 32 | + ) |
| 33 | + return try decode(data, response) |
| 34 | + } |
| 35 | + |
| 36 | + /// Invokes a function. |
| 37 | + /// - Parameters: |
| 38 | + /// - functionName: the name of the function to invoke. |
| 39 | + public func invoke<T: Decodable>( |
| 40 | + functionName: String, |
| 41 | + invokeOptions: FunctionInvokeOptions = .init(), |
| 42 | + decoder: JSONDecoder = JSONDecoder() |
| 43 | + ) async throws -> T { |
| 44 | + try await invoke( |
| 45 | + functionName: functionName, |
| 46 | + invokeOptions: invokeOptions, |
| 47 | + decode: { data, _ in try decoder.decode(T.self, from: data) } |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + /// Invokes a function. |
| 52 | + /// - Parameters: |
| 53 | + /// - functionName: the name of the function to invoke. |
| 54 | + public func invoke( |
| 55 | + functionName: String, |
| 56 | + invokeOptions: FunctionInvokeOptions = .init() |
| 57 | + ) async throws { |
| 58 | + try await invoke( |
| 59 | + functionName: functionName, |
| 60 | + invokeOptions: invokeOptions, |
| 61 | + decode: { _, _ in () } |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + private func rawInvoke( |
| 66 | + functionName: String, |
| 67 | + invokeOptions: FunctionInvokeOptions |
| 68 | + ) async throws -> (Data, HTTPURLResponse) { |
| 69 | + let body = invokeOptions.body |
| 70 | + let invokeHeaders = invokeOptions.headers |
| 71 | + |
| 72 | + var request = URLRequest(url: url.appendingPathComponent(functionName)) |
| 73 | + request.httpMethod = "POST" |
| 74 | + request.httpBody = body |
| 75 | + request.allHTTPHeaderFields = invokeHeaders.merging(headers) { invokeHeader, _ in invokeHeader } |
| 76 | + |
| 77 | + let (data, response) = try await session.data(for: request) |
| 78 | + guard let httpResponse = response as? HTTPURLResponse else { |
| 79 | + throw URLError(.badServerResponse) |
| 80 | + } |
| 81 | + |
| 82 | + guard 200 ..< 300 ~= httpResponse.statusCode else { |
| 83 | + throw FunctionsError.httpError(code: httpResponse.statusCode, data: data) |
5 | 84 | }
|
| 85 | + |
| 86 | + let isRelayError = httpResponse.value(forHTTPHeaderField: "x-relay-error") == "true" |
| 87 | + if isRelayError { |
| 88 | + throw FunctionsError.relayError |
| 89 | + } |
| 90 | + |
| 91 | + return (data, httpResponse) |
| 92 | + } |
6 | 93 | }
|
0 commit comments