Skip to content
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

Add HTTP methods support #3

Merged
merged 1 commit into from
Aug 10, 2023
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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let package = Package(
.tvOS(.v13),
],
products: [
.library(name: "Functions", targets: ["Functions"]),
.library(name: "Functions", targets: ["Functions"])
],
dependencies: [
.package(url: "https://github.com/kean/Get", from: "2.1.5"),
.package(url: "https://github.com/kean/Get", from: "2.1.5")
],
targets: [
.target(
Expand Down
4 changes: 2 additions & 2 deletions Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final class FunctionsClient {
) async throws -> (Data, HTTPURLResponse) {
let request = Request(
path: functionName,
method: .post,
method: invokeOptions.method.map({ HTTPMethod(rawValue: $0.rawValue) }) ?? .post,
body: invokeOptions.body,
headers: invokeOptions.headers.merging(headers) { first, _ in first }
)
Expand All @@ -87,7 +87,7 @@ public final class FunctionsClient {
throw URLError(.badServerResponse)
}

guard 200 ..< 300 ~= httpResponse.statusCode else {
guard 200..<300 ~= httpResponse.statusCode else {
throw FunctionsError.httpError(code: httpResponse.statusCode, data: response.data)
}

Expand Down
15 changes: 13 additions & 2 deletions Sources/Functions/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public enum FunctionsError: Error, LocalizedError {
}

public struct FunctionInvokeOptions {
let method: Method?
let headers: [String: String]
let body: Data?

public init(headers: [String: String] = [:], body: some Encodable) {
public init(method: Method? = nil, headers: [String: String] = [:], body: some Encodable) {
var headers = headers

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

self.method = method
self.headers = headers
}

public init(headers: [String: String] = [:]) {
public init(method: Method? = nil, headers: [String: String] = [:]) {
self.method = method
self.headers = headers
body = nil
}

public enum Method: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
}
}
3 changes: 2 additions & 1 deletion Tests/FunctionsTests/FunctionInvokeOptionsTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@testable import Functions
import XCTest

@testable import Functions

final class FunctionInvokeOptionsTests: XCTestCase {
func testStringBody() {
let options = FunctionInvokeOptions(body: "string value")
Expand Down