diff --git a/FirebaseFunctions/Sources/HTTPSCallable.swift b/FirebaseFunctions/Sources/HTTPSCallable.swift index 2c772bc8c78..c2281e54866 100644 --- a/FirebaseFunctions/Sources/HTTPSCallable.swift +++ b/FirebaseFunctions/Sources/HTTPSCallable.swift @@ -78,22 +78,32 @@ open class HTTPSCallable: NSObject { @objc(callWithObject:completion:) open func call(_ data: Any? = nil, completion: @escaping (HTTPSCallableResult?, Error?) -> Void) { - let callback: ((Result) -> Void) = { result in - switch result { - case let .success(callableResult): - completion(callableResult, nil) - case let .failure(error): - completion(nil, error) + if #available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *) { + Task { + do { + let result = try await call(data) + completion(result, nil) + } catch { + completion(nil, error) + } + } + } else { + // This isn’t expected to ever be called because Functions + // doesn’t officially support the older platforms. + functions.callFunction( + at: url, + withObject: data, + options: options, + timeout: timeoutInterval + ) { result in + switch result { + case let .success(callableResult): + completion(callableResult, nil) + case let .failure(error): + completion(nil, error) + } } } - - functions.callFunction( - at: url, - withObject: data, - options: options, - timeout: timeoutInterval, - completion: callback - ) } /// Executes this Callable HTTPS trigger asynchronously. This API should only be used from diff --git a/FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift b/FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift index 8ed63c344b7..3c1399ad522 100644 --- a/FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift +++ b/FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift @@ -31,6 +31,15 @@ private let expectationTimeout: TimeInterval = 2 class MockFunctions: Functions { let mockCallFunction: () throws -> HTTPSCallableResult var verifyParameters: ((_ url: URL, _ data: Any?, _ timeout: TimeInterval) throws -> Void)? + + override func callFunction(at url: URL, + withObject data: Any?, + options: HTTPSCallableOptions?, + timeout: TimeInterval) async throws -> HTTPSCallableResult { + try verifyParameters?(url, data, timeout) + return try mockCallFunction() + } + override func callFunction(at url: URL, withObject data: Any?, options: HTTPSCallableOptions?,