HTTPSCallable+Combine.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2021 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. #if canImport(Combine) && swift(>=5.0)
  15. import Combine
  16. import FirebaseFunctions
  17. @available(swift 5.0)
  18. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
  19. public extension HTTPSCallable {
  20. // MARK: - HTTPS Callable Functions
  21. /// Executes this Callable HTTPS trigger asynchronously without any parameters.
  22. ///
  23. /// The publisher will emit on the **main** thread.
  24. ///
  25. /// The request to the Cloud Functions backend made by this method automatically includes a
  26. /// Firebase Instance ID token to identify the app instance. If a user is logged in with
  27. /// Firebase
  28. /// Auth, an auth ID token for the user is also automatically included.
  29. ///
  30. /// Firebase Installations ID sends data to the Firebase backend periodically to collect
  31. /// information
  32. /// regarding the app instance. To stop this, see `[FIRInstallations delete]`. It
  33. /// resumes with a new Instance ID the next time you call this method.
  34. ///
  35. /// - Returns: A publisher emitting a `HTTPSCallableResult` instance. The publisher will emit on
  36. /// the *main* thread.
  37. @discardableResult
  38. func call() -> Future<HTTPSCallableResult, Error> {
  39. Future<HTTPSCallableResult, Error> { promise in
  40. self.call { callableResult, error in
  41. if let error = error {
  42. promise(.failure(error))
  43. } else if let callableResult = callableResult {
  44. promise(.success(callableResult))
  45. }
  46. }
  47. }
  48. }
  49. /// Executes this Callable HTTPS trigger asynchronously.
  50. ///
  51. /// The publisher will emit on the **main** thread.
  52. ///
  53. /// The data passed into the trigger can be any of the following types:
  54. /// - `nil`
  55. /// - `String`
  56. /// - `Number`
  57. /// - `Array<Any>`, where the contained objects are also one of these types.
  58. /// - `Dictionary<String, Any>`, where the contained objects are also one of these types.
  59. ///
  60. /// The request to the Cloud Functions backend made by this method automatically includes a
  61. /// Firebase Instance ID token to identify the app instance. If a user is logged in with
  62. /// Firebase
  63. /// Auth, an auth ID token for the user is also automatically included.
  64. ///
  65. /// Firebase Instance ID sends data to the Firebase backend periodically to collect information
  66. /// regarding the app instance. To stop this, see `[FIRInstanceID deleteIDWithHandler:]`. It
  67. /// resumes with a new Instance ID the next time you call this method.
  68. ///
  69. /// - Parameter data: The data passed into the Callable Function.
  70. /// - Returns: A publisher emitting a `HTTPSCallableResult` instance. The publisher will emit on
  71. /// the *main* thread.
  72. @discardableResult
  73. func call(_ data: Any?) -> Future<HTTPSCallableResult, Error> {
  74. Future<HTTPSCallableResult, Error> { promise in
  75. self.call(data) { callableResult, error in
  76. if let error = error {
  77. promise(.failure(error))
  78. } else if let callableResult = callableResult {
  79. promise(.success(callableResult))
  80. }
  81. }
  82. }
  83. }
  84. }
  85. #endif // canImport(Combine) && swift(>=5.0) && canImport(FirebaseFunctions)