Skip to content

Commit 98878a6

Browse files
authored
Add HTTP methods support (#3)
1 parent c680cdf commit 98878a6

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ let package = Package(
1212
.tvOS(.v13),
1313
],
1414
products: [
15-
.library(name: "Functions", targets: ["Functions"]),
15+
.library(name: "Functions", targets: ["Functions"])
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/kean/Get", from: "2.1.5"),
18+
.package(url: "https://github.com/kean/Get", from: "2.1.5")
1919
],
2020
targets: [
2121
.target(

Sources/Functions/FunctionsClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public final class FunctionsClient {
7676
) async throws -> (Data, HTTPURLResponse) {
7777
let request = Request(
7878
path: functionName,
79-
method: .post,
79+
method: invokeOptions.method.map({ HTTPMethod(rawValue: $0.rawValue) }) ?? .post,
8080
body: invokeOptions.body,
8181
headers: invokeOptions.headers.merging(headers) { first, _ in first }
8282
)
@@ -87,7 +87,7 @@ public final class FunctionsClient {
8787
throw URLError(.badServerResponse)
8888
}
8989

90-
guard 200 ..< 300 ~= httpResponse.statusCode else {
90+
guard 200..<300 ~= httpResponse.statusCode else {
9191
throw FunctionsError.httpError(code: httpResponse.statusCode, data: response.data)
9292
}
9393

Sources/Functions/Types.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ public enum FunctionsError: Error, LocalizedError {
1313
}
1414

1515
public struct FunctionInvokeOptions {
16+
let method: Method?
1617
let headers: [String: String]
1718
let body: Data?
1819

19-
public init(headers: [String: String] = [:], body: some Encodable) {
20+
public init(method: Method? = nil, headers: [String: String] = [:], body: some Encodable) {
2021
var headers = headers
2122

2223
switch body {
@@ -32,11 +33,21 @@ public struct FunctionInvokeOptions {
3233
self.body = try? JSONEncoder().encode(body)
3334
}
3435

36+
self.method = method
3537
self.headers = headers
3638
}
3739

38-
public init(headers: [String: String] = [:]) {
40+
public init(method: Method? = nil, headers: [String: String] = [:]) {
41+
self.method = method
3942
self.headers = headers
4043
body = nil
4144
}
45+
46+
public enum Method: String {
47+
case get = "GET"
48+
case post = "POST"
49+
case put = "PUT"
50+
case patch = "PATCH"
51+
case delete = "DELETE"
52+
}
4253
}

Tests/FunctionsTests/FunctionInvokeOptionsTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
@testable import Functions
21
import XCTest
32

3+
@testable import Functions
4+
45
final class FunctionInvokeOptionsTests: XCTestCase {
56
func testStringBody() {
67
let options = FunctionInvokeOptions(body: "string value")

0 commit comments

Comments
 (0)