HTTPSCallable.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// A `HTTPSCallableResult` contains the result of calling a `HTTPSCallable`.
  16. @objc(FIRHTTPSCallableResult)
  17. open class HTTPSCallableResult: NSObject {
  18. /// The data that was returned from the Callable HTTPS trigger.
  19. ///
  20. /// The data is in the form of native objects. For example, if your trigger returned an
  21. /// array, this object would be an `Array<Any>`. If your trigger returned a JavaScript object with
  22. /// keys and values, this object would be an instance of `[String: Any]`.
  23. @objc public let data: Any
  24. init(data: Any) {
  25. self.data = data
  26. }
  27. }
  28. /**
  29. * A `HTTPSCallable` is a reference to a particular Callable HTTPS trigger in Cloud Functions.
  30. */
  31. @objc(FIRHTTPSCallable)
  32. open class HTTPSCallable: NSObject {
  33. // MARK: - Private Properties
  34. // The functions client to use for making calls.
  35. private let functions: Functions
  36. private let url: URL
  37. private let options: HTTPSCallableOptions?
  38. // MARK: - Public Properties
  39. /// The timeout to use when calling the function. Defaults to 70 seconds.
  40. @objc open var timeoutInterval: TimeInterval = 70
  41. init(functions: Functions, url: URL, options: HTTPSCallableOptions? = nil) {
  42. self.functions = functions
  43. self.url = url
  44. self.options = options
  45. }
  46. /// Executes this Callable HTTPS trigger asynchronously.
  47. ///
  48. /// The data passed into the trigger can be any of the following types:
  49. /// - `nil` or `NSNull`
  50. /// - `String`
  51. /// - `NSNumber`, or any Swift numeric type bridgeable to `NSNumber`
  52. /// - `[Any]`, where the contained objects are also one of these types.
  53. /// - `[String: Any]` where the values are also one of these types.
  54. ///
  55. /// The request to the Cloud Functions backend made by this method automatically includes a
  56. /// Firebase Installations ID token to identify the app instance. If a user is logged in with
  57. /// Firebase Auth, an auth ID token for the user is also automatically included.
  58. ///
  59. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  60. /// information
  61. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  62. /// resumes with a new FCM Token the next time you call this method.
  63. ///
  64. /// - Parameters:
  65. /// - data: Parameters to pass to the trigger.
  66. /// - completion: The block to call when the HTTPS request has completed.
  67. @objc(callWithObject:completion:) open func call(_ data: Any? = nil,
  68. completion: @escaping @MainActor (HTTPSCallableResult?,
  69. Error?) -> Void) {
  70. if #available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *) {
  71. Task {
  72. do {
  73. let result = try await call(data)
  74. await completion(result, nil)
  75. } catch {
  76. await completion(nil, error)
  77. }
  78. }
  79. } else {
  80. // This isn’t expected to ever be called because Functions
  81. // doesn’t officially support the older platforms.
  82. functions.callFunction(
  83. at: url,
  84. withObject: data,
  85. options: options,
  86. timeout: timeoutInterval
  87. ) { result in
  88. switch result {
  89. case let .success(callableResult):
  90. DispatchQueue.main.async {
  91. completion(callableResult, nil)
  92. }
  93. case let .failure(error):
  94. DispatchQueue.main.async {
  95. completion(nil, error)
  96. }
  97. }
  98. }
  99. }
  100. }
  101. /// Executes this Callable HTTPS trigger asynchronously. This API should only be used from
  102. /// Objective-C.
  103. ///
  104. /// The request to the Cloud Functions backend made by this method automatically includes a
  105. /// Firebase Installations ID token to identify the app instance. If a user is logged in with
  106. /// Firebase Auth, an auth ID token for the user is also automatically included.
  107. ///
  108. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  109. /// information
  110. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  111. /// resumes with a new FCM Token the next time you call this method.
  112. ///
  113. /// - Parameter completion: The block to call when the HTTPS request has completed.
  114. @objc(callWithCompletion:) public func __call(completion: @escaping (HTTPSCallableResult?,
  115. Error?) -> Void) {
  116. call(nil, completion: completion)
  117. }
  118. /// Executes this Callable HTTPS trigger asynchronously.
  119. ///
  120. /// The request to the Cloud Functions backend made by this method automatically includes a
  121. /// FCM token to identify the app instance. If a user is logged in with Firebase
  122. /// Auth, an auth ID token for the user is also automatically included.
  123. ///
  124. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  125. /// information
  126. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  127. /// resumes with a new FCM Token the next time you call this method.
  128. ///
  129. /// - Parameter data: Parameters to pass to the trigger.
  130. /// - Throws: An error if the Cloud Functions invocation failed.
  131. /// - Returns: The result of the call.
  132. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  133. open func call(_ data: Any? = nil) async throws -> HTTPSCallableResult {
  134. try await functions
  135. .callFunction(at: url, withObject: data, options: options, timeout: timeoutInterval)
  136. }
  137. @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
  138. func stream(_ data: Any? = nil) -> AsyncThrowingStream<JSONStreamResponse, Error> {
  139. functions.stream(at: url, data: data, options: options, timeout: timeoutInterval)
  140. }
  141. }