Skip to content

Commit 15caf49

Browse files
authored
Switched HTTPSCallable to Async Method (#14085)
1 parent f981799 commit 15caf49

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

FirebaseFunctions/Sources/HTTPSCallable.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,32 @@ open class HTTPSCallable: NSObject {
7878
@objc(callWithObject:completion:) open func call(_ data: Any? = nil,
7979
completion: @escaping (HTTPSCallableResult?,
8080
Error?) -> Void) {
81-
let callback: ((Result<HTTPSCallableResult, Error>) -> Void) = { result in
82-
switch result {
83-
case let .success(callableResult):
84-
completion(callableResult, nil)
85-
case let .failure(error):
86-
completion(nil, error)
81+
if #available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *) {
82+
Task {
83+
do {
84+
let result = try await call(data)
85+
completion(result, nil)
86+
} catch {
87+
completion(nil, error)
88+
}
89+
}
90+
} else {
91+
// This isn’t expected to ever be called because Functions
92+
// doesn’t officially support the older platforms.
93+
functions.callFunction(
94+
at: url,
95+
withObject: data,
96+
options: options,
97+
timeout: timeoutInterval
98+
) { result in
99+
switch result {
100+
case let .success(callableResult):
101+
completion(callableResult, nil)
102+
case let .failure(error):
103+
completion(nil, error)
104+
}
87105
}
88106
}
89-
90-
functions.callFunction(
91-
at: url,
92-
withObject: data,
93-
options: options,
94-
timeout: timeoutInterval,
95-
completion: callback
96-
)
97107
}
98108

99109
/// Executes this Callable HTTPS trigger asynchronously. This API should only be used from

FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ private let expectationTimeout: TimeInterval = 2
3131
class MockFunctions: Functions {
3232
let mockCallFunction: () throws -> HTTPSCallableResult
3333
var verifyParameters: ((_ url: URL, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
34+
35+
override func callFunction(at url: URL,
36+
withObject data: Any?,
37+
options: HTTPSCallableOptions?,
38+
timeout: TimeInterval) async throws -> HTTPSCallableResult {
39+
try verifyParameters?(url, data, timeout)
40+
return try mockCallFunction()
41+
}
42+
3443
override func callFunction(at url: URL,
3544
withObject data: Any?,
3645
options: HTTPSCallableOptions?,

0 commit comments

Comments
 (0)