FunctionsAPITests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 FirebaseCore
  17. import FirebaseFunctions
  18. final class FunctionsAPITests {
  19. func usage() {
  20. // MARK: - Functions
  21. // Retrieve Functions instance
  22. _ = Functions.functions()
  23. if let app = FirebaseApp.app() {
  24. _ = Functions.functions(app: app)
  25. _ = Functions.functions(app: app, region: "alderaan")
  26. _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
  27. }
  28. _ = Functions.functions(region: "alderaan")
  29. _ = Functions.functions(customDomain: "https://visitalderaan.com")
  30. // Reference to a callable HTTPS trigger
  31. _ = Functions.functions().httpsCallable("setCourseForAlderaan")
  32. // Functions emulator
  33. Functions.functions().useEmulator(withHost: "host", port: 3000)
  34. if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
  35. // ...
  36. }
  37. // MARK: - HTTPSCallable
  38. let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
  39. callableRef.timeoutInterval = 60
  40. let data: Any? = nil
  41. callableRef.call(data) { result, error in
  42. if let result = result {
  43. _ = result.data
  44. } else if let _ /* error */ = error {
  45. // ...
  46. }
  47. }
  48. #if compiler(>=5.5.2) && canImport(_Concurrency)
  49. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  50. // async/await is a Swift 5.5+ feature available on iOS 15+
  51. Task {
  52. do {
  53. let result = try await callableRef.call(data)
  54. _ = result.data
  55. } catch {
  56. // ...
  57. }
  58. }
  59. }
  60. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  61. callableRef.call { result, error in
  62. if let result = result {
  63. _ = result.data
  64. } else if let _ /* error */ = error {
  65. // ...
  66. }
  67. }
  68. #if compiler(>=5.5.2) && canImport(_Concurrency)
  69. if #available(iOS 13.0, macOS 11.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
  70. // async/await is a Swift 5.5+ feature available on iOS 15+
  71. Task {
  72. do {
  73. let result = try await callableRef.call()
  74. _ = result.data
  75. } catch {
  76. // ...
  77. }
  78. }
  79. }
  80. #endif // compiler(>=5.5.2) && canImport(_Concurrency)
  81. // MARK: - FunctionsErrorCode
  82. callableRef.call { _, error in
  83. if let error = error {
  84. switch (error as NSError).code {
  85. case FunctionsErrorCode.OK.rawValue:
  86. break
  87. case FunctionsErrorCode.cancelled.rawValue:
  88. break
  89. case FunctionsErrorCode.unknown.rawValue:
  90. break
  91. case FunctionsErrorCode.invalidArgument.rawValue:
  92. break
  93. case FunctionsErrorCode.deadlineExceeded.rawValue:
  94. break
  95. case FunctionsErrorCode.notFound.rawValue:
  96. break
  97. case FunctionsErrorCode.alreadyExists.rawValue:
  98. break
  99. case FunctionsErrorCode.permissionDenied.rawValue:
  100. break
  101. case FunctionsErrorCode.resourceExhausted.rawValue:
  102. break
  103. case FunctionsErrorCode.failedPrecondition.rawValue:
  104. break
  105. case FunctionsErrorCode.aborted.rawValue:
  106. break
  107. case FunctionsErrorCode.outOfRange.rawValue:
  108. break
  109. case FunctionsErrorCode.unimplemented.rawValue:
  110. break
  111. case FunctionsErrorCode.internal.rawValue:
  112. break
  113. case FunctionsErrorCode.unavailable.rawValue:
  114. break
  115. case FunctionsErrorCode.dataLoss.rawValue:
  116. break
  117. case FunctionsErrorCode.unauthenticated.rawValue:
  118. break
  119. default:
  120. break
  121. }
  122. }
  123. }
  124. }
  125. }