Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched HTTPSCallable to Async Method #14085

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions FirebaseFunctions/Sources/HTTPSCallable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPSCallableResult, Error>) -> 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
Expand Down
9 changes: 9 additions & 0 deletions FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Loading