@@ -198,24 +198,7 @@ public final class FunctionsClient: Sendable {
198
198
199
199
let urlRequest = buildRequest ( functionName: functionName, options: invokeOptions) . urlRequest
200
200
201
- let task = session. dataTask ( with: urlRequest) { data, response, _ in
202
- guard let httpResponse = response as? HTTPURLResponse else {
203
- continuation. finish ( throwing: URLError ( . badServerResponse) )
204
- return
205
- }
206
-
207
- guard 200 ..< 300 ~= httpResponse. statusCode else {
208
- let error = FunctionsError . httpError ( code: httpResponse. statusCode, data: data ?? Data ( ) )
209
- continuation. finish ( throwing: error)
210
- return
211
- }
212
-
213
- let isRelayError = httpResponse. value ( forHTTPHeaderField: " x-relay-error " ) == " true "
214
- if isRelayError {
215
- continuation. finish ( throwing: FunctionsError . relayError)
216
- }
217
- }
218
-
201
+ let task = session. dataTask ( with: urlRequest)
219
202
task. resume ( )
220
203
221
204
continuation. onTermination = { _ in
@@ -259,4 +242,23 @@ final class StreamResponseDelegate: NSObject, URLSessionDataDelegate, Sendable {
259
242
func urlSession( _: URLSession , task _: URLSessionTask , didCompleteWithError error: ( any Error ) ? ) {
260
243
continuation. finish ( throwing: error)
261
244
}
245
+
246
+ func urlSession( _: URLSession , dataTask _: URLSessionDataTask , didReceive response: URLResponse , completionHandler: @escaping ( URLSession . ResponseDisposition ) -> Void ) {
247
+ guard let httpResponse = response as? HTTPURLResponse else {
248
+ continuation. finish ( throwing: URLError ( . badServerResponse) )
249
+ return
250
+ }
251
+
252
+ guard 200 ..< 300 ~= httpResponse. statusCode else {
253
+ let error = FunctionsError . httpError ( code: httpResponse. statusCode, data: Data ( ) )
254
+ continuation. finish ( throwing: error)
255
+ return
256
+ }
257
+
258
+ let isRelayError = httpResponse. value ( forHTTPHeaderField: " x-relay-error " ) == " true "
259
+ if isRelayError {
260
+ continuation. finish ( throwing: FunctionsError . relayError)
261
+ }
262
+ completionHandler ( . allow)
263
+ }
262
264
}
0 commit comments