FunctionsAPITests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import FirebaseSharedSwift
  20. final class FunctionsAPITests: XCTestCase {
  21. func usage() {
  22. // MARK: - Functions
  23. // Retrieve Functions instance
  24. _ = Functions.functions()
  25. if let app = FirebaseApp.app() {
  26. _ = Functions.functions(app: app)
  27. _ = Functions.functions(app: app, region: "alderaan")
  28. _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
  29. }
  30. _ = Functions.functions(region: "alderaan")
  31. _ = Functions.functions(customDomain: "https://visitalderaan.com")
  32. // Reference to a callable HTTPS trigger
  33. _ = Functions.functions().httpsCallable("setCourseForAlderaan")
  34. // Functions emulator
  35. Functions.functions().useEmulator(withHost: "host", port: 3000)
  36. if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
  37. // ...
  38. }
  39. // MARK: - HTTPSCallable
  40. let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
  41. callableRef.timeoutInterval = 60
  42. let url = URL(string: "https://localhost:8080/setCourseForAlderaan")!
  43. _ = Functions.functions().httpsCallable(url)
  44. struct Message: Codable {
  45. let hello: String
  46. let world: String
  47. }
  48. struct Response: Codable {
  49. let response: String
  50. }
  51. let callableCodable = Functions.functions()
  52. .httpsCallable("codable", requestAs: Message.self, responseAs: Response.self)
  53. _ = Functions.functions()
  54. .httpsCallable(url, requestAs: Message.self, responseAs: Response.self)
  55. let message = Message(hello: "hello", world: "world")
  56. callableCodable.call(message) { result in
  57. switch result {
  58. case let .success(response):
  59. let _: Response = response
  60. case .failure:
  61. ()
  62. }
  63. }
  64. let data: Any? = nil
  65. callableRef.call(data) { result, error in
  66. if let result {
  67. _ = result.data
  68. } else if let _ /* error */ = error {
  69. // ...
  70. }
  71. }
  72. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  73. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  74. Task {
  75. do {
  76. let data: Any? = nil
  77. let result = try await callableRef.call(data)
  78. _ = result.data
  79. } catch {
  80. // ...
  81. }
  82. }
  83. }
  84. callableRef.call { result, error in
  85. if let result {
  86. _ = result.data
  87. } else if let _ /* error */ = error {
  88. // ...
  89. }
  90. }
  91. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  92. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  93. Task {
  94. do {
  95. let result = try await callableRef.call()
  96. _ = result.data
  97. } catch {
  98. // ...
  99. }
  100. }
  101. }
  102. // MARK: - FunctionsErrorCode
  103. callableRef.call { _, error in
  104. if let error {
  105. switch (error as NSError).code {
  106. case FunctionsErrorCode.OK.rawValue:
  107. break
  108. case FunctionsErrorCode.cancelled.rawValue:
  109. break
  110. case FunctionsErrorCode.unknown.rawValue:
  111. break
  112. case FunctionsErrorCode.invalidArgument.rawValue:
  113. break
  114. case FunctionsErrorCode.deadlineExceeded.rawValue:
  115. break
  116. case FunctionsErrorCode.notFound.rawValue:
  117. break
  118. case FunctionsErrorCode.alreadyExists.rawValue:
  119. break
  120. case FunctionsErrorCode.permissionDenied.rawValue:
  121. break
  122. case FunctionsErrorCode.resourceExhausted.rawValue:
  123. break
  124. case FunctionsErrorCode.failedPrecondition.rawValue:
  125. break
  126. case FunctionsErrorCode.aborted.rawValue:
  127. break
  128. case FunctionsErrorCode.outOfRange.rawValue:
  129. break
  130. case FunctionsErrorCode.unimplemented.rawValue:
  131. break
  132. case FunctionsErrorCode.internal.rawValue:
  133. break
  134. case FunctionsErrorCode.unavailable.rawValue:
  135. break
  136. case FunctionsErrorCode.dataLoss.rawValue:
  137. break
  138. case FunctionsErrorCode.unauthenticated.rawValue:
  139. break
  140. default:
  141. break
  142. }
  143. }
  144. }
  145. }
  146. func testErrorGlobals() {
  147. XCTAssertEqual(FunctionsErrorDetailsKey, "details")
  148. XCTAssertEqual(FunctionsErrorDomain, "com.firebase.functions")
  149. }
  150. }