Skip to content

Adopt AHC's .shared singleton as the default Client #39

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

Merged
merged 3 commits into from
Oct 3, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let package = Package(
],
dependencies: [
.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/swift-server/async-http-client.git", from: "1.23.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Expand Down
26 changes: 13 additions & 13 deletions Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,27 @@ public struct AsyncHTTPClientTransport: ClientTransport {
/// The HTTP client used for performing HTTP calls.
public var client: HTTPClient

/// The default shared HTTP client.
///
/// This is a workaround for the lack of a shared client
/// in AsyncHTTPClient. Do not use this value directly, outside of
/// the `Configuration.init(client:timeout:)` initializer, as it will
/// likely be removed in the future.
private static let sharedClient: HTTPClient = .init()

/// The default request timeout.
public var timeout: TimeAmount

/// Creates a new configuration with the specified client and timeout.
/// - Parameters:
/// - client: The underlying client used to perform HTTP operations.
/// Provide nil to use the shared internal client.
/// - timeout: The request timeout, defaults to 1 minute.
public init(client: HTTPClient? = nil, timeout: TimeAmount = .minutes(1)) {
self.client = client ?? Self.sharedClient
public init(client: HTTPClient = .shared, timeout: TimeAmount = .minutes(1)) {
self.client = client
self.timeout = timeout
}

/// Creates a new configuration with the specified client and timeout.
/// - Parameters:
/// - client: The underlying client used to perform HTTP operations.
/// Provide nil to use the shared client.
/// - timeout: The request timeout, defaults to 1 minute.
@available(*, deprecated, message: "Use the initializer with a non-optional client parameter.")
@_disfavoredOverload public init(client: HTTPClient? = nil, timeout: TimeAmount = .minutes(1)) {
self.init(client: client ?? .shared, timeout: timeout)
}
}

/// A request to be sent by the transport.
Expand Down Expand Up @@ -174,8 +175,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
let length: HTTPClientRequest.Body.Length
switch body.length {
case .unknown: length = .unknown
case .known(let count):
if let intValue = Int(exactly: count) { length = .known(intValue) } else { length = .unknown }
case .known(let count): length = .known(count)
}
clientRequest.body = .stream(body.map { .init(bytes: $0) }, length: length)
}
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.2204.main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
test:
image: *image
environment:
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
# - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
- STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete

Expand Down