diff --git a/Package.swift b/Package.swift index 8227e7b..a44a659 100644 --- a/Package.swift +++ b/Package.swift @@ -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( diff --git a/Sources/Functions/FunctionsClient.swift b/Sources/Functions/FunctionsClient.swift index 28b3dbd..ca692b5 100644 --- a/Sources/Functions/FunctionsClient.swift +++ b/Sources/Functions/FunctionsClient.swift @@ -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 } ) @@ -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) } diff --git a/Sources/Functions/Types.swift b/Sources/Functions/Types.swift index 1f71255..6770a87 100644 --- a/Sources/Functions/Types.swift +++ b/Sources/Functions/Types.swift @@ -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 { @@ -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" + } } diff --git a/Tests/FunctionsTests/FunctionInvokeOptionsTests.swift b/Tests/FunctionsTests/FunctionInvokeOptionsTests.swift index fb957ae..ef5ae21 100644 --- a/Tests/FunctionsTests/FunctionInvokeOptionsTests.swift +++ b/Tests/FunctionsTests/FunctionInvokeOptionsTests.swift @@ -1,6 +1,7 @@ -@testable import Functions import XCTest +@testable import Functions + final class FunctionInvokeOptionsTests: XCTestCase { func testStringBody() { let options = FunctionInvokeOptions(body: "string value")