HTTPSCallable.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  16. * A `HTTPSCallableResult` contains the result of calling a `HTTPSCallable`.
  17. */
  18. @objc(FIRHTTPSCallableResult)
  19. open class HTTPSCallableResult: NSObject {
  20. /**
  21. * The data that was returned from the Callable HTTPS trigger.
  22. *
  23. * The data is in the form of native objects. For example, if your trigger returned an
  24. * array, this object would be an NSArray. If your trigger returned a JavaScript object with
  25. * keys and values, this object would be an NSDictionary.
  26. */
  27. @objc public let data: Any
  28. internal init(data: Any) {
  29. self.data = data
  30. }
  31. }
  32. /**
  33. * A `HTTPSCallable` is reference to a particular Callable HTTPS trigger in Cloud Functions.
  34. */
  35. @objc(FIRHTTPSCallable)
  36. open class HTTPSCallable: NSObject {
  37. // MARK: - Private Properties
  38. // The functions client to use for making calls.
  39. private let functions: Functions
  40. // The name of the http endpoint this reference refers to.
  41. private let name: String
  42. // MARK: - Public Properties
  43. /**
  44. * The timeout to use when calling the function. Defaults to 70 seconds.
  45. */
  46. @objc open var timeoutInterval: TimeInterval = 70
  47. internal init(functions: Functions, name: String) {
  48. self.functions = functions
  49. self.name = name
  50. }
  51. /**
  52. * Executes this Callable HTTPS trigger asynchronously.
  53. *
  54. * The data passed into the trigger can be any of the following types:
  55. * * NSNull
  56. * * NSString
  57. * * NSNumber
  58. * * NSArray<id>, where the contained objects are also one of these types.
  59. * * NSDictionary<NSString, id>, where the values are also one of these types.
  60. *
  61. * The request to the Cloud Functions backend made by this method automatically includes a
  62. * Firebase Installations ID token to identify the app instance. If a user is logged in with
  63. * Firebase Auth, an auth ID token for the user is also automatically included.
  64. *
  65. * Firebase Cloud Messaging sends data to the Firebase backend periodically to collect information
  66. * regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  67. * resumes with a new FCM Token the next time you call this method.
  68. *
  69. * @param data Parameters to pass to the trigger.
  70. * @param completion The block to call when the HTTPS request has completed.
  71. */
  72. @objc(callWithObject:completion:) open func call(_ data: Any? = nil,
  73. completion: @escaping (HTTPSCallableResult?,
  74. Error?) -> Void) {
  75. functions.callFunction(name: name,
  76. withObject: data,
  77. timeout: timeoutInterval) { result in
  78. switch result {
  79. case let .success(callableResult):
  80. completion(callableResult, nil)
  81. case let .failure(error):
  82. completion(nil, error)
  83. }
  84. }
  85. }
  86. /**
  87. * Executes this Callable HTTPS trigger asynchronously. This API should only be used from Objective C
  88. *
  89. * The request to the Cloud Functions backend made by this method automatically includes a
  90. * Firebase Installations ID token to identify the app instance. If a user is logged in with
  91. * Firebase Auth, an auth ID token for the user is also automatically included.
  92. *
  93. * Firebase Cloud Messaging sends data to the Firebase backend periodically to collect information
  94. * regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  95. * resumes with a new FCM Token the next time you call this method.
  96. *
  97. * @param completion The block to call when the HTTPS request has completed.
  98. */
  99. @objc(callWithCompletion:) public func __call(completion: @escaping (HTTPSCallableResult?,
  100. Error?) -> Void) {
  101. call(nil, completion: completion)
  102. }
  103. #if compiler(>=5.5.2) && canImport(_Concurrency)
  104. /**
  105. * Executes this Callable HTTPS trigger asynchronously.
  106. *
  107. * The request to the Cloud Functions backend made by this method automatically includes a
  108. * FCM token to identify the app instance. If a user is logged in with Firebase
  109. * Auth, an auth ID token for the user is also automatically included.
  110. *
  111. * Firebase Cloud Messaging sends data to the Firebase backend periodically to collect information
  112. * regarding the app instance. To stop this, see `Messaging.deleteData()`. It
  113. * resumes with a new FCM Token the next time you call this method.
  114. *
  115. * @param data Parameters to pass to the trigger.
  116. * @returns The result of the call.
  117. */
  118. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  119. open func call(_ data: Any? = nil) async throws -> HTTPSCallableResult {
  120. return try await withCheckedThrowingContinuation { continuation in
  121. // TODO(bonus): Use task to handle and cancellation.
  122. self.call(data) { callableResult, error in
  123. if let callableResult = callableResult {
  124. continuation.resume(returning: callableResult)
  125. } else {
  126. continuation.resume(throwing: error!)
  127. }
  128. }
  129. }
  130. }
  131. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  132. }