From 762f5e50ef7e342cc8099c0f4bab496fc8d7e237 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 1 Aug 2023 16:14:00 +0200 Subject: [PATCH 1/3] [AHCTransport] Consistent style for initializing local variables (#10) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [AHCTransport] Consistent style for initializing local variables ### Motivation Move to a consistent style when initializing local variables, always use `let foo = Foo(...)` vs `let foo: Foo = .init(...)`. ### Modifications Updated all occurrences of the latter to use the former. ### Result Consistent local variable initialization. ### Test Plan All tests passed. Reviewed by: gjcairo, simonjbeaumont 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. https://github.com/swift-server/swift-openapi-async-http-client/pull/10 --- .../Test_AsyncHTTPClientTransport.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift index b81fd02..f110257 100644 --- a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift +++ b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift @@ -63,7 +63,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase { } func testConvertResponse() async throws { - let httpResponse: HTTPClientResponse = .init( + let httpResponse = HTTPClientResponse( status: .ok, headers: [ "content-type": "application/json" @@ -83,7 +83,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase { func testSend() async throws { let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let httpClient: HTTPClient = .init( + let httpClient = HTTPClient( eventLoopGroupProvider: .shared(eventLoopGroup), configuration: .init() ) From 95bb2f8456ccbad2d69460c3a0967c80798a19ac Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 11 Aug 2023 16:02:13 +0200 Subject: [PATCH 2/3] Enable strict concurrency checking in CI (#11) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable strict concurrency checking in CI ### Motivation To further avoid concurrency bugs, enable complete concurrency checking in CI. ### Modifications Added the compiler flag to the docker-compose scripts. ### Result If a warning of this nature comes up, because we have warnings-as-errors, it'll fail CI. ### Test Plan Locally built without any warnings with the flag enabled. Reviewed by: 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. https://github.com/swift-server/swift-openapi-async-http-client/pull/11 --- .../AsyncHTTPClientTransport.swift | 9 ++++++++- docker/docker-compose.2204.58.yaml | 1 + docker/docker-compose.2204.59.yaml | 1 + docker/docker-compose.2204.main.yaml | 1 + docker/docker-compose.yaml | 2 +- 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index 35cb654..f09fdbb 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -15,8 +15,15 @@ import OpenAPIRuntime import AsyncHTTPClient import NIOCore import NIOHTTP1 -import Foundation import NIOFoundationCompat +#if canImport(Darwin) +import Foundation +#else +@preconcurrency import struct Foundation.URL +import struct Foundation.URLComponents +import struct Foundation.Data +import protocol Foundation.LocalizedError +#endif /// A client transport that performs HTTP operations using the HTTPClient type /// provided by the AsyncHTTPClient library. diff --git a/docker/docker-compose.2204.58.yaml b/docker/docker-compose.2204.58.yaml index eb4be59..eca0761 100644 --- a/docker/docker-compose.2204.58.yaml +++ b/docker/docker-compose.2204.58.yaml @@ -13,6 +13,7 @@ services: environment: - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors - IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error + - STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete shell: image: *image diff --git a/docker/docker-compose.2204.59.yaml b/docker/docker-compose.2204.59.yaml index a475877..1ac8741 100644 --- a/docker/docker-compose.2204.59.yaml +++ b/docker/docker-compose.2204.59.yaml @@ -12,6 +12,7 @@ services: environment: - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors - IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error + - STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete shell: image: *image diff --git a/docker/docker-compose.2204.main.yaml b/docker/docker-compose.2204.main.yaml index 31dbe00..b1443e6 100644 --- a/docker/docker-compose.2204.main.yaml +++ b/docker/docker-compose.2204.main.yaml @@ -12,6 +12,7 @@ services: environment: - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors - IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error + - STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete shell: image: *image diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 994d8b4..055978f 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -30,7 +30,7 @@ services: test: <<: *common - command: /bin/bash -xcl "swift $${SWIFT_TEST_VERB-test} $${WARN_AS_ERROR_ARG-} $${SANITIZER_ARG-} $${IMPORT_CHECK_ARG-}" + command: /bin/bash -xcl "swift $${SWIFT_TEST_VERB-test} $${WARN_AS_ERROR_ARG-} $${SANITIZER_ARG-} $${IMPORT_CHECK_ARG-} $${STRICT_CONCURRENCY_ARG-}" shell: <<: *common From 91dfd35092f622aedf2f79943293a9cccc25bff3 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 29 Aug 2023 14:45:24 +0200 Subject: [PATCH 3/3] Adopt the new shared HTTP client (#13) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. https://github.com/swift-server/swift-openapi-async-http-client/pull/13 --- Package.swift | 4 +-- .../AsyncHTTPClientTransport.swift | 26 +++++++------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Package.swift b/Package.swift index 04afd62..f093651 100644 --- a/Package.swift +++ b/Package.swift @@ -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"), ], diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index f09fdbb..1c5c27a 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -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 @@ -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. @@ -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 }