Callable+Codable.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import FirebaseFunctions
  18. import FirebaseSharedSwift
  19. public extension Functions {
  20. /**
  21. * Creates a reference to the Callable HTTPS trigger with the given name, the type of an `Encodable`
  22. * request and the type of a `Decodable` response.
  23. *
  24. * - Parameter name: The name of the Callable HTTPS trigger
  25. * - Parameter requestType: The type of the `Encodable` entity to use for requests to this `Callable`
  26. * - Parameter responseType: The type of the `Decodable` entity to use for responses from this `Callable`
  27. */
  28. func httpsCallable<Request: Encodable,
  29. Response: Decodable>(_ name: String,
  30. requestType: Request.Type,
  31. responseType: Response.Type)
  32. -> Callable<Request, Response> {
  33. return Callable(callable: httpsCallable(name))
  34. }
  35. }
  36. /**
  37. * A `Callable` is reference to a particular Callable HTTPS trigger in Cloud Functions.
  38. */
  39. public struct Callable<Request: Encodable, Response: Decodable> {
  40. /**
  41. * The timeout to use when calling the function. Defaults to 60 seconds.
  42. */
  43. public var timeoutInterval: TimeInterval {
  44. get {
  45. callable.timeoutInterval
  46. }
  47. set {
  48. callable.timeoutInterval = newValue
  49. }
  50. }
  51. enum CallableError: Error {
  52. case internalError
  53. }
  54. private let callable: HTTPSCallable
  55. init(callable: HTTPSCallable) {
  56. self.callable = callable
  57. }
  58. /**
  59. * Executes this Callable HTTPS trigger asynchronously.
  60. *
  61. * The data passed into the trigger must be of the generic `Request` type:
  62. *
  63. * The request to the Cloud Functions backend made by this method automatically includes a
  64. * Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase
  65. * Auth, an auth ID token for the user is also automatically included.
  66. *
  67. * Firebase Instance ID sends data to the Firebase backend periodically to collect information
  68. * regarding the app instance. To stop this, see `[FIRInstanceID deleteIDWithHandler:]`. It
  69. * resumes with a new Instance ID the next time you call this method.
  70. *
  71. * - Parameter data: Parameters to pass to the trigger.
  72. * - Parameter completion: The block to call when the HTTPS request has completed.
  73. *
  74. * - Throws: An error if any value throws an error during encoding.
  75. */
  76. public func call(_ data: Request,
  77. encoder: StructureEncoder = StructureEncoder(),
  78. decoder: StructureDecoder = StructureDecoder(),
  79. completion: @escaping (Result<Response, Error>)
  80. -> Void) throws {
  81. let encoded = try encoder.encode(data)
  82. callable.call(encoded) { result, error in
  83. do {
  84. if let result = result {
  85. let decoded = try decoder.decode(Response.self, from: result.data)
  86. completion(.success(decoded))
  87. } else if let error = error {
  88. completion(.failure(error))
  89. } else {
  90. completion(.failure(CallableError.internalError))
  91. }
  92. } catch {
  93. completion(.failure(error))
  94. }
  95. }
  96. }
  97. #if compiler(>=5.5) && canImport(_Concurrency)
  98. /**
  99. * Executes this Callable HTTPS trigger asynchronously.
  100. *
  101. * The data passed into the trigger must be of the generic `Request` type:
  102. *
  103. * The request to the Cloud Functions backend made by this method automatically includes a
  104. * Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase
  105. * Auth, an auth ID token for the user is also automatically included.
  106. *
  107. * Firebase Instance ID sends data to the Firebase backend periodically to collect information
  108. * regarding the app instance. To stop this, see `[FIRInstanceID deleteIDWithHandler:]`. It
  109. * resumes with a new Instance ID the next time you call this method.
  110. *
  111. * - Parameter data: The `Request` representing the data to pass to the trigger.
  112. *
  113. * - Throws: An error if any value throws an error during encoding.
  114. * - Throws: An error if any value throws an error during decoding.
  115. * - Throws: An error if the callable fails to complete
  116. *
  117. * - Returns: The decoded `Response` value
  118. */
  119. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  120. public func call(_ data: Request,
  121. encoder: StructureEncoder = StructureEncoder(),
  122. decoder: StructureDecoder =
  123. StructureDecoder()) async throws -> Response {
  124. let encoded = try encoder.encode(data)
  125. let result = try await callable.call(encoded)
  126. return try decoder.decode(Response.self, from: result.data)
  127. }
  128. #endif
  129. }