Skip to content

Commit 010e498

Browse files
authored
Create the default RequestOptions from the client instead of using a required init (#97)
Related discussion in #71
1 parent 55211b6 commit 010e498

7 files changed

Lines changed: 33 additions & 13 deletions

File tree

Sources/HTTPAPIs/Client/HTTPClient+Conveniences.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ extension HTTPClient where Self: ~Copyable {
4040
public func perform<Return: ~Copyable>(
4141
request: HTTPRequest,
4242
body: consuming HTTPClientRequestBody<RequestWriter>? = nil,
43-
options: RequestOptions = .init(),
43+
options: RequestOptions? = nil,
4444
responseHandler: (HTTPResponse, consuming ResponseConcludingReader) async throws -> Return,
4545
) async throws -> Return {
46+
let options = options ?? self.defaultRequestOptions
4647
return try await self.perform(request: request, body: body, options: options, responseHandler: responseHandler)
4748
}
4849

@@ -63,10 +64,11 @@ extension HTTPClient where Self: ~Copyable {
6364
public func get(
6465
url: URL,
6566
headerFields: HTTPFields = [:],
66-
options: RequestOptions = .init(),
67+
options: RequestOptions? = nil,
6768
collectUpTo limit: Int,
6869
) async throws -> (response: HTTPResponse, bodyData: Data) {
6970
let request = HTTPRequest(url: url, headerFields: headerFields)
71+
let options = options ?? self.defaultRequestOptions
7072
return try await self.perform(request: request, body: nil, options: options) { response, body in
7173
(
7274
response,
@@ -94,10 +96,11 @@ extension HTTPClient where Self: ~Copyable {
9496
url: URL,
9597
headerFields: HTTPFields = [:],
9698
bodyData: Data,
97-
options: RequestOptions = .init(),
99+
options: RequestOptions? = nil,
98100
collectUpTo limit: Int,
99101
) async throws -> (response: HTTPResponse, bodyData: Data) {
100102
let request = HTTPRequest(method: .post, url: url, headerFields: headerFields)
103+
let options = options ?? self.defaultRequestOptions
101104
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
102105
(
103106
response,
@@ -125,10 +128,11 @@ extension HTTPClient where Self: ~Copyable {
125128
url: URL,
126129
headerFields: HTTPFields = [:],
127130
bodyData: Data,
128-
options: RequestOptions = .init(),
131+
options: RequestOptions? = nil,
129132
collectUpTo limit: Int,
130133
) async throws -> (response: HTTPResponse, bodyData: Data) {
131134
let request = HTTPRequest(method: .put, url: url, headerFields: headerFields)
135+
let options = options ?? self.defaultRequestOptions
132136
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
133137
(
134138
response,
@@ -156,10 +160,11 @@ extension HTTPClient where Self: ~Copyable {
156160
url: URL,
157161
headerFields: HTTPFields = [:],
158162
bodyData: Data? = nil,
159-
options: RequestOptions = .init(),
163+
options: RequestOptions? = nil,
160164
collectUpTo limit: Int,
161165
) async throws -> (response: HTTPResponse, bodyData: Data) {
162166
let request = HTTPRequest(method: .delete, url: url, headerFields: headerFields)
167+
let options = options ?? self.defaultRequestOptions
163168
return try await self.perform(request: request, body: bodyData.map { .data($0) }, options: options) { response, body in
164169
(
165170
response,
@@ -187,10 +192,11 @@ extension HTTPClient where Self: ~Copyable {
187192
url: URL,
188193
headerFields: HTTPFields = [:],
189194
bodyData: Data,
190-
options: RequestOptions = .init(),
195+
options: RequestOptions? = nil,
191196
collectUpTo limit: Int,
192197
) async throws -> (response: HTTPResponse, bodyData: Data) {
193198
let request = HTTPRequest(method: .patch, url: url, headerFields: headerFields)
199+
let options = options ?? self.defaultRequestOptions
194200
return try await self.perform(request: request, body: .data(bodyData), options: options) { response, body in
195201
(
196202
response,

Sources/HTTPAPIs/Client/HTTPClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public protocol HTTPClient<RequestOptions>: Sendable, ~Copyable {
3535
associatedtype ResponseConcludingReader: ConcludingAsyncReader, ~Copyable, SendableMetatype
3636
where ResponseConcludingReader.Underlying.ReadElement == UInt8, ResponseConcludingReader.FinalElement == HTTPFields?
3737

38+
/// The default request options for `perform`.
39+
var defaultRequestOptions: RequestOptions { get }
40+
3841
/// Performs an HTTP request and processes the response.
3942
///
4043
/// This method executes the HTTP request with the specified options, then invokes

Sources/HTTPAPIs/Client/HTTPClientCapability+RequestOptions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ public enum HTTPClientCapability {
2020
/// Additional options supported by a subset of clients are defined in child
2121
/// protocols to allow libraries to depend on a specific capabilities.
2222
public protocol RequestOptions {
23-
init()
2423
}
2524
}

Sources/HTTPClient/DefaultHTTPClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public struct DefaultHTTPClient: HTTPClient, ~Copyable {
159159
fatalError()
160160
#endif
161161
}
162+
163+
public var defaultRequestOptions: HTTPRequestOptions {
164+
.init()
165+
}
162166
}
163167

164168
#endif

Sources/HTTPClient/HTTP+Conveniences.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension HTTP {
4141
public static func perform<Client: HTTPClient & ~Copyable, Return: ~Copyable>(
4242
request: HTTPRequest,
4343
body: consuming HTTPClientRequestBody<Client.RequestWriter>? = nil,
44-
options: Client.RequestOptions = .init(),
44+
options: Client.RequestOptions? = nil,
4545
on client: borrowing Client = DefaultHTTPClient.shared,
4646
responseHandler: (HTTPResponse, consuming Client.ResponseConcludingReader) async throws -> Return,
4747
) async throws -> Return {
@@ -67,7 +67,7 @@ extension HTTP {
6767
public static func get<Client: HTTPClient & ~Copyable>(
6868
url: URL,
6969
headerFields: HTTPFields = [:],
70-
options: Client.RequestOptions = .init(),
70+
options: Client.RequestOptions? = nil,
7171
on client: borrowing Client = DefaultHTTPClient.shared,
7272
collectUpTo limit: Int,
7373
) async throws -> (response: HTTPResponse, bodyData: Data) {
@@ -95,7 +95,7 @@ extension HTTP {
9595
url: URL,
9696
headerFields: HTTPFields = [:],
9797
bodyData: Data,
98-
options: Client.RequestOptions = .init(),
98+
options: Client.RequestOptions? = nil,
9999
on client: borrowing Client = DefaultHTTPClient.shared,
100100
collectUpTo limit: Int,
101101
) async throws -> (response: HTTPResponse, bodyData: Data) {
@@ -123,7 +123,7 @@ extension HTTP {
123123
url: URL,
124124
headerFields: HTTPFields = [:],
125125
bodyData: Data,
126-
options: Client.RequestOptions = .init(),
126+
options: Client.RequestOptions? = nil,
127127
on client: borrowing Client = DefaultHTTPClient.shared,
128128
collectUpTo limit: Int,
129129
) async throws -> (response: HTTPResponse, bodyData: Data) {
@@ -151,7 +151,7 @@ extension HTTP {
151151
url: URL,
152152
headerFields: HTTPFields = [:],
153153
bodyData: Data? = nil,
154-
options: Client.RequestOptions = .init(),
154+
options: Client.RequestOptions? = nil,
155155
on client: borrowing Client = DefaultHTTPClient.shared,
156156
collectUpTo limit: Int,
157157
) async throws -> (response: HTTPResponse, bodyData: Data) {
@@ -179,7 +179,7 @@ extension HTTP {
179179
url: URL,
180180
headerFields: HTTPFields = [:],
181181
bodyData: Data,
182-
options: Client.RequestOptions = .init(),
182+
options: Client.RequestOptions? = nil,
183183
on client: borrowing Client = DefaultHTTPClient.shared,
184184
collectUpTo limit: Int,
185185
) async throws -> (response: HTTPResponse, bodyData: Data) {

Sources/HTTPClient/URLSession/URLSessionHTTPClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,9 @@ final class URLSessionHTTPClient: HTTPClient, IdleTimerEntryProvider {
290290
}
291291
return try result!.get()
292292
}
293+
294+
var defaultRequestOptions: HTTPRequestOptions {
295+
.init()
296+
}
293297
}
294298
#endif

Tests/HTTPAPIsTests/Helpers/HTTPClientAndServerTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ final class TestClientAndServer: HTTPClient, HTTPServer {
160160
)
161161
}
162162

163+
var defaultRequestOptions: RequestOptions {
164+
.init()
165+
}
166+
163167
func serve(
164168
handler: some HTTPServerRequestHandler<AsyncChannelConcludingAsyncReader, AsyncChannelConcludingAsyncWriter>
165169
) async throws {

0 commit comments

Comments
 (0)