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