| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //
- // Copyright 2021 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- // MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
- import Foundation
- import FirebaseCore
- import FirebaseFunctions
- final class FunctionsAPITests {
- func usage() {
- // MARK: - Functions
- // Retrieve Functions instance
- _ = Functions.functions()
- if let app = FirebaseApp.app() {
- _ = Functions.functions(app: app)
- _ = Functions.functions(app: app, region: "alderaan")
- _ = Functions.functions(app: app, customDomain: "https://visitalderaan.com")
- }
- _ = Functions.functions(region: "alderaan")
- _ = Functions.functions(customDomain: "https://visitalderaan.com")
- // Reference to a callable HTTPS trigger
- _ = Functions.functions().httpsCallable("setCourseForAlderaan")
- // Functions emulator
- Functions.functions().useEmulator(withHost: "host", port: 3000)
- if let _ /* emulatorOrigin */ = Functions.functions().emulatorOrigin {
- // ...
- }
- // MARK: - HTTPSCallable
- let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan")
- callableRef.timeoutInterval = 60
- let data: Any? = nil
- callableRef.call(data) { result, error in
- if let result = result {
- _ = result.data
- } else if let _ /* error */ = error {
- // ...
- }
- }
- #if compiler(>=5.5) && canImport(_Concurrency)
- if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
- // async/await is a Swift 5.5+ feature available on iOS 15+
- Task {
- do {
- let result = try await callableRef.call(data)
- _ = result.data
- } catch {
- // ...
- }
- }
- }
- #endif // compiler(>=5.5) && canImport(_Concurrency)
- callableRef.call { result, error in
- if let result = result {
- _ = result.data
- } else if let _ /* error */ = error {
- // ...
- }
- }
- #if compiler(>=5.5) && canImport(_Concurrency)
- if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
- // async/await is a Swift 5.5+ feature available on iOS 15+
- Task {
- do {
- let result = try await callableRef.call()
- _ = result.data
- } catch {
- // ...
- }
- }
- }
- #endif // compiler(>=5.5) && canImport(_Concurrency)
- // MARK: - FunctionsErrorCode
- callableRef.call { _, error in
- if let error = error {
- switch (error as NSError).code {
- case FunctionsErrorCode.OK.rawValue:
- break
- case FunctionsErrorCode.cancelled.rawValue:
- break
- case FunctionsErrorCode.unknown.rawValue:
- break
- case FunctionsErrorCode.invalidArgument.rawValue:
- break
- case FunctionsErrorCode.deadlineExceeded.rawValue:
- break
- case FunctionsErrorCode.notFound.rawValue:
- break
- case FunctionsErrorCode.alreadyExists.rawValue:
- break
- case FunctionsErrorCode.permissionDenied.rawValue:
- break
- case FunctionsErrorCode.resourceExhausted.rawValue:
- break
- case FunctionsErrorCode.failedPrecondition.rawValue:
- break
- case FunctionsErrorCode.aborted.rawValue:
- break
- case FunctionsErrorCode.outOfRange.rawValue:
- break
- case FunctionsErrorCode.unimplemented.rawValue:
- break
- case FunctionsErrorCode.internal.rawValue:
- break
- case FunctionsErrorCode.unavailable.rawValue:
- break
- case FunctionsErrorCode.dataLoss.rawValue:
- break
- case FunctionsErrorCode.unauthenticated.rawValue:
- break
- default:
- break
- }
- }
- }
- }
- }
|