Skip to content

Commit 995c933

Browse files
authored
Refactored Duplicate Methods in Functions (#13771)
1 parent 64aedfc commit 995c933

File tree

4 files changed

+54
-79
lines changed

4 files changed

+54
-79
lines changed

FirebaseFunctions/Sources/Functions.swift

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ enum FunctionsConstants {
135135
/// - Parameter name: The name of the Callable HTTPS trigger.
136136
/// - Returns: A reference to a Callable HTTPS trigger.
137137
@objc(HTTPSCallableWithName:) open func httpsCallable(_ name: String) -> HTTPSCallable {
138-
return HTTPSCallable(functions: self, name: name)
138+
HTTPSCallable(functions: self, url: functionURL(for: name)!)
139139
}
140140

141141
/// Creates a reference to the Callable HTTPS trigger with the given name and configuration
@@ -147,7 +147,7 @@ enum FunctionsConstants {
147147
@objc(HTTPSCallableWithName:options:) public func httpsCallable(_ name: String,
148148
options: HTTPSCallableOptions)
149149
-> HTTPSCallable {
150-
return HTTPSCallable(functions: self, name: name, options: options)
150+
HTTPSCallable(functions: self, url: functionURL(for: name)!, options: options)
151151
}
152152

153153
/// Creates a reference to the Callable HTTPS trigger with the given name.
@@ -369,46 +369,23 @@ enum FunctionsConstants {
369369
appCheck: appCheck)
370370
}
371371

372-
func urlWithName(_ name: String) -> String {
372+
func functionURL(for name: String) -> URL? {
373373
assert(!name.isEmpty, "Name cannot be empty")
374374

375375
// Check if we're using the emulator
376376
if let emulatorOrigin {
377-
return "\(emulatorOrigin)/\(projectID)/\(region)/\(name)"
377+
return URL(string: "\(emulatorOrigin)/\(projectID)/\(region)/\(name)")
378378
}
379379

380380
// Check the custom domain.
381381
if let customDomain {
382-
return "\(customDomain)/\(name)"
382+
return URL(string: "\(customDomain)/\(name)")
383383
}
384384

385-
return "https://\(region)-\(projectID).cloudfunctions.net/\(name)"
385+
return URL(string: "https://\(region)-\(projectID).cloudfunctions.net/\(name)")
386386
}
387387

388-
func callFunction(name: String,
389-
withObject data: Any?,
390-
options: HTTPSCallableOptions?,
391-
timeout: TimeInterval,
392-
completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
393-
// Get context first.
394-
contextProvider.getContext(options: options) { context, error in
395-
// Note: context is always non-nil since some checks could succeed, we're only failing if
396-
// there's an error.
397-
if let error {
398-
completion(.failure(error))
399-
} else {
400-
let url = self.urlWithName(name)
401-
self.callFunction(url: URL(string: url)!,
402-
withObject: data,
403-
options: options,
404-
timeout: timeout,
405-
context: context,
406-
completion: completion)
407-
}
408-
}
409-
}
410-
411-
func callFunction(url: URL,
388+
func callFunction(at url: URL,
412389
withObject data: Any?,
413390
options: HTTPSCallableOptions?,
414391
timeout: TimeInterval,

FirebaseFunctions/Sources/HTTPSCallable.swift

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ open class HTTPSCallable: NSObject {
3939
// The functions client to use for making calls.
4040
private let functions: Functions
4141

42-
private enum EndpointType {
43-
case name(String)
44-
case url(URL)
45-
}
46-
47-
private let endpoint: EndpointType
42+
private let url: URL
4843

4944
private let options: HTTPSCallableOptions?
5045

@@ -53,16 +48,10 @@ open class HTTPSCallable: NSObject {
5348
/// The timeout to use when calling the function. Defaults to 70 seconds.
5449
@objc open var timeoutInterval: TimeInterval = 70
5550

56-
init(functions: Functions, name: String, options: HTTPSCallableOptions? = nil) {
57-
self.functions = functions
58-
self.options = options
59-
endpoint = .name(name)
60-
}
61-
6251
init(functions: Functions, url: URL, options: HTTPSCallableOptions? = nil) {
6352
self.functions = functions
53+
self.url = url
6454
self.options = options
65-
endpoint = .url(url)
6655
}
6756

6857
/// Executes this Callable HTTPS trigger asynchronously.
@@ -98,20 +87,13 @@ open class HTTPSCallable: NSObject {
9887
}
9988
}
10089

101-
switch endpoint {
102-
case let .name(name):
103-
functions.callFunction(name: name,
104-
withObject: data,
105-
options: options,
106-
timeout: timeoutInterval,
107-
completion: callback)
108-
case let .url(url):
109-
functions.callFunction(url: url,
110-
withObject: data,
111-
options: options,
112-
timeout: timeoutInterval,
113-
completion: callback)
114-
}
90+
functions.callFunction(
91+
at: url,
92+
withObject: data,
93+
options: options,
94+
timeout: timeoutInterval,
95+
completion: callback
96+
)
11597
}
11698

11799
/// Executes this Callable HTTPS trigger asynchronously. This API should only be used from

FirebaseFunctions/Tests/CombineUnit/HTTPSCallableTests.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ private let expectationTimeout: TimeInterval = 2
3030

3131
class MockFunctions: Functions {
3232
let mockCallFunction: () throws -> HTTPSCallableResult
33-
var verifyParameters: ((_ name: String, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
34-
override func callFunction(name: String,
33+
var verifyParameters: ((_ url: URL, _ data: Any?, _ timeout: TimeInterval) throws -> Void)?
34+
override func callFunction(at url: URL,
3535
withObject data: Any?,
3636
options: HTTPSCallableOptions?,
3737
timeout: TimeInterval,
38-
completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
38+
completion: @escaping (
39+
(Result<HTTPSCallableResult, any Error>) -> Void
40+
)) {
3941
do {
40-
try verifyParameters?(name, data, timeout)
42+
try verifyParameters?(url, data, timeout)
4143
let result = try mockCallFunction()
4244
completion(.success(result))
4345
} catch {
@@ -49,7 +51,7 @@ class MockFunctions: Functions {
4951
self.mockCallFunction = mockCallFunction
5052
super.init(
5153
projectID: "dummy-project",
52-
region: "",
54+
region: "test-region",
5355
customDomain: nil,
5456
auth: nil,
5557
messaging: nil,
@@ -122,8 +124,11 @@ class HTTPSCallableTests: XCTestCase {
122124
httpsFunctionWasCalledExpectation.fulfill()
123125
return HTTPSCallableResultFake(data: expectedResult)
124126
}
125-
functions.verifyParameters = { name, data, timeout in
126-
XCTAssertEqual(name as String, "dummyFunction")
127+
functions.verifyParameters = { url, data, timeout in
128+
XCTAssertEqual(
129+
url.absoluteString,
130+
"https://test-region-dummy-project.cloudfunctions.net/dummyFunction"
131+
)
127132
XCTAssertEqual(data as? String, inputParameter)
128133
XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
129134
}
@@ -169,8 +174,11 @@ class HTTPSCallableTests: XCTestCase {
169174
code: FunctionsErrorCode.internal.rawValue,
170175
userInfo: [NSLocalizedDescriptionKey: "Response is missing data field."])
171176
}
172-
functions.verifyParameters = { name, data, timeout in
173-
XCTAssertEqual(name as String, "dummyFunction")
177+
functions.verifyParameters = { url, data, timeout in
178+
XCTAssertEqual(
179+
url.absoluteString,
180+
"https://test-region-dummy-project.cloudfunctions.net/dummyFunction"
181+
)
174182
XCTAssertEqual(data as? String, inputParameter)
175183
XCTAssertEqual(timeout as TimeInterval, timeoutInterval)
176184
}

FirebaseFunctions/Tests/Unit/FunctionsTests.swift

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,34 @@ class FunctionsTests: XCTestCase {
7979
XCTAssertEqual(functions1, functions2)
8080
}
8181

82-
func testURLWithName() throws {
83-
let url = try XCTUnwrap(functions?.urlWithName("my-endpoint"))
84-
XCTAssertEqual(url, "https://my-region-my-project.cloudfunctions.net/my-endpoint")
82+
func testFunctionURLForName() throws {
83+
XCTAssertEqual(
84+
functions?.functionURL(for: "my-endpoint")?.absoluteString,
85+
"https://my-region-my-project.cloudfunctions.net/my-endpoint"
86+
)
8587
}
8688

87-
func testRegionWithEmulator() throws {
89+
func testFunctionURLForNameEmulator() throws {
8890
functionsCustomDomain?.useEmulator(withHost: "localhost", port: 5005)
89-
let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
90-
XCTAssertEqual(url, "http://localhost:5005/my-project/my-region/my-endpoint")
91+
XCTAssertEqual(
92+
functionsCustomDomain?.functionURL(for: "my-endpoint")?.absoluteString,
93+
"http://localhost:5005/my-project/my-region/my-endpoint"
94+
)
9195
}
9296

93-
func testRegionWithEmulatorWithScheme() throws {
97+
func testFunctionURLForNameRegionWithEmulatorWithScheme() throws {
9498
functionsCustomDomain?.useEmulator(withHost: "http://localhost", port: 5005)
95-
let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
96-
XCTAssertEqual(url, "http://localhost:5005/my-project/my-region/my-endpoint")
99+
XCTAssertEqual(
100+
functionsCustomDomain?.functionURL(for: "my-endpoint")?.absoluteString,
101+
"http://localhost:5005/my-project/my-region/my-endpoint"
102+
)
97103
}
98104

99-
func testCustomDomain() throws {
100-
let url = try XCTUnwrap(functionsCustomDomain?.urlWithName("my-endpoint"))
101-
XCTAssertEqual(url, "https://mydomain.com/my-endpoint")
105+
func testFunctionURLForNameCustomDomain() throws {
106+
XCTAssertEqual(
107+
functionsCustomDomain?.functionURL(for: "my-endpoint")?.absoluteString,
108+
"https://mydomain.com/my-endpoint"
109+
)
102110
}
103111

104112
func testSetEmulatorSettings() throws {
@@ -303,7 +311,7 @@ class FunctionsTests: XCTestCase {
303311

304312
let completionExpectation = expectation(description: "completionExpectation")
305313
functionsCustomDomain?.callFunction(
306-
name: "fake_func",
314+
at: URL(string: "https://example.com/fake_func")!,
307315
withObject: nil,
308316
options: nil,
309317
timeout: 10

0 commit comments

Comments
 (0)