Skip to content

fix(functions): invoke with custom http method #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public actor FunctionsClient {
) async throws -> Response {
var request = Request(
path: functionName,
method: .post,
method: invokeOptions.method?.httpMethod ?? .post,
headers: invokeOptions.headers.merging(headers) { invoke, _ in invoke },
body: invokeOptions.body
)
Expand Down
11 changes: 11 additions & 0 deletions Sources/Functions/Types.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _Helpers
import Foundation

/// An error type representing various errors that can occur while invoking functions.
Expand Down Expand Up @@ -87,6 +88,16 @@ public struct FunctionInvokeOptions: Sendable {
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"

var httpMethod: Request.Method {
switch self {
case .get: .get
case .post: .post
case .put: .put
case .patch: .patch
case .delete: .delete
}
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions Tests/FunctionsTests/FunctionsClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ final class FunctionsClientTests: XCTestCase {
)
}

func testInvokeWithCustomMethod() async throws {
let url = URL(string: "http://localhost:5432/functions/v1/hello_world")!
let _request = ActorIsolated(URLRequest?.none)

let sut = FunctionsClient(url: self.url, headers: ["Apikey": apiKey]) { request in
await _request.setValue(request)
return (
Data(), HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
)
}

try await sut.invoke("hello_world", options: .init(method: .get))
let request = await _request.value

XCTAssertEqual(request?.httpMethod, "GET")
}

func testInvokeWithRegionDefinedInClient() async {
let sut = FunctionsClient(url: url, region: .caCentral1) {
let region = $0.value(forHTTPHeaderField: "x-region")
Expand Down