HTTPSCallable+Combine.swift 3.5 KB

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