diff --git a/FirebaseFunctions/Sources/Functions.swift b/FirebaseFunctions/Sources/Functions.swift index d78d33f4e4d..d23859d4869 100644 --- a/FirebaseFunctions/Sources/Functions.swift +++ b/FirebaseFunctions/Sources/Functions.swift @@ -104,7 +104,7 @@ internal enum FunctionsConstants { return HTTPSCallable(functions: self, name: name) } - @objc(HTTPSCallableWithURL:) open func httpsCallable(url: URL) -> HTTPSCallable { + @objc(HTTPSCallableWithURL:) open func httpsCallable(_ url: URL) -> HTTPSCallable { return HTTPSCallable(functions: self, url: url) } @@ -135,7 +135,7 @@ internal enum FunctionsConstants { /// - Parameter encoder: The encoder instance to use to run the encoding. /// - Parameter decoder: The decoder instance to use to run the decoding. open func httpsCallable(url: URL, + Response: Decodable>(_ url: URL, requestAs: Request.Type = Request.self, responseAs: Response.Type = Response.self, encoder: FirebaseDataEncoder = FirebaseDataEncoder( @@ -143,7 +143,7 @@ internal enum FunctionsConstants { decoder: FirebaseDataDecoder = FirebaseDataDecoder( )) -> Callable { - return Callable(callable: httpsCallable(url: url), encoder: encoder, decoder: decoder) + return Callable(callable: httpsCallable(url), encoder: encoder, decoder: decoder) } /** diff --git a/FirebaseFunctions/Tests/Integration/IntegrationTests.swift b/FirebaseFunctions/Tests/Integration/IntegrationTests.swift index ce0d2581c67..dd6340f2832 100644 --- a/FirebaseFunctions/Tests/Integration/IntegrationTests.swift +++ b/FirebaseFunctions/Tests/Integration/IntegrationTests.swift @@ -90,7 +90,7 @@ class IntegrationTests: XCTestCase { let byName = functions.httpsCallable("dataTest", requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) - let byURL = functions.httpsCallable(url: emulatorURL("dataTest"), + let byURL = functions.httpsCallable(emulatorURL("dataTest"), requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) @@ -129,7 +129,7 @@ class IntegrationTests: XCTestCase { let byName = functions.httpsCallable("dataTest", requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) - let byUrl = functions.httpsCallable(url: emulatorURL("dataTest"), + let byUrl = functions.httpsCallable(emulatorURL("dataTest"), requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) @@ -152,7 +152,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("scalarTest"), + emulatorURL("scalarTest"), requestAs: Int16.self, responseAs: Int.self ) @@ -180,7 +180,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("scalarTest"), + emulatorURL("scalarTest"), requestAs: Int16.self, responseAs: Int.self ) @@ -194,7 +194,7 @@ class IntegrationTests: XCTestCase { @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) func testScalarAsyncAlternateSignature() async throws { let byName: Callable = functions.httpsCallable("scalarTest") - let byURL: Callable = functions.httpsCallable(url: emulatorURL("scalarTest")) + let byURL: Callable = functions.httpsCallable(emulatorURL("scalarTest")) for function in [byName, byURL] { let result = try await function.call(17) XCTAssertEqual(result, 76) @@ -221,7 +221,7 @@ class IntegrationTests: XCTestCase { responseAs: [String: Int].self ) let byURL = functions.httpsCallable( - url: emulatorURL("tokenTest"), + emulatorURL("tokenTest"), requestAs: [String: Int].self, responseAs: [String: Int].self ) @@ -261,7 +261,7 @@ class IntegrationTests: XCTestCase { responseAs: [String: Int].self ) let byURL = functions.httpsCallable( - url: emulatorURL("tokenTest"), + emulatorURL("tokenTest"), requestAs: [String: Int].self, responseAs: [String: Int].self ) @@ -280,7 +280,7 @@ class IntegrationTests: XCTestCase { responseAs: [String: Int].self ) let byURL = functions.httpsCallable( - url: emulatorURL("FCMTokenTest"), + emulatorURL("FCMTokenTest"), requestAs: [String: Int].self, responseAs: [String: Int].self ) @@ -308,7 +308,7 @@ class IntegrationTests: XCTestCase { responseAs: [String: Int].self ) let byURL = functions.httpsCallable( - url: emulatorURL("FCMTokenTest"), + emulatorURL("FCMTokenTest"), requestAs: [String: Int].self, responseAs: [String: Int].self ) @@ -327,7 +327,7 @@ class IntegrationTests: XCTestCase { responseAs: Int?.self ) let byURL = functions.httpsCallable( - url: emulatorURL("nullTest"), + emulatorURL("nullTest"), requestAs: Int?.self, responseAs: Int?.self ) @@ -355,7 +355,7 @@ class IntegrationTests: XCTestCase { responseAs: Int?.self ) let byURL = functions.httpsCallable( - url: emulatorURL("nullTest"), + emulatorURL("nullTest"), requestAs: Int?.self, responseAs: Int?.self ) @@ -374,7 +374,7 @@ class IntegrationTests: XCTestCase { responseAs: Int?.self ) let byURL = functions.httpsCallable( - url: emulatorURL("missingResultTest"), + emulatorURL("missingResultTest"), requestAs: Int?.self, responseAs: Int?.self ) @@ -406,7 +406,7 @@ class IntegrationTests: XCTestCase { responseAs: Int?.self ) let byURL = functions.httpsCallable( - url: emulatorURL("missingResultTest"), + emulatorURL("missingResultTest"), requestAs: Int?.self, responseAs: Int?.self ) @@ -430,7 +430,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("unhandledErrorTest"), + emulatorURL("unhandledErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -486,7 +486,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("unknownErrorTest"), + emulatorURL("unknownErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -517,7 +517,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("unknownErrorTest"), + emulatorURL("unknownErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -574,7 +574,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("explicitErrorTest"), + emulatorURL("explicitErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -600,7 +600,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("httpErrorTest"), + emulatorURL("httpErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -631,7 +631,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("httpErrorTest"), + emulatorURL("httpErrorTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -654,7 +654,7 @@ class IntegrationTests: XCTestCase { responseAs: Int.self ) let byURL = functions.httpsCallable( - url: emulatorURL("timeoutTest"), + emulatorURL("timeoutTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -688,7 +688,7 @@ class IntegrationTests: XCTestCase { ) byName.timeoutInterval = 0.05 var byURL = functions.httpsCallable( - url: emulatorURL("timeoutTest"), + emulatorURL("timeoutTest"), requestAs: [Int].self, responseAs: Int.self ) @@ -719,7 +719,7 @@ class IntegrationTests: XCTestCase { let byName = functions.httpsCallable("dataTest", requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) - let byURL = functions.httpsCallable(url: emulatorURL("dataTest"), + let byURL = functions.httpsCallable(emulatorURL("dataTest"), requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) for function in [byName, byURL] { @@ -758,7 +758,7 @@ class IntegrationTests: XCTestCase { requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) - let byURL = functions.httpsCallable(url: emulatorURL("dataTest"), + let byURL = functions.httpsCallable(emulatorURL("dataTest"), requestAs: DataTestRequest.self, responseAs: DataTestResponse.self) @@ -785,7 +785,7 @@ class IntegrationTests: XCTestCase { ) let byName: Callable = functions.httpsCallable("dataTest") let byURL: Callable = functions - .httpsCallable(url: emulatorURL("dataTest")) + .httpsCallable(emulatorURL("dataTest")) for function in [byName, byURL] { let expectation = expectation(description: #function) @@ -822,7 +822,7 @@ class IntegrationTests: XCTestCase { let byName: Callable = functions .httpsCallable("dataTest") let byURL: Callable = functions - .httpsCallable(url: emulatorURL("dataTest")) + .httpsCallable(emulatorURL("dataTest")) for function in [byName, byURL] { let response = try await function(data) diff --git a/FirebaseFunctions/Tests/Unit/FunctionsAPITests.swift b/FirebaseFunctions/Tests/Unit/FunctionsAPITests.swift index dc98ac4353a..b3af8a2b9a4 100644 --- a/FirebaseFunctions/Tests/Unit/FunctionsAPITests.swift +++ b/FirebaseFunctions/Tests/Unit/FunctionsAPITests.swift @@ -51,19 +51,28 @@ final class FunctionsAPITests: XCTestCase { let callableRef = Functions.functions().httpsCallable("setCourseForAlderaan") callableRef.timeoutInterval = 60 let url = URL(string: "https://localhost:8080/setCourseForAlderaan")! - let callableRefByURL = Functions.functions().httpsCallable(url: url) + let callableRefByURL = Functions.functions().httpsCallable(url) struct Message: Codable { let hello: String let world: String } + struct Response: Codable { + let response: String + } + + let callableCodable = Functions.functions() + .httpsCallable("codable", requestAs: Message.self, responseAs: Response.self) + let callableCodable2 = Functions.functions() + .httpsCallable(url, requestAs: Message.self, responseAs: Response.self) let message = Message(hello: "hello", world: "world") - callableRef.call(message) { result, error in - if let result = result { - _ = result.data - } else if let _ /* error */ = error { - // ... + callableCodable.call(message) { result in + switch result { + case let .success(response): + let _: Response = response + case let .failure(error): + () } }