Skip to content

Commit a283b68

Browse files
authored
fix(functions): invoke with custom http method (#367)
1 parent a188688 commit a283b68

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Sources/Functions/FunctionsClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public actor FunctionsClient {
131131
) async throws -> Response {
132132
var request = Request(
133133
path: functionName,
134-
method: .post,
134+
method: invokeOptions.method?.httpMethod ?? .post,
135135
headers: invokeOptions.headers.merging(headers) { invoke, _ in invoke },
136136
body: invokeOptions.body
137137
)

Sources/Functions/Types.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _Helpers
12
import Foundation
23

34
/// An error type representing various errors that can occur while invoking functions.
@@ -87,6 +88,16 @@ public struct FunctionInvokeOptions: Sendable {
8788
case put = "PUT"
8889
case patch = "PATCH"
8990
case delete = "DELETE"
91+
92+
var httpMethod: Request.Method {
93+
switch self {
94+
case .get: .get
95+
case .post: .post
96+
case .put: .put
97+
case .patch: .patch
98+
case .delete: .delete
99+
}
100+
}
90101
}
91102
}
92103

Tests/FunctionsTests/FunctionsClientTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ final class FunctionsClientTests: XCTestCase {
5757
)
5858
}
5959

60+
func testInvokeWithCustomMethod() async throws {
61+
let url = URL(string: "http://localhost:5432/functions/v1/hello_world")!
62+
let _request = ActorIsolated(URLRequest?.none)
63+
64+
let sut = FunctionsClient(url: self.url, headers: ["Apikey": apiKey]) { request in
65+
await _request.setValue(request)
66+
return (
67+
Data(), HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
68+
)
69+
}
70+
71+
try await sut.invoke("hello_world", options: .init(method: .get))
72+
let request = await _request.value
73+
74+
XCTAssertEqual(request?.httpMethod, "GET")
75+
}
76+
6077
func testInvokeWithRegionDefinedInClient() async {
6178
let sut = FunctionsClient(url: url, region: .caCentral1) {
6279
let region = $0.value(forHTTPHeaderField: "x-region")

0 commit comments

Comments
 (0)