Skip to content

[AHC Transport] Async bodies + swift-http-types adoption #16

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 10 commits into from
Sep 27, 2023
Prev Previous commit
Next Next commit
Review feedback: make response body optional
  • Loading branch information
czechboy0 committed Sep 13, 2023
commit fba582b5ea57787eb74bc97641871577e57d4bec
29 changes: 20 additions & 9 deletions Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ public struct AsyncHTTPClientTransport: ClientTransport {
body: HTTPBody?,
baseURL: URL,
operationID: String
) async throws -> (HTTPResponse, HTTPBody) {
) async throws -> (HTTPResponse, HTTPBody?) {
let httpRequest = try Self.convertRequest(request, body: body, baseURL: baseURL)
let httpResponse = try await invokeSession(with: httpRequest)
let response = try await Self.convertResponse(httpResponse)
let response = try await Self.convertResponse(
method: request.method,
httpResponse: httpResponse
)
return response
}

Expand Down Expand Up @@ -192,8 +195,9 @@ public struct AsyncHTTPClientTransport: ClientTransport {

/// Converts the received URLResponse into the shared Response.
internal static func convertResponse(
_ httpResponse: HTTPClientResponse
) async throws -> (HTTPResponse, HTTPBody) {
method: HTTPRequest.Method,
httpResponse: HTTPClientResponse
) async throws -> (HTTPResponse, HTTPBody?) {

var headerFields: HTTPFields = [:]
for header in httpResponse.headers {
Expand All @@ -209,11 +213,18 @@ public struct AsyncHTTPClientTransport: ClientTransport {
length = .unknown
}

let body = HTTPBody(
httpResponse.body.map { $0.readableBytesView },
length: length,
iterationBehavior: .single
)
let body: HTTPBody?
switch method {
case .head, .connect, .trace:
body = nil
default:
body = HTTPBody(
httpResponse.body.map { $0.readableBytesView },
length: length,
iterationBehavior: .single
)
}

let response = HTTPResponse(
status: .init(code: Int(httpResponse.status.code)),
headerFields: headerFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
],
body: .bytes(Self.testBuffer)
)
let (response, responseBody) = try await AsyncHTTPClientTransport.convertResponse(httpResponse)
let (response, maybeResponseBody) = try await AsyncHTTPClientTransport.convertResponse(
method: .get,
httpResponse: httpResponse
)
let responseBody = try XCTUnwrap(maybeResponseBody)
XCTAssertEqual(response.status.code, 200)
XCTAssertEqual(
response.headerFields,
Expand Down Expand Up @@ -99,12 +103,13 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
.init("x-request")!: "yes"
]
)
let (response, responseBody) = try await transport.send(
let (response, maybeResponseBody) = try await transport.send(
request,
body: nil,
baseURL: Self.testUrl,
operationID: "sayHello"
)
let responseBody = try XCTUnwrap(maybeResponseBody)
let bufferedResponseBody = try await String(collecting: responseBody, upTo: .max)
XCTAssertEqual(bufferedResponseBody, "[{}]")
XCTAssertEqual(response.status.code, 200)
Expand Down