1
- /*
2
- * Copyright 2020 Google LLC
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
1
+ // Copyright 2021 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
16
14
17
15
import Foundation
18
16
import FirebaseFunctions
19
17
import FirebaseSharedSwift
20
18
21
19
public extension Functions {
22
- /**
23
- * Creates a reference to the Callable HTTPS trigger with the given name, the type of an `Encodable`
24
- * request and the type of a `Decodable` response.
25
- *
26
- * - Parameter name: The name of the Callable HTTPS trigger
27
- * - Parameter requestType: The type of the `Encodable` entity to use for requests to this `Callable`
28
- * - Parameter responseType: The type of the `Decodable` entity to use for responses from this `Callable`
29
- */
20
+ /// Creates a reference to the Callable HTTPS trigger with the given name, the type of an `Encodable`
21
+ /// request and the type of a `Decodable` response.
22
+ /// - Parameter name: The name of the Callable HTTPS trigger
23
+ /// - Parameter requestType: The type of the `Encodable` entity to use for requests to this `Callable`
24
+ /// - Parameter responseType: The type of the `Decodable` entity to use for responses from this `Callable`
30
25
func httpsCallable< Request: Encodable ,
31
26
Response: Decodable > ( _ name: String ,
32
- requestType: Request . Type ,
33
- responseType: Response . Type ,
34
- encoder: StructureEncoder = StructureEncoder ( ) ,
35
- decoder: StructureDecoder = StructureDecoder ( ) )
27
+ requestAs requestType: Request . Type = Request . self ,
28
+ responseAs responseType: Response . Type = Response . self ,
29
+ encoder: FirebaseDataEncoder = FirebaseDataEncoder ( ) ,
30
+ decoder: FirebaseDataDecoder = FirebaseDataDecoder ( ) )
36
31
-> Callable < Request , Response > {
37
32
return Callable ( callable: httpsCallable ( name) , encoder: encoder, decoder: decoder)
38
33
}
39
-
40
- /// Creates a reference to the Callable HTTPS trigger with the given name. The types of the `Encodable`
41
- /// request and tyhe `Decodable` response will be inferred by the compiler.
42
- ///
43
- /// At the call site, use the following syntax:
44
- ///
45
- /// let greeter: Callable<DataTestRequest, DataTestResponse> = functions.httpsCallable("greeter")
46
- /// try greeter(data) { result in
47
- /// print(result.greeting)
48
- /// }
49
- ///
50
- /// - Parameters:
51
- /// - name: The name of the Callable HTTPS trigger
52
- func httpsCallable< Request, Response> ( _ name: String ,
53
- encoder: StructureEncoder = StructureEncoder ( ) ,
54
- decoder: StructureDecoder = StructureDecoder ( ) )
55
- -> Callable < Request , Response > where Request: Encodable , Response: Decodable {
56
- return Callable ( callable: httpsCallable ( name) , encoder: encoder, decoder: decoder)
57
- }
58
34
}
59
35
60
- /**
61
- * A `Callable` is reference to a particular Callable HTTPS trigger in Cloud Functions.
62
- */
36
+ // A `Callable` is reference to a particular Callable HTTPS trigger in Cloud Functions.
63
37
public struct Callable < Request: Encodable , Response: Decodable > {
64
- /**
65
- * The timeout to use when calling the function. Defaults to 60 seconds.
66
- */
38
+ /// The timeout to use when calling the function. Defaults to 60 seconds.
67
39
public var timeoutInterval : TimeInterval {
68
40
get {
69
41
callable. timeoutInterval
@@ -78,116 +50,127 @@ public struct Callable<Request: Encodable, Response: Decodable> {
78
50
}
79
51
80
52
private let callable : HTTPSCallable
81
- private let encoder : StructureEncoder
82
- private let decoder : StructureDecoder
53
+ private let encoder : FirebaseDataEncoder
54
+ private let decoder : FirebaseDataDecoder
83
55
84
- init ( callable: HTTPSCallable , encoder: StructureEncoder , decoder: StructureDecoder ) {
56
+ init ( callable: HTTPSCallable , encoder: FirebaseDataEncoder , decoder: FirebaseDataDecoder ) {
85
57
self . callable = callable
86
58
self . encoder = encoder
87
59
self . decoder = decoder
88
60
}
89
61
90
- /**
91
- * Executes this Callable HTTPS trigger asynchronously.
92
- *
93
- * The data passed into the trigger must be of the generic `Request` type:
94
- *
95
- * The request to the Cloud Functions backend made by this method automatically includes a
96
- * Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase
97
- * Auth, an auth ID token for the user is also automatically included.
98
- *
99
- * Firebase Instance ID sends data to the Firebase backend periodically to collect information
100
- * regarding the app instance. To stop this, see `[FIRInstanceID deleteIDWithHandler:]`. It
101
- * resumes with a new Instance ID the next time you call this method.
102
- *
103
- * - Parameter data: Parameters to pass to the trigger.
104
- * - Parameter completion: The block to call when the HTTPS request has completed.
105
- *
106
- * - Throws: An error if any value throws an error during encoding.
107
- */
62
+ /// Executes this Callable HTTPS trigger asynchronously.
63
+ ///
64
+ /// The data passed into the trigger must be of the generic `Request` type:
65
+ ///
66
+ /// The request to the Cloud Functions backend made by this method automatically includes a
67
+ /// FCM token to identify the app instance. If a user is logged in with Firebase
68
+ /// Auth, an auth ID token for the user is also automatically included.
69
+ ///
70
+ /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect information
71
+ /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
72
+ /// resumes with a new FCM Token the next time you call this method.
73
+ ///
74
+ /// - Parameter data: Parameters to pass to the trigger.
75
+ /// - Parameter completion: The block to call when the HTTPS request has completed.
108
76
public func call( _ data: Request ,
109
77
completion: @escaping ( Result < Response , Error > )
110
- -> Void ) throws {
111
- let encoded = try encoder. encode ( data)
78
+ -> Void ) {
79
+ do {
80
+ let encoded = try encoder. encode ( data)
112
81
113
- callable. call ( encoded) { result, error in
114
- do {
115
- if let result = result {
116
- let decoded = try decoder. decode ( Response . self, from: result. data)
117
- completion ( . success( decoded) )
118
- } else if let error = error {
82
+ callable. call ( encoded) { result, error in
83
+ do {
84
+ if let result = result {
85
+ let decoded = try decoder. decode ( Response . self, from: result. data)
86
+ completion ( . success( decoded) )
87
+ } else if let error = error {
88
+ completion ( . failure( error) )
89
+ } else {
90
+ completion ( . failure( CallableError . internalError) )
91
+ }
92
+ } catch {
119
93
completion ( . failure( error) )
120
- } else {
121
- completion ( . failure( CallableError . internalError) )
122
94
}
123
- } catch {
124
- completion ( . failure( error) )
125
95
}
96
+ } catch {
97
+ completion ( . failure( error) )
126
98
}
127
99
}
128
100
129
101
/// Creates a directly callable function.
130
102
///
131
- /// This allows users to call a HTTPS Callable Funciton like a normal Swift function:
132
- ///
103
+ /// This allows users to call a HTTPS Callable Function like a normal Swift function:
104
+ /// ```swift
133
105
/// let greeter = functions.httpsCallable("greeter",
134
106
/// requestType: GreetingRequest.self,
135
107
/// responseType: GreetingResponse.self)
136
- /// try greeter(data) { result in
108
+ /// greeter(data) { result in
137
109
/// print(result.greeting)
138
110
/// }
139
- ///
111
+ /// ```
112
+ /// You can also call a HTTPS Callable function using the following syntax:
113
+ /// ```swift
114
+ /// let greeter: Callable<GreetingRequest, GreetingResponse> = functions.httpsCallable("greeter")
115
+ /// greeter(data) { result in
116
+ /// print(result.greeting)
117
+ /// }
118
+ /// ```
140
119
/// - Parameters:
141
120
/// - data: Parameters to pass to the trigger.
142
121
/// - completion: The block to call when the HTTPS request has completed.
143
122
public func callAsFunction( _ data: Request ,
144
123
completion: @escaping ( Result < Response , Error > )
145
- -> Void ) throws {
146
- try call ( data, completion: completion)
124
+ -> Void ) {
125
+ call ( data, completion: completion)
147
126
}
148
127
149
128
#if compiler(>=5.5) && canImport(_Concurrency)
150
- /**
151
- * Executes this Callable HTTPS trigger asynchronously.
152
- *
153
- * The data passed into the trigger must be of the generic `Request` type:
154
- *
155
- * The request to the Cloud Functions backend made by this method automatically includes a
156
- * Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase
157
- * Auth, an auth ID token for the user is also automatically included.
158
- *
159
- * Firebase Instance ID sends data to the Firebase backend periodically to collect information
160
- * regarding the app instance. To stop this, see `[FIRInstanceID deleteIDWithHandler:]`. It
161
- * resumes with a new Instance ID the next time you call this method.
162
- *
163
- * - Parameter data: The `Request` representing the data to pass to the trigger.
164
- *
165
- * - Throws: An error if any value throws an error during encoding.
166
- * - Throws: An error if any value throws an error during decoding.
167
- * - Throws: An error if the callable fails to complete
168
- *
169
- * - Returns: The decoded `Response` value
170
- */
129
+ /// Executes this Callable HTTPS trigger asynchronously.
130
+ ///
131
+ /// The data passed into the trigger must be of the generic `Request` type:
132
+ ///
133
+ /// The request to the Cloud Functions backend made by this method automatically includes a
134
+ /// FCM token to identify the app instance. If a user is logged in with Firebase
135
+ /// Auth, an auth ID token for the user is also automatically included.
136
+ ///
137
+ /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect information
138
+ /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
139
+ /// resumes with a new FCM Token the next time you call this method.
140
+ ///
141
+ /// - Parameter data: The `Request` representing the data to pass to the trigger.
142
+ ///
143
+ /// - Throws: An error if any value throws an error during encoding.
144
+ /// - Throws: An error if any value throws an error during decoding.
145
+ /// - Throws: An error if the callable fails to complete
146
+ ///
147
+ /// - Returns: The decoded `Response` value
171
148
@available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
172
149
public func call( _ data: Request ,
173
- encoder: StructureEncoder = StructureEncoder ( ) ,
174
- decoder: StructureDecoder =
175
- StructureDecoder ( ) ) async throws -> Response {
150
+ encoder: FirebaseDataEncoder = FirebaseDataEncoder ( ) ,
151
+ decoder: FirebaseDataDecoder =
152
+ FirebaseDataDecoder ( ) ) async throws -> Response {
176
153
let encoded = try encoder. encode ( data)
177
154
let result = try await callable. call ( encoded)
178
155
return try decoder. decode ( Response . self, from: result. data)
179
156
}
180
157
181
158
/// Creates a directly callable function.
182
159
///
183
- /// This allows users to call a HTTPS Callable Funciton like a normal Swift function:
184
- ///
160
+ /// This allows users to call a HTTPS Callable Function like a normal Swift function:
161
+ /// ```swift
185
162
/// let greeter = functions.httpsCallable("greeter",
186
163
/// requestType: GreetingRequest.self,
187
164
/// responseType: GreetingResponse.self)
188
165
/// let result = try await greeter(data)
189
166
/// print(result.greeting)
190
- ///
167
+ /// ```
168
+ /// You can also call a HTTPS Callable function using the following syntax:
169
+ /// ```swift
170
+ /// let greeter: Callable<GreetingRequest, GreetingResponse> = functions.httpsCallable("greeter")
171
+ /// let result = try await greeter(data)
172
+ /// print(result.greeting)
173
+ /// ```
191
174
/// - Parameters:
192
175
/// - data: Parameters to pass to the trigger.
193
176
/// - Returns: The decoded `Response` value
0 commit comments