HTTPSCallable+Combine.swift 3.7 KB

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