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 convenience methods for sending Client requests with a Content body #2770

Merged
merged 2 commits into from
Jan 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/Vapor/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ extension Client {
public func delete(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) -> EventLoopFuture<ClientResponse> {
return self.send(.DELETE, headers: headers, to: url, beforeSend: beforeSend)
}

public func post<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
return self.post(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func patch<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
return self.patch(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func put<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) -> EventLoopFuture<ClientResponse> where T: Content {
return self.put(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func send(
_ method: HTTPMethod,
Expand Down
12 changes: 12 additions & 0 deletions Sources/Vapor/Concurrency/Client+Concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ extension Client {
public func delete(_ url: URI, headers: HTTPHeaders = [:], beforeSend: (inout ClientRequest) throws -> () = { _ in }) async throws -> ClientResponse {
return try await self.send(.DELETE, headers: headers, to: url, beforeSend: beforeSend).get()
}

public func post<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) async throws -> ClientResponse where T: Content {
return try await self.post(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func patch<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) async throws -> ClientResponse where T: Content {
return try await self.patch(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func put<T>(_ url: URI, headers: HTTPHeaders = [:], content: T) async throws -> ClientResponse where T: Content {
return try await self.put(url, headers: headers, beforeSend: { try $0.content.encode(content) })
}

public func send(
_ method: HTTPMethod,
Expand Down
16 changes: 16 additions & 0 deletions Tests/VaporTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ final class ClientTests: XCTestCase {
XCTAssertEqual(data.headers["Content-Type"], "application/json; charset=utf-8")
}

func testClientContent() throws {
let app = Application()
defer { app.shutdown() }
try app.boot()

let res = try app.client.post("http://httpbin.org/anything", content: ["hello": "world"]).wait()

struct HTTPBinAnything: Codable {
var headers: [String: String]
var json: [String: String]
}
let data = try res.content.decode(HTTPBinAnything.self)
XCTAssertEqual(data.json, ["hello": "world"])
XCTAssertEqual(data.headers["Content-Type"], "application/json; charset=utf-8")
}

func testBoilerplateClient() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down