diff --git a/Sources/AsyncHTTPClient/HTTPHandler.swift b/Sources/AsyncHTTPClient/HTTPHandler.swift index 7b2c5c6ff..278bf18a8 100644 --- a/Sources/AsyncHTTPClient/HTTPHandler.swift +++ b/Sources/AsyncHTTPClient/HTTPHandler.swift @@ -750,13 +750,33 @@ extension HTTPClient { return self.promise.futureResult } + #if swift(>=5.7) /// Waits for execution of this request to complete. /// - /// - returns: The value of the `EventLoopFuture` when it completes. - /// - throws: The error value of the `EventLoopFuture` if it errors. + /// - returns: The value of ``futureResult`` when it completes. + /// - throws: The error value of ``futureResult`` if it errors. + @available(*, noasync, message: "wait() can block indefinitely, prefer get()", renamed: "get()") public func wait() throws -> Response { return try self.promise.futureResult.wait() } + #else + /// Waits for execution of this request to complete. + /// + /// - returns: The value of ``futureResult`` when it completes. + /// - throws: The error value of ``futureResult`` if it errors. + public func wait() throws -> Response { + return try self.promise.futureResult.wait() + } + #endif + + /// Provides the result of this request. + /// + /// - returns: The value of ``futureResult`` when it completes. + /// - throws: The error value of ``futureResult`` if it errors. + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + public func get() async throws -> Response { + return try await self.promise.futureResult.get() + } /// Cancels the request execution. public func cancel() { diff --git a/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests+XCTest.swift b/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests+XCTest.swift index c0b028905..20538c43a 100644 --- a/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests+XCTest.swift +++ b/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests+XCTest.swift @@ -49,6 +49,7 @@ extension AsyncAwaitEndToEndTests { ("testRejectsInvalidCharactersInHeaderFieldNames_http2", testRejectsInvalidCharactersInHeaderFieldNames_http2), ("testRejectsInvalidCharactersInHeaderFieldValues_http1", testRejectsInvalidCharactersInHeaderFieldValues_http1), ("testRejectsInvalidCharactersInHeaderFieldValues_http2", testRejectsInvalidCharactersInHeaderFieldValues_http2), + ("testUsingGetMethodInsteadOfWait", testUsingGetMethodInsteadOfWait), ] } } diff --git a/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift b/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift index 97c802319..3d0e709d6 100644 --- a/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift +++ b/Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift @@ -725,6 +725,26 @@ final class AsyncAwaitEndToEndTests: XCTestCase { } } } + + func testUsingGetMethodInsteadOfWait() { + guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return } + XCTAsyncTest { + let bin = HTTPBin(.http2(compress: false)) + defer { XCTAssertNoThrow(try bin.shutdown()) } + let client = makeDefaultHTTPClient() + defer { XCTAssertNoThrow(try client.syncShutdown()) } + let request = try HTTPClient.Request(url: "https://localhost:\(bin.port)/get") + + guard let response = await XCTAssertNoThrowWithResult( + try await client.execute(request: request).get() + ) else { + return + } + + XCTAssertEqual(response.status, .ok) + XCTAssertEqual(response.version, .http2) + } + } } extension AsyncSequence where Element == ByteBuffer {