Skip to content

Commit 91dfd35

Browse files
authored
Adopt the new shared HTTP client (#13)
Adopt the new shared HTTP client ### Motivation Now that SwiftNIO/AsyncHTTPClient have a singleton variant of the `EventLoopGroup`, which allows creating an `HTTPClient` without any argument, let's simplify the initializer of the transport to take advantage of it - bringing it in line with the URLSession transport. ### Modifications Default the HTTPClient to a new one with a default event loop group, and remove the mandatory shutdown call. ### Result Adopters can more easily create the AHC transport. ### Test Plan N/A Reviewed by: dnadoba, glbrntt Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #13
1 parent 95bb2f8 commit 91dfd35

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ let package = Package(
3838
),
3939
],
4040
dependencies: [
41-
.package(url: "https://github.com/apple/swift-nio", from: "2.51.0"),
42-
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.17.0"),
41+
.package(url: "https://github.com/apple/swift-nio", from: "2.58.0"),
42+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.19.0"),
4343
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.3")),
4444
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
4545
],

Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift

+9-17
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,9 @@ import protocol Foundation.LocalizedError
3030
///
3131
/// ### Use the AsyncHTTPClient transport
3232
///
33-
/// Create the underlying HTTP client:
33+
/// Instantiate the transport:
3434
///
35-
/// let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
36-
///
37-
/// Either store a reference to the client elsewhere and shut it down during
38-
/// cleanup, or add a defer block if the client is only used in the current
39-
/// scope:
40-
///
41-
/// defer {
42-
/// try! httpClient.syncShutdown()
43-
/// }
44-
///
45-
/// Instantiate the transport and provide the HTTP client to it:
46-
///
47-
/// let transport = AsyncHTTPClientTransport(
48-
/// configuration: .init(client: httpClient)
49-
/// )
35+
/// let transport = AsyncHTTPClientTransport()
5036
///
5137
/// Create the base URL of the server to call using your client. If the server
5238
/// URL was defined in the OpenAPI document, you find a generated method for it
@@ -68,6 +54,12 @@ import protocol Foundation.LocalizedError
6854
///
6955
/// let response = try await client.checkHealth(.init())
7056
/// // ...
57+
///
58+
/// ### Provide a custom Client
59+
///
60+
/// The ``AsyncHTTPClientTransport/Configuration-swift.struct`` type allows you
61+
/// to provide a custom `HTTPClient` and tweak behaviors such as the default
62+
/// timeout.
7163
public struct AsyncHTTPClientTransport: ClientTransport {
7264

7365
/// A set of configuration values for the AsyncHTTPClient transport.
@@ -83,7 +75,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
8375
/// - Parameters:
8476
/// - client: The underlying client used to perform HTTP operations.
8577
/// - timeout: The request timeout, defaults to 1 minute.
86-
public init(client: HTTPClient, timeout: TimeAmount = .minutes(1)) {
78+
public init(client: HTTPClient = .init(), timeout: TimeAmount = .minutes(1)) {
8779
self.client = client
8880
self.timeout = timeout
8981
}

0 commit comments

Comments
 (0)