FunctionsAPITests.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
  15. import Foundation
  16. import XCTest
  17. import FirebaseCore
  18. import FirebaseFunctions
  19. private struct MyRequest: Codable {
  20. let question: String
  21. }
  22. private struct MyResponse: Codable {
  23. let answer: String
  24. }
  25. final class FunctionsAPITests: XCTestCase {
  26. func usage() {
  27. // MARK: - Functions
  28. // Retrieve Functions instance
  29. _ = Functions.functions()
  30. if let app = FirebaseApp.app() {
  31. _ = Functions.functions(app: app)
  32. _ = Functions.functions(app: app, region: "alderaan")
  33. _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
  34. }
  35. _ = Functions.functions(region: "alderaan")
  36. _ = Functions.functions(customDomain: "https://visitalderaan.com")
  37. // Reference to a callable HTTPS trigger
  38. _ = Functions.functions().httpsCallable("setCourseForAlderaan")
  39. // Functions emulator
  40. Functions.functions().useEmulator(withHost: "host", port: 3000)
  41. if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
  42. // ...
  43. }
  44. // MARK: - HTTPSCallable
  45. let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
  46. callableRef.timeoutInterval = 60
  47. let url = URL(string: "https://localhost:8080/setCourseForAlderaan")!
  48. let callableRefByURL = Functions.functions().httpsCallable(url: url)
  49. let data: Any? = nil
  50. callableRef.call(data) { result, error in
  51. if let result = result {
  52. _ = result.data
  53. } else if let _ /* error */ = error {
  54. // ...
  55. }
  56. }
  57. #if compiler(>=5.5.2) && canImport(_Concurrency)
  58. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  59. // async/await is a Swift 5.5+ feature available on iOS 15+
  60. Task {
  61. do {
  62. let result = try await callableRef.call(data)
  63. _ = result.data
  64. } catch {
  65. // ...
  66. }
  67. }
  68. }
  69. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  70. callableRef.call { result, error in
  71. if let result = result {
  72. _ = result.data
  73. } else if let _ /* error */ = error {
  74. // ...
  75. }
  76. }
  77. #if compiler(>=5.5.2) && canImport(_Concurrency)
  78. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  79. // async/await is a Swift 5.5+ feature available on iOS 15+
  80. Task {
  81. do {
  82. let result = try await callableRef.call()
  83. _ = result.data
  84. } catch {
  85. // ...
  86. }
  87. }
  88. }
  89. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  90. // MARK: - HTTPSCallable Codable
  91. let myRequest = MyRequest(question: "WhereIsIt?")
  92. let callableCodableRef = Functions.functions().httpsCallable("setCourseForAlderaan",
  93. requestAs: MyRequest.self,
  94. responseAs: MyResponse.self)
  95. callableCodableRef.timeoutInterval = 60
  96. callableCodableRef.call(myRequest) { result in
  97. switch result {
  98. case let .success(response):
  99. let _: MyResponse = response
  100. case let .failure(error):
  101. print("error: \(error)")
  102. }
  103. }
  104. // Call as a function.
  105. callableCodableRef(myRequest) { result in
  106. switch result {
  107. case let .success(response):
  108. let _: MyResponse = response
  109. case let .failure(error):
  110. print("error: \(error)")
  111. }
  112. }
  113. #if compiler(>=5.5.2) && canImport(_Concurrency)
  114. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  115. // async/await is a Swift 5.5+ feature available on iOS 15+
  116. Task {
  117. do {
  118. let _: MyResponse = try await callableCodableRef.call()
  119. let _: MyResponse = try await callableCodableRef()
  120. } catch {
  121. // ...
  122. }
  123. }
  124. }
  125. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  126. let callableCodableRefByURL = Functions.functions().httpsCallable(url,
  127. requestAs: MyRequest.self,
  128. responseAs: MyResponse.self)
  129. callableCodableRefByURL.timeoutInterval = 60
  130. callableCodableRefByURL.call(myRequest) { result in
  131. switch result {
  132. case let .success(response):
  133. let _: MyResponse = response
  134. case let .failure(error):
  135. print("error: \(error)")
  136. }
  137. }
  138. // Call as function.
  139. callableCodableRefByURL(myRequest) { result in
  140. switch result {
  141. case let .success(response):
  142. let _: MyResponse = response
  143. case let .failure(error):
  144. print("error: \(error)")
  145. }
  146. }
  147. #if compiler(>=5.5.2) && canImport(_Concurrency)
  148. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  149. // async/await is a Swift 5.5+ feature available on iOS 15+
  150. Task {
  151. do {
  152. let _: MyResponse = try await callableCodableRefByURL.call()
  153. // Call as a function.
  154. let _: MyResponse = try await callableCodableRefByURL()
  155. } catch {
  156. // ...
  157. }
  158. }
  159. }
  160. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  161. // MARK: - FunctionsErrorCode
  162. callableRef.call { _, error in
  163. if let error = error {
  164. switch (error as NSError).code {
  165. case FunctionsErrorCode.OK.rawValue:
  166. break
  167. case FunctionsErrorCode.cancelled.rawValue:
  168. break
  169. case FunctionsErrorCode.unknown.rawValue:
  170. break
  171. case FunctionsErrorCode.invalidArgument.rawValue:
  172. break
  173. case FunctionsErrorCode.deadlineExceeded.rawValue:
  174. break
  175. case FunctionsErrorCode.notFound.rawValue:
  176. break
  177. case FunctionsErrorCode.alreadyExists.rawValue:
  178. break
  179. case FunctionsErrorCode.permissionDenied.rawValue:
  180. break
  181. case FunctionsErrorCode.resourceExhausted.rawValue:
  182. break
  183. case FunctionsErrorCode.failedPrecondition.rawValue:
  184. break
  185. case FunctionsErrorCode.aborted.rawValue:
  186. break
  187. case FunctionsErrorCode.outOfRange.rawValue:
  188. break
  189. case FunctionsErrorCode.unimplemented.rawValue:
  190. break
  191. case FunctionsErrorCode.internal.rawValue:
  192. break
  193. case FunctionsErrorCode.unavailable.rawValue:
  194. break
  195. case FunctionsErrorCode.dataLoss.rawValue:
  196. break
  197. case FunctionsErrorCode.unauthenticated.rawValue:
  198. break
  199. default:
  200. break
  201. }
  202. }
  203. }
  204. }
  205. func testErrorGlobals() {
  206. XCTAssertEqual(FunctionsErrorDetailsKey, "details")
  207. XCTAssertEqual(FunctionsErrorDomain, "com.firebase.functions")
  208. }
  209. }