Skip to content
Open
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
18 changes: 12 additions & 6 deletions Sources/HTTPAPIs/Client/HTTPClient+Conveniences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ extension HTTPClient where Self: ~Copyable {
public func perform<Return: ~Copyable>(
request: HTTPRequest,
body: consuming HTTPClientRequestBody<RequestWriter>? = nil,
options: RequestOptions = .init(),
options: RequestOptions? = nil,
responseHandler: (HTTPResponse, consuming ResponseConcludingReader) async throws -> Return,
) async throws -> Return {
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: body, options: options, responseHandler: responseHandler)
}

Expand All @@ -63,10 +64,11 @@ extension HTTPClient where Self: ~Copyable {
public func get(
url: URL,
headerFields: HTTPFields = [:],
options: RequestOptions = .init(),
options: RequestOptions? = nil,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
let request = HTTPRequest(url: url, headerFields: headerFields)
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: nil, options: options) { response, body in
(
response,
Expand Down Expand Up @@ -94,10 +96,11 @@ extension HTTPClient where Self: ~Copyable {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: RequestOptions = .init(),
options: RequestOptions? = nil,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
let request = HTTPRequest(method: .post, url: url, headerFields: headerFields)
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
(
response,
Expand Down Expand Up @@ -125,10 +128,11 @@ extension HTTPClient where Self: ~Copyable {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: RequestOptions = .init(),
options: RequestOptions? = nil,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
let request = HTTPRequest(method: .put, url: url, headerFields: headerFields)
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
(
response,
Expand Down Expand Up @@ -156,10 +160,11 @@ extension HTTPClient where Self: ~Copyable {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data? = nil,
options: RequestOptions = .init(),
options: RequestOptions? = nil,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
let request = HTTPRequest(method: .delete, url: url, headerFields: headerFields)
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: bodyData.map { .data($0) }, options: options) { response, body in
(
response,
Expand Down Expand Up @@ -187,10 +192,11 @@ extension HTTPClient where Self: ~Copyable {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: RequestOptions = .init(),
options: RequestOptions? = nil,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
let request = HTTPRequest(method: .patch, url: url, headerFields: headerFields)
let options = options ?? self.defaultRequestOptions
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
(
response,
Expand Down
3 changes: 3 additions & 0 deletions Sources/HTTPAPIs/Client/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public protocol HTTPClient<RequestOptions>: Sendable, ~Copyable {
associatedtype ResponseConcludingReader: ConcludingAsyncReader, ~Copyable, SendableMetatype
where ResponseConcludingReader.Underlying.ReadElement == UInt8, ResponseConcludingReader.FinalElement == HTTPFields?

/// The default request options for `perform`.
var defaultRequestOptions: RequestOptions { get }

/// Performs an HTTP request and processes the response.
///
/// This method executes the HTTP request with the specified options, then invokes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public enum HTTPClientCapability {
/// Additional options supported by a subset of clients are defined in child
/// protocols to allow libraries to depend on a specific capabilities.
public protocol RequestOptions {
init()
}
}
4 changes: 4 additions & 0 deletions Sources/HTTPClient/DefaultHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public struct DefaultHTTPClient: HTTPClient, ~Copyable {
fatalError()
#endif
}

public var defaultRequestOptions: HTTPRequestOptions {
.init()
}
}

#endif
12 changes: 6 additions & 6 deletions Sources/HTTPClient/HTTP+Conveniences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension HTTP {
public static func perform<Client: HTTPClient & ~Copyable, Return: ~Copyable>(
request: HTTPRequest,
body: consuming HTTPClientRequestBody<Client.RequestWriter>? = nil,
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
responseHandler: (HTTPResponse, consuming Client.ResponseConcludingReader) async throws -> Return,
) async throws -> Return {
Expand All @@ -67,7 +67,7 @@ extension HTTP {
public static func get<Client: HTTPClient & ~Copyable>(
url: URL,
headerFields: HTTPFields = [:],
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
Expand Down Expand Up @@ -95,7 +95,7 @@ extension HTTP {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
Expand Down Expand Up @@ -123,7 +123,7 @@ extension HTTP {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
Expand Down Expand Up @@ -151,7 +151,7 @@ extension HTTP {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data? = nil,
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
Expand Down Expand Up @@ -179,7 +179,7 @@ extension HTTP {
url: URL,
headerFields: HTTPFields = [:],
bodyData: Data,
options: Client.RequestOptions = .init(),
options: Client.RequestOptions? = nil,
on client: borrowing Client = DefaultHTTPClient.shared,
collectUpTo limit: Int,
) async throws -> (response: HTTPResponse, bodyData: Data) {
Expand Down
4 changes: 4 additions & 0 deletions Sources/HTTPClient/URLSession/URLSessionHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,9 @@ final class URLSessionHTTPClient: HTTPClient, IdleTimerEntryProvider {
}
return try result!.get()
}

var defaultRequestOptions: HTTPRequestOptions {
.init()
}
}
#endif
4 changes: 2 additions & 2 deletions Sources/HTTPClientConformance/HTTPClientConformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ where Client.RequestOptions: HTTPClientCapability.RedirectionHandler {
path: "/308"
)

var options = Client.RequestOptions()
var options = client.defaultRequestOptions
options.redirectionHandlerClosure = { response, newRequest in
#expect(response.status == .permanentRedirect)
return .follow(newRequest)
Expand Down Expand Up @@ -311,7 +311,7 @@ where Client.RequestOptions: HTTPClientCapability.RedirectionHandler {
path: "/301"
)

var options = Client.RequestOptions()
var options = client.defaultRequestOptions
options.redirectionHandlerClosure = { response, newRequest in
#expect(response.status == .movedPermanently)
return .follow(newRequest)
Expand Down
4 changes: 4 additions & 0 deletions Tests/HTTPAPIsTests/Helpers/HTTPClientAndServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ final class TestClientAndServer: HTTPClient, HTTPServer {
)
}

var defaultRequestOptions: RequestOptions {
.init()
}

func serve(
handler: some HTTPServerRequestHandler<AsyncChannelConcludingAsyncReader, AsyncChannelConcludingAsyncWriter>
) async throws {
Expand Down
Loading