Skip to content

Commit

Permalink
Adopt the new shared HTTP client (#13)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
czechboy0 authored Aug 29, 2023
1 parent 95bb2f8 commit 91dfd35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio", from: "2.51.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.17.0"),
.package(url: "https://github.com/apple/swift-nio", from: "2.58.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.3")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
Expand Down
26 changes: 9 additions & 17 deletions Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,9 @@ import protocol Foundation.LocalizedError
///
/// ### Use the AsyncHTTPClient transport
///
/// Create the underlying HTTP client:
/// Instantiate the transport:
///
/// let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
///
/// Either store a reference to the client elsewhere and shut it down during
/// cleanup, or add a defer block if the client is only used in the current
/// scope:
///
/// defer {
/// try! httpClient.syncShutdown()
/// }
///
/// Instantiate the transport and provide the HTTP client to it:
///
/// let transport = AsyncHTTPClientTransport(
/// configuration: .init(client: httpClient)
/// )
/// let transport = AsyncHTTPClientTransport()
///
/// Create the base URL of the server to call using your client. If the server
/// URL was defined in the OpenAPI document, you find a generated method for it
Expand All @@ -68,6 +54,12 @@ import protocol Foundation.LocalizedError
///
/// let response = try await client.checkHealth(.init())
/// // ...
///
/// ### Provide a custom Client
///
/// The ``AsyncHTTPClientTransport/Configuration-swift.struct`` type allows you
/// to provide a custom `HTTPClient` and tweak behaviors such as the default
/// timeout.
public struct AsyncHTTPClientTransport: ClientTransport {

/// A set of configuration values for the AsyncHTTPClient transport.
Expand All @@ -83,7 +75,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
/// - Parameters:
/// - client: The underlying client used to perform HTTP operations.
/// - timeout: The request timeout, defaults to 1 minute.
public init(client: HTTPClient, timeout: TimeAmount = .minutes(1)) {
public init(client: HTTPClient = .init(), timeout: TimeAmount = .minutes(1)) {
self.client = client
self.timeout = timeout
}
Expand Down

0 comments on commit 91dfd35

Please sign in to comment.