FunctionsAPITests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright 2021 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. // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
  17. import Foundation
  18. import FirebaseCore
  19. import FirebaseFunctions
  20. final class FunctionsAPITests {
  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 data: Any? = nil
  43. callableRef.call(data) { result, error in
  44. if let result = result {
  45. _ = result.data
  46. } else if let _ /* error */ = error {
  47. // ...
  48. }
  49. }
  50. #if compiler(>=5.5) && canImport(_Concurrency)
  51. if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  52. // async/await is a Swift 5.5+ feature available on iOS 15+
  53. Task {
  54. do {
  55. let result = try await callableRef.call(data)
  56. _ = result.data
  57. } catch {
  58. // ...
  59. }
  60. }
  61. }
  62. #endif // compiler(>=5.5) && canImport(_Concurrency)
  63. callableRef.call { result, error in
  64. if let result = result {
  65. _ = result.data
  66. } else if let _ /* error */ = error {
  67. // ...
  68. }
  69. }
  70. #if compiler(>=5.5) && canImport(_Concurrency)
  71. if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
  72. // async/await is a Swift 5.5+ feature available on iOS 15+
  73. Task {
  74. do {
  75. let result = try await callableRef.call()
  76. _ = result.data
  77. } catch {
  78. // ...
  79. }
  80. }
  81. }
  82. #endif // compiler(>=5.5) && canImport(_Concurrency)
  83. // MARK: - FunctionsErrorCode
  84. callableRef.call { _, error in
  85. if let error = error {
  86. switch (error as NSError).code {
  87. case FunctionsErrorCode.OK.rawValue:
  88. break
  89. case FunctionsErrorCode.cancelled.rawValue:
  90. break
  91. case FunctionsErrorCode.unknown.rawValue:
  92. break
  93. case FunctionsErrorCode.invalidArgument.rawValue:
  94. break
  95. case FunctionsErrorCode.deadlineExceeded.rawValue:
  96. break
  97. case FunctionsErrorCode.notFound.rawValue:
  98. break
  99. case FunctionsErrorCode.alreadyExists.rawValue:
  100. break
  101. case FunctionsErrorCode.permissionDenied.rawValue:
  102. break
  103. case FunctionsErrorCode.resourceExhausted.rawValue:
  104. break
  105. case FunctionsErrorCode.failedPrecondition.rawValue:
  106. break
  107. case FunctionsErrorCode.aborted.rawValue:
  108. break
  109. case FunctionsErrorCode.outOfRange.rawValue:
  110. break
  111. case FunctionsErrorCode.unimplemented.rawValue:
  112. break
  113. case FunctionsErrorCode.internal.rawValue:
  114. break
  115. case FunctionsErrorCode.unavailable.rawValue:
  116. break
  117. case FunctionsErrorCode.dataLoss.rawValue:
  118. break
  119. case FunctionsErrorCode.unauthenticated.rawValue:
  120. break
  121. default:
  122. break
  123. }
  124. }
  125. }
  126. }
  127. }