FunctionsAPITests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 result = try await callableRef.call(data)
  77. _ = result.data
  78. } catch {
  79. // ...
  80. }
  81. }
  82. }
  83. callableRef.call { result, error in
  84. if let result {
  85. _ = result.data
  86. } else if let _ /* error */ = error {
  87. // ...
  88. }
  89. }
  90. if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  91. // async/await is a Swift Concurrency feature available on iOS 13+ and macOS 10.15+
  92. Task {
  93. do {
  94. let result = try await callableRef.call()
  95. _ = result.data
  96. } catch {
  97. // ...
  98. }
  99. }
  100. }
  101. // MARK: - FunctionsErrorCode
  102. callableRef.call { _, error in
  103. if let error {
  104. switch (error as NSError).code {
  105. case FunctionsErrorCode.OK.rawValue:
  106. break
  107. case FunctionsErrorCode.cancelled.rawValue:
  108. break
  109. case FunctionsErrorCode.unknown.rawValue:
  110. break
  111. case FunctionsErrorCode.invalidArgument.rawValue:
  112. break
  113. case FunctionsErrorCode.deadlineExceeded.rawValue:
  114. break
  115. case FunctionsErrorCode.notFound.rawValue:
  116. break
  117. case FunctionsErrorCode.alreadyExists.rawValue:
  118. break
  119. case FunctionsErrorCode.permissionDenied.rawValue:
  120. break
  121. case FunctionsErrorCode.resourceExhausted.rawValue:
  122. break
  123. case FunctionsErrorCode.failedPrecondition.rawValue:
  124. break
  125. case FunctionsErrorCode.aborted.rawValue:
  126. break
  127. case FunctionsErrorCode.outOfRange.rawValue:
  128. break
  129. case FunctionsErrorCode.unimplemented.rawValue:
  130. break
  131. case FunctionsErrorCode.internal.rawValue:
  132. break
  133. case FunctionsErrorCode.unavailable.rawValue:
  134. break
  135. case FunctionsErrorCode.dataLoss.rawValue:
  136. break
  137. case FunctionsErrorCode.unauthenticated.rawValue:
  138. break
  139. default:
  140. break
  141. }
  142. }
  143. }
  144. }
  145. func testErrorGlobals() {
  146. XCTAssertEqual(FunctionsErrorDetailsKey, "details")
  147. XCTAssertEqual(FunctionsErrorDomain, "com.firebase.functions")
  148. }
  149. }