Skip to content

Commit 694760b

Browse files
peterfriesepaulb777
authored andcommitted
Make HPPS Callable Functions callable like a plain Swift function
Example: let greeter = functions.httpsCallable("greeter", requestType: GreetingRequest.self, responseType: GreetingResponse.self) let result = try await greeter(data) print(result.greeting) Signed-off-by: Peter Friese <[email protected]>
1 parent aea5e89 commit 694760b

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

FirebaseFunctionsSwift/Sources/Codable/Callable+Codable.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ public struct Callable<Request: Encodable, Response: Decodable> {
107107
}
108108
}
109109

110+
/// Creates a directly callable function.
111+
///
112+
/// This allows users to call a HTTPS Callable Funciton like a normal Swift function:
113+
///
114+
/// let greeter = functions.httpsCallable("greeter",
115+
/// requestType: GreetingRequest.self,
116+
/// responseType: GreetingResponse.self)
117+
/// try greeter(data) { result in
118+
/// print(result.greeting)
119+
/// }
120+
///
121+
/// - Parameters:
122+
/// - data: Parameters to pass to the trigger.
123+
/// - completion: The block to call when the HTTPS request has completed.
124+
public func callAsFunction(_ data: Request,
125+
completion: @escaping (Result<Response, Error>)
126+
-> Void) throws {
127+
try call(data, completion: completion)
128+
}
129+
110130
#if compiler(>=5.5) && canImport(_Concurrency)
111131
/**
112132
* Executes this Callable HTTPS trigger asynchronously.
@@ -138,5 +158,23 @@ public struct Callable<Request: Encodable, Response: Decodable> {
138158
let result = try await callable.call(encoded)
139159
return try decoder.decode(Response.self, from: result.data)
140160
}
161+
162+
/// Creates a directly callable function.
163+
///
164+
/// This allows users to call a HTTPS Callable Funciton like a normal Swift function:
165+
///
166+
/// let greeter = functions.httpsCallable("greeter",
167+
/// requestType: GreetingRequest.self,
168+
/// responseType: GreetingResponse.self)
169+
/// let result = try await greeter(data)
170+
/// print(result.greeting)
171+
///
172+
/// - Parameters:
173+
/// - data: Parameters to pass to the trigger.
174+
/// - Returns: The decoded `Response` value
175+
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
176+
public func callAsFunction(_ data: Request) async throws -> Response {
177+
return try await call(data)
178+
}
141179
#endif
142180
}

FirebaseFunctionsSwift/Tests/IntegrationTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,60 @@ class IntegrationTests: XCTestCase {
531531
}
532532
}
533533
#endif
534+
535+
func testCallAsFunction() throws {
536+
let expectation = expectation(description: #function)
537+
let data = DataTestRequest(
538+
bool: true,
539+
int: 2,
540+
long: 9_876_543_210,
541+
string: "four",
542+
array: [5, 6],
543+
null: nil
544+
)
545+
let function = functions.httpsCallable("dataTest",
546+
requestType: DataTestRequest.self,
547+
responseType: DataTestResponse.self)
548+
try function(data) { result in
549+
do {
550+
let response = try result.get()
551+
let expected = DataTestResponse(
552+
message: "stub response",
553+
long: 420,
554+
code: 42
555+
)
556+
XCTAssertEqual(response, expected)
557+
expectation.fulfill()
558+
} catch {
559+
XCTAssert(false, "Failed to unwrap the function result: \(error)")
560+
}
561+
}
562+
waitForExpectations(timeout: 5)
563+
}
564+
565+
#if compiler(>=5.5) && canImport(_Concurrency)
566+
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
567+
func testCallAsFunctionAsync() async throws {
568+
let data = DataTestRequest(
569+
bool: true,
570+
int: 2,
571+
long: 9_876_543_210,
572+
string: "four",
573+
array: [5, 6],
574+
null: nil
575+
)
576+
577+
let function = functions.httpsCallable("dataTest",
578+
requestType: DataTestRequest.self,
579+
responseType: DataTestResponse.self)
580+
581+
let response = try await function(data)
582+
let expected = DataTestResponse(
583+
message: "stub response",
584+
long: 420,
585+
code: 42
586+
)
587+
XCTAssertEqual(response, expected)
588+
}
589+
#endif
534590
}

0 commit comments

Comments
 (0)