From 97b84ae60824941639b4520ac87808e0a455e04a Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Mon, 18 Sep 2023 09:10:46 +0200 Subject: [PATCH 1/3] Adopt a custom shared client --- .../AsyncHTTPClientTransport.swift | 10 +++++++++- .../Test_AsyncHTTPClientTransport.swift | 10 +--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index 748c093..0af5486 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -68,6 +68,14 @@ 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. + public static let sharedClient: HTTPClient = .init() + /// The default request timeout. public var timeout: TimeAmount @@ -75,7 +83,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 = .init(), timeout: TimeAmount = .minutes(1)) { + public init(client: HTTPClient = Self.sharedClient, timeout: TimeAmount = .minutes(1)) { self.client = client self.timeout = timeout } diff --git a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift index 03b732b..594a828 100644 --- a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift +++ b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift @@ -82,16 +82,8 @@ class Test_AsyncHTTPClientTransport: XCTestCase { } func testSend() async throws { - let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let httpClient = HTTPClient( - eventLoopGroupProvider: .shared(eventLoopGroup), - configuration: .init() - ) - defer { - try! httpClient.syncShutdown() - } let transport = AsyncHTTPClientTransport( - configuration: .init(client: httpClient), + configuration: .init(), requestSender: TestSender.test ) let request: OpenAPIRuntime.Request = .init( From 22d55af82334fa4950c977ba6ba299215da6586c Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Mon, 18 Sep 2023 15:10:46 +0200 Subject: [PATCH 2/3] Make the shared client private --- .../OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index 0af5486..4d732b9 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -74,7 +74,7 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// 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. - public static let sharedClient: HTTPClient = .init() + private static let sharedClient: HTTPClient = .init() /// The default request timeout. public var timeout: TimeAmount @@ -82,9 +82,10 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// 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 = Self.sharedClient, timeout: TimeAmount = .minutes(1)) { - self.client = client + public init(client: HTTPClient? = nil, timeout: TimeAmount = .minutes(1)) { + self.client = Self.sharedClient self.timeout = timeout } } From 312833717f2632732941a420877ab0cc73c03821 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Mon, 18 Sep 2023 15:17:51 +0200 Subject: [PATCH 3/3] Update Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift Co-authored-by: David Nadoba --- Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index 4d732b9..08d11b2 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -85,7 +85,7 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// 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 = Self.sharedClient + self.client = client ?? Self.sharedClient self.timeout = timeout } }