HTTPSCallable.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 enum EndpointType {
  37. case name(String)
  38. case url(URL)
  39. }
  40. private let endpoint: EndpointType
  41. private let options: HTTPSCallableOptions?
  42. // MARK: - Public Properties
  43. /// The timeout to use when calling the function. Defaults to 70 seconds.
  44. @objc open var timeoutInterval: TimeInterval = 70
  45. init(functions: Functions, name: String, options: HTTPSCallableOptions? = nil) {
  46. self.functions = functions
  47. self.options = options
  48. endpoint = .name(name)
  49. }
  50. init(functions: Functions, url: URL, options: HTTPSCallableOptions? = nil) {
  51. self.functions = functions
  52. self.options = options
  53. endpoint = .url(url)
  54. }
  55. /// Executes this Callable HTTPS trigger asynchronously.
  56. ///
  57. /// The data passed into the trigger can be any of the following types:
  58. /// - `nil` or `NSNull`
  59. /// - `String`
  60. /// - `NSNumber`, or any Swift numeric type bridgeable to `NSNumber`
  61. /// - `[Any]`, where the contained objects are also one of these types.
  62. /// - `[String: Any]` where the values are also one of these types.
  63. ///
  64. /// The request to the Cloud Functions backend made by this method automatically includes a
  65. /// Firebase Installations ID token to identify the app instance. If a user is logged in with
  66. /// Firebase Auth, an auth ID token for the user is also automatically included.
  67. ///
  68. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  69. /// information
  70. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  71. /// resumes with a new FCM Token the next time you call this method.
  72. ///
  73. /// - Parameters:
  74. /// - data: Parameters to pass to the trigger.
  75. /// - completion: The block to call when the HTTPS request has completed.
  76. @objc(callWithObject:completion:) open func call(_ data: Any? = nil,
  77. completion: @escaping (HTTPSCallableResult?,
  78. Error?) -> Void) {
  79. let callback: ((Result<HTTPSCallableResult, Error>) -> Void) = { result in
  80. switch result {
  81. case let .success(callableResult):
  82. completion(callableResult, nil)
  83. case let .failure(error):
  84. completion(nil, error)
  85. }
  86. }
  87. switch endpoint {
  88. case let .name(name):
  89. functions.callFunction(name: name,
  90. withObject: data,
  91. options: options,
  92. timeout: timeoutInterval,
  93. completion: callback)
  94. case let .url(url):
  95. functions.callFunction(url: url,
  96. withObject: data,
  97. options: options,
  98. timeout: timeoutInterval,
  99. completion: callback)
  100. }
  101. }
  102. /// Executes this Callable HTTPS trigger asynchronously. This API should only be used from
  103. /// Objective-C.
  104. ///
  105. /// The request to the Cloud Functions backend made by this method automatically includes a
  106. /// Firebase Installations ID token to identify the app instance. If a user is logged in with
  107. /// Firebase Auth, an auth ID token for the user is also automatically included.
  108. ///
  109. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  110. /// information
  111. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  112. /// resumes with a new FCM Token the next time you call this method.
  113. ///
  114. /// - Parameter completion: The block to call when the HTTPS request has completed.
  115. @objc(callWithCompletion:) public func __call(completion: @escaping (HTTPSCallableResult?,
  116. Error?) -> Void) {
  117. call(nil, completion: completion)
  118. }
  119. /// Executes this Callable HTTPS trigger asynchronously.
  120. ///
  121. /// The request to the Cloud Functions backend made by this method automatically includes a
  122. /// FCM token to identify the app instance. If a user is logged in with Firebase
  123. /// Auth, an auth ID token for the user is also automatically included.
  124. ///
  125. /// Firebase Cloud Messaging sends data to the Firebase backend periodically to collect
  126. /// information
  127. /// regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  128. /// resumes with a new FCM Token the next time you call this method.
  129. ///
  130. /// - Parameter data: Parameters to pass to the trigger.
  131. /// - Throws: An error if the Cloud Functions invocation failed.
  132. /// - Returns: The result of the call.
  133. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  134. open func call(_ data: Any? = nil) async throws -> HTTPSCallableResult {
  135. return try await withCheckedThrowingContinuation { continuation in
  136. // TODO(bonus): Use task to handle and cancellation.
  137. self.call(data) { callableResult, error in
  138. if let callableResult = callableResult {
  139. continuation.resume(returning: callableResult)
  140. } else {
  141. continuation.resume(throwing: error!)
  142. }
  143. }
  144. }
  145. }
  146. }