Skip to content

Commit

Permalink
fix(functions): invoke with custom http method (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored May 8, 2024
1 parent a188688 commit a283b68
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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

0 comments on commit a283b68

Please sign in to comment.