2022-01-31 09:10:27 -08:00
|
|
|
|
// Copyright 2022 Google LLC
|
2022-01-26 15:24:08 -05:00
|
|
|
|
//
|
2022-01-31 09:10:27 -08:00
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
|
// You may obtain a copy of the License at
|
2022-01-26 15:24:08 -05:00
|
|
|
|
//
|
2022-01-31 09:10:27 -08:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
//
|
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
|
// limitations under the License.
|
2022-01-26 15:24:08 -05:00
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// A `HTTPSCallableResult` contains the result of calling a `HTTPSCallable`.
|
2022-01-26 15:24:08 -05:00
|
|
|
|
@objc(FIRHTTPSCallableResult)
|
2022-02-11 15:51:30 -09:00
|
|
|
|
open class HTTPSCallableResult: NSObject {
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// The data that was returned from the Callable HTTPS trigger.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The data is in the form of native objects. For example, if your trigger returned an
|
|
|
|
|
|
/// array, this object would be an `Array<Any>`. If your trigger returned a JavaScript object with
|
|
|
|
|
|
/// keys and values, this object would be an instance of `[String: Any]`.
|
2022-02-02 16:20:48 -08:00
|
|
|
|
@objc public let data: Any
|
2022-01-26 15:24:08 -05:00
|
|
|
|
|
2023-11-21 12:12:05 -08:00
|
|
|
|
init(data: Any) {
|
2022-01-26 15:24:08 -05:00
|
|
|
|
self.data = data
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-04-26 11:04:38 -07:00
|
|
|
|
* A `HTTPSCallable` is a reference to a particular Callable HTTPS trigger in Cloud Functions.
|
2022-01-26 15:24:08 -05:00
|
|
|
|
*/
|
|
|
|
|
|
@objc(FIRHTTPSCallable)
|
2022-02-11 15:51:30 -09:00
|
|
|
|
open class HTTPSCallable: NSObject {
|
2022-01-26 15:24:08 -05:00
|
|
|
|
// MARK: - Private Properties
|
|
|
|
|
|
|
|
|
|
|
|
// The functions client to use for making calls.
|
|
|
|
|
|
private let functions: Functions
|
|
|
|
|
|
|
2024-10-03 22:07:46 +02:00
|
|
|
|
private let url: URL
|
2022-01-26 15:24:08 -05:00
|
|
|
|
|
2023-05-16 15:33:15 -07:00
|
|
|
|
private let options: HTTPSCallableOptions?
|
|
|
|
|
|
|
2022-01-26 15:24:08 -05:00
|
|
|
|
// MARK: - Public Properties
|
|
|
|
|
|
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// The timeout to use when calling the function. Defaults to 70 seconds.
|
2022-03-03 09:10:32 -08:00
|
|
|
|
@objc open var timeoutInterval: TimeInterval = 70
|
2022-01-26 15:24:08 -05:00
|
|
|
|
|
2023-11-21 12:12:05 -08:00
|
|
|
|
init(functions: Functions, url: URL, options: HTTPSCallableOptions? = nil) {
|
2022-04-19 14:19:55 -07:00
|
|
|
|
self.functions = functions
|
2024-10-03 22:07:46 +02:00
|
|
|
|
self.url = url
|
2023-05-16 15:33:15 -07:00
|
|
|
|
self.options = options
|
2022-01-26 15:24:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// Executes this Callable HTTPS trigger asynchronously.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The data passed into the trigger can be any of the following types:
|
|
|
|
|
|
/// - `nil` or `NSNull`
|
|
|
|
|
|
/// - `String`
|
|
|
|
|
|
/// - `NSNumber`, or any Swift numeric type bridgeable to `NSNumber`
|
|
|
|
|
|
/// - `[Any]`, where the contained objects are also one of these types.
|
|
|
|
|
|
/// - `[String: Any]` where the values are also one of these types.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The request to the Cloud Functions backend made by this method automatically includes a
|
|
|
|
|
|
/// Firebase Installations ID token to identify the app instance. If a user is logged in with
|
|
|
|
|
|
/// Firebase Auth, an auth ID token for the user is also automatically included.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
|
|
|
|
|
|
/// information
|
|
|
|
|
|
/// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
|
|
|
|
|
|
/// resumes with a new FCM Token the next time you call this method.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
|
/// - data: Parameters to pass to the trigger.
|
|
|
|
|
|
/// - completion: The block to call when the HTTPS request has completed.
|
2022-03-03 09:10:32 -08:00
|
|
|
|
@objc(callWithObject:completion:) open func call(_ data: Any? = nil,
|
2025-04-08 17:31:09 -04:00
|
|
|
|
completion: @escaping @MainActor (HTTPSCallableResult?,
|
2022-03-03 09:10:32 -08:00
|
|
|
|
Error?) -> Void) {
|
2024-11-18 16:13:07 +01:00
|
|
|
|
if #available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *) {
|
|
|
|
|
|
Task {
|
|
|
|
|
|
do {
|
|
|
|
|
|
let result = try await call(data)
|
2025-04-08 17:31:09 -04:00
|
|
|
|
await completion(result, nil)
|
2024-11-18 16:13:07 +01:00
|
|
|
|
} catch {
|
2025-04-08 17:31:09 -04:00
|
|
|
|
await completion(nil, error)
|
2024-11-18 16:13:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} 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):
|
2025-04-08 17:31:09 -04:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
completion(callableResult, nil)
|
|
|
|
|
|
}
|
2024-11-18 16:13:07 +01:00
|
|
|
|
case let .failure(error):
|
2025-04-08 17:31:09 -04:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
completion(nil, error)
|
|
|
|
|
|
}
|
2024-11-18 16:13:07 +01:00
|
|
|
|
}
|
2022-01-26 15:24:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// Executes this Callable HTTPS trigger asynchronously. This API should only be used from
|
|
|
|
|
|
/// Objective-C.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The request to the Cloud Functions backend made by this method automatically includes a
|
|
|
|
|
|
/// Firebase Installations ID token to identify the app instance. If a user is logged in with
|
|
|
|
|
|
/// Firebase Auth, an auth ID token for the user is also automatically included.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
|
|
|
|
|
|
/// information
|
|
|
|
|
|
/// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
|
|
|
|
|
|
/// resumes with a new FCM Token the next time you call this method.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - Parameter completion: The block to call when the HTTPS request has completed.
|
2022-02-02 16:20:48 -08:00
|
|
|
|
@objc(callWithCompletion:) public func __call(completion: @escaping (HTTPSCallableResult?,
|
|
|
|
|
|
Error?) -> Void) {
|
|
|
|
|
|
call(nil, completion: completion)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-29 09:43:20 -08:00
|
|
|
|
/// Executes this Callable HTTPS trigger asynchronously.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The request to the Cloud Functions backend made by this method automatically includes a
|
|
|
|
|
|
/// FCM token to identify the app instance. If a user is logged in with Firebase
|
|
|
|
|
|
/// Auth, an auth ID token for the user is also automatically included.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
|
|
|
|
|
|
/// information
|
|
|
|
|
|
/// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
|
|
|
|
|
|
/// resumes with a new FCM Token the next time you call this method.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - Parameter data: Parameters to pass to the trigger.
|
|
|
|
|
|
/// - Throws: An error if the Cloud Functions invocation failed.
|
|
|
|
|
|
/// - Returns: The result of the call.
|
2024-01-03 07:04:02 -08:00
|
|
|
|
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
|
|
|
|
|
|
open func call(_ data: Any? = nil) async throws -> HTTPSCallableResult {
|
2024-10-29 23:44:26 +01:00
|
|
|
|
try await functions
|
|
|
|
|
|
.callFunction(at: url, withObject: data, options: options, timeout: timeoutInterval)
|
2024-01-03 07:04:02 -08:00
|
|
|
|
}
|
2025-03-10 14:37:52 -04:00
|
|
|
|
|
|
|
|
|
|
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
|
|
|
|
|
func stream(_ data: Any? = nil) -> AsyncThrowingStream<JSONStreamResponse, Error> {
|
|
|
|
|
|
functions.stream(at: url, data: data, options: options, timeout: timeoutInterval)
|
|
|
|
|
|
}
|
2022-01-26 15:24:08 -05:00
|
|
|
|
}
|