-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFunctionsClient.swift
109 lines (96 loc) · 3.07 KB
/
FunctionsClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
@preconcurrency import Foundation
@preconcurrency import Get
public final class FunctionsClient {
let url: URL
var headers: [String: String]
let client: APIClient
public init(
url: URL,
headers: [String: String] = [:],
apiClientDelegate: APIClientDelegate? = nil
) {
self.url = url
self.headers = headers
self.headers["X-Client-Info"] = "functions-swift/\(version)"
client = APIClient(baseURL: url) {
$0.delegate = apiClientDelegate
}
}
/// Updates the authorization header.
/// - Parameter token: the new JWT token sent in the authorization header
public func setAuth(token: String) {
headers["Authorization"] = "Bearer \(token)"
}
/// Invokes a function.
/// - Parameters:
/// - functionName: the name of the function to invoke.
public func invoke<Response>(
functionName: String,
query: [(String, String?)]? = nil,
invokeOptions: FunctionInvokeOptions = .init(),
decode: (Data, HTTPURLResponse) throws -> Response
) async throws -> Response {
let (data, response) = try await rawInvoke(
functionName: functionName,
query: query,
invokeOptions: invokeOptions
)
return try decode(data, response)
}
/// Invokes a function.
/// - Parameters:
/// - functionName: the name of the function to invoke.
public func invoke<T: Decodable>(
functionName: String,
query: [(String, String?)]? = nil,
invokeOptions: FunctionInvokeOptions = .init(),
decoder: JSONDecoder = JSONDecoder()
) async throws -> T {
try await invoke(
functionName: functionName,
query: query,
invokeOptions: invokeOptions,
decode: { data, _ in try decoder.decode(T.self, from: data) }
)
}
/// Invokes a function.
/// - Parameters:
/// - functionName: the name of the function to invoke.
public func invoke(
functionName: String,
query: [(String, String?)]? = nil,
invokeOptions: FunctionInvokeOptions = .init()
) async throws {
try await invoke(
functionName: functionName,
query: query,
invokeOptions: invokeOptions,
decode: { _, _ in () }
)
}
private func rawInvoke(
functionName: String,
query: [(String, String?)]? = nil,
invokeOptions: FunctionInvokeOptions
) async throws -> (Data, HTTPURLResponse) {
let request = Request(
path: functionName,
method: invokeOptions.method.map({ HTTPMethod(rawValue: $0.rawValue) }) ?? .post,
query: query,
body: invokeOptions.body,
headers: invokeOptions.headers.merging(headers) { first, _ in first }
)
let response = try await client.data(for: request)
guard let httpResponse = response.response as? HTTPURLResponse else {
throw URLError(.badServerResponse)
}
guard 200..<300 ~= httpResponse.statusCode else {
throw FunctionsError.httpError(code: httpResponse.statusCode, data: response.data)
}
let isRelayError = httpResponse.value(forHTTPHeaderField: "x-relay-error") == "true"
if isRelayError {
throw FunctionsError.relayError
}
return (response.data, httpResponse)
}
}