From 1f7ee43023825596baf11f3303d1be7e75d2f2b2 Mon Sep 17 00:00:00 2001 From: PARAIPAN SORIN <paraipan.sorin1993@gmail.com> Date: Wed, 1 Nov 2023 17:15:37 +0200 Subject: [PATCH] Set respectsExistingLineBreaks to true from .swift-format --- .swift-format | 2 +- .../AsyncHTTPClientTransport.swift | 140 +++++------------- .../Test_AsyncHTTPClientTransport.swift | 59 ++------ 3 files changed, 53 insertions(+), 148 deletions(-) diff --git a/.swift-format b/.swift-format index f8c8ff5..0df000c 100644 --- a/.swift-format +++ b/.swift-format @@ -14,7 +14,7 @@ "lineLength" : 120, "maximumBlankLines" : 1, "prioritizeKeepingFunctionOutputTogether" : false, - "respectsExistingLineBreaks" : true, + "respectsExistingLineBreaks" : false, "rules" : { "AllPublicDeclarationsHaveDocumentation" : true, "AlwaysUseLowerCamelCase" : false, diff --git a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift index bb68295..5851103 100644 --- a/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift +++ b/Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift @@ -115,9 +115,7 @@ public struct AsyncHTTPClientTransport: ClientTransport { // MARK: LocalizedError - var errorDescription: String? { - description - } + var errorDescription: String? { description } } /// A set of configuration values used by the transport. @@ -130,10 +128,7 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// - Parameters: /// - configuration: A set of configuration values used by the transport. /// - requestSender: The underlying request sender. - internal init( - configuration: Configuration, - requestSender: any HTTPRequestSending - ) { + internal init(configuration: Configuration, requestSender: any HTTPRequestSending) { self.configuration = configuration self.requestSender = requestSender } @@ -141,10 +136,7 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// Creates a new transport. /// - Parameter configuration: A set of configuration values used by the transport. public init(configuration: Configuration) { - self.init( - configuration: configuration, - requestSender: AsyncHTTPRequestSender() - ) + self.init(configuration: configuration, requestSender: AsyncHTTPRequestSender()) } // MARK: ClientTransport @@ -159,40 +151,27 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// /// - Returns: A tuple containing the HTTP response and an optional HTTP body in the response. /// - Throws: An error if the request or response handling encounters any issues. - public func send( - _ request: HTTPRequest, - body: HTTPBody?, - baseURL: URL, - operationID: String - ) async throws -> (HTTPResponse, HTTPBody?) { + public func send(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL, operationID: String) 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( - method: request.method, - httpResponse: httpResponse - ) + let response = try await Self.convertResponse(method: request.method, httpResponse: httpResponse) return response } // MARK: Internal /// Converts the shared Request type into URLRequest. - internal static func convertRequest( - _ request: HTTPRequest, - body: HTTPBody?, - baseURL: URL - ) throws -> HTTPClientRequest { - guard - var baseUrlComponents = URLComponents(string: baseURL.absoluteString), + internal static func convertRequest(_ request: HTTPRequest, body: HTTPBody?, baseURL: URL) throws + -> HTTPClientRequest + { + guard var baseUrlComponents = URLComponents(string: baseURL.absoluteString), let requestUrlComponents = URLComponents(string: request.path ?? "") - else { - throw Error.invalidRequestURL(request: request, baseURL: baseURL) - } + else { throw Error.invalidRequestURL(request: request, baseURL: baseURL) } baseUrlComponents.percentEncodedPath += requestUrlComponents.percentEncodedPath baseUrlComponents.percentEncodedQuery = requestUrlComponents.percentEncodedQuery - guard let url = baseUrlComponents.url else { - throw Error.invalidRequestURL(request: request, baseURL: baseURL) - } + guard let url = baseUrlComponents.url else { throw Error.invalidRequestURL(request: request, baseURL: baseURL) } var clientRequest = HTTPClientRequest(url: url.absoluteString) clientRequest.method = request.method.asHTTPMethod for header in request.headerFields { @@ -201,34 +180,24 @@ public struct AsyncHTTPClientTransport: ClientTransport { if let body { let length: HTTPClientRequest.Body.Length switch body.length { - case .unknown: - length = .unknown - case .known(let count): - length = .known(count) + case .unknown: length = .unknown + case .known(let count): length = .known(count) } - clientRequest.body = .stream( - body.map { .init(bytes: $0) }, - length: length - ) + clientRequest.body = .stream(body.map { .init(bytes: $0) }, length: length) } return clientRequest } /// Converts the received URLResponse into the shared Response. - internal static func convertResponse( - method: HTTPRequest.Method, - httpResponse: HTTPClientResponse - ) async throws -> (HTTPResponse, HTTPBody?) { + internal static func convertResponse(method: HTTPRequest.Method, httpResponse: HTTPClientResponse) async throws -> ( + HTTPResponse, HTTPBody? + ) { var headerFields: HTTPFields = [:] - for header in httpResponse.headers { - headerFields[.init(header.name)!] = header.value - } + for header in httpResponse.headers { headerFields[.init(header.name)!] = header.value } let length: HTTPBody.Length - if let lengthHeaderString = headerFields[.contentLength], - let lengthHeader = Int(lengthHeaderString) - { + if let lengthHeaderString = headerFields[.contentLength], let lengthHeader = Int(lengthHeaderString) { length = .known(lengthHeader) } else { length = .unknown @@ -236,20 +205,12 @@ public struct AsyncHTTPClientTransport: ClientTransport { let body: HTTPBody? switch method { - case .head, .connect, .trace: - body = nil + case .head, .connect, .trace: body = nil default: - body = HTTPBody( - httpResponse.body.map { $0.readableBytesView }, - length: length, - iterationBehavior: .single - ) + body = HTTPBody(httpResponse.body.map { $0.readableBytesView }, length: length, iterationBehavior: .single) } - let response = HTTPResponse( - status: .init(code: Int(httpResponse.status.code)), - headerFields: headerFields - ) + let response = HTTPResponse(status: .init(code: Int(httpResponse.status.code)), headerFields: headerFields) return (response, body) } @@ -257,58 +218,35 @@ public struct AsyncHTTPClientTransport: ClientTransport { /// Makes the underlying HTTP call. private func invokeSession(with request: Request) async throws -> Response { - try await requestSender.send( - request: request, - with: configuration.client, - timeout: configuration.timeout - ) + try await requestSender.send(request: request, with: configuration.client, timeout: configuration.timeout) } } extension HTTPTypes.HTTPRequest.Method { var asHTTPMethod: NIOHTTP1.HTTPMethod { switch self { - case .get: - return .GET - case .put: - return .PUT - case .post: - return .POST - case .delete: - return .DELETE - case .options: - return .OPTIONS - case .head: - return .HEAD - case .patch: - return .PATCH - case .trace: - return .TRACE - default: - return .RAW(value: rawValue) + case .get: return .GET + case .put: return .PUT + case .post: return .POST + case .delete: return .DELETE + case .options: return .OPTIONS + case .head: return .HEAD + case .patch: return .PATCH + case .trace: return .TRACE + default: return .RAW(value: rawValue) } } } /// A type that performs HTTP operations using the HTTP client. internal protocol HTTPRequestSending: Sendable { - func send( - request: AsyncHTTPClientTransport.Request, - with client: HTTPClient, - timeout: TimeAmount - ) async throws -> AsyncHTTPClientTransport.Response + func send(request: AsyncHTTPClientTransport.Request, with client: HTTPClient, timeout: TimeAmount) async throws + -> AsyncHTTPClientTransport.Response } /// Performs HTTP calls using AsyncHTTPClient internal struct AsyncHTTPRequestSender: HTTPRequestSending { - func send( - request: AsyncHTTPClientTransport.Request, - with client: AsyncHTTPClient.HTTPClient, - timeout: TimeAmount - ) async throws -> AsyncHTTPClientTransport.Response { - try await client.execute( - request, - timeout: timeout - ) - } + func send(request: AsyncHTTPClientTransport.Request, with client: AsyncHTTPClient.HTTPClient, timeout: TimeAmount) + async throws -> AsyncHTTPClientTransport.Response + { try await client.execute(request, timeout: timeout) } } diff --git a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift index 4a627a4..7bd9f41 100644 --- a/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift +++ b/Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift @@ -21,20 +21,12 @@ import HTTPTypes class Test_AsyncHTTPClientTransport: XCTestCase { - static var testData: Data { - get throws { - try XCTUnwrap(#"[{}]"#.data(using: .utf8)) - } - } + static var testData: Data { get throws { try XCTUnwrap(#"[{}]"#.data(using: .utf8)) } } - static var testBuffer: ByteBuffer { - ByteBuffer(string: #"[{}]"#) - } + static var testBuffer: ByteBuffer { ByteBuffer(string: #"[{}]"#) } static var testUrl: URL { - get throws { - try XCTUnwrap(URL(string: "http://example.com/api/v1/hello/Maria?greeting=Howdy")) - } + get throws { try XCTUnwrap(URL(string: "http://example.com/api/v1/hello/Maria?greeting=Howdy")) } } func testConvertRequest() throws { @@ -43,9 +35,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase { scheme: nil, authority: nil, path: "/hello%20world/Maria?greeting=Howdy", - headerFields: [ - .contentType: "application/json" - ] + headerFields: [.contentType: "application/json"] ) let requestBody = try HTTPBody(Self.testData) let httpRequest = try AsyncHTTPClientTransport.convertRequest( @@ -55,12 +45,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase { ) XCTAssertEqual(httpRequest.url, "http://example.com/api/v1/hello%20world/Maria?greeting=Howdy") XCTAssertEqual(httpRequest.method, .POST) - XCTAssertEqual( - httpRequest.headers, - [ - "content-type": "application/json" - ] - ) + XCTAssertEqual(httpRequest.headers, ["content-type": "application/json"]) // TODO: Not sure how to test that httpRequest.body is what we expect, can't // find an API for reading it back. } @@ -68,9 +53,7 @@ class Test_AsyncHTTPClientTransport: XCTestCase { func testConvertResponse() async throws { let httpResponse = HTTPClientResponse( status: .ok, - headers: [ - "content-type": "application/json" - ], + headers: ["content-type": "application/json"], body: .bytes(Self.testBuffer) ) let (response, maybeResponseBody) = try await AsyncHTTPClientTransport.convertResponse( @@ -79,29 +62,19 @@ class Test_AsyncHTTPClientTransport: XCTestCase { ) let responseBody = try XCTUnwrap(maybeResponseBody) XCTAssertEqual(response.status.code, 200) - XCTAssertEqual( - response.headerFields, - [ - .contentType: "application/json" - ] - ) + XCTAssertEqual(response.headerFields, [.contentType: "application/json"]) let bufferedResponseBody = try await Data(collecting: responseBody, upTo: .max) XCTAssertEqual(bufferedResponseBody, try Self.testData) } func testSend() async throws { - let transport = AsyncHTTPClientTransport( - configuration: .init(), - requestSender: TestSender.test - ) + let transport = AsyncHTTPClientTransport(configuration: .init(), requestSender: TestSender.test) let request: HTTPRequest = .init( method: .get, scheme: nil, authority: nil, path: "/api/v1/hello/Maria", - headerFields: [ - .init("x-request")!: "yes" - ] + headerFields: [.init("x-request")!: "yes"] ) let (response, maybeResponseBody) = try await transport.send( request, @@ -120,22 +93,16 @@ struct TestSender: HTTPRequestSending { var sendClosure: @Sendable (AsyncHTTPClientTransport.Request, HTTPClient, TimeAmount) async throws -> AsyncHTTPClientTransport.Response - func send( - request: AsyncHTTPClientTransport.Request, - with client: HTTPClient, - timeout: TimeAmount - ) async throws -> AsyncHTTPClientTransport.Response { - try await sendClosure(request, client, timeout) - } + func send(request: AsyncHTTPClientTransport.Request, with client: HTTPClient, timeout: TimeAmount) async throws + -> AsyncHTTPClientTransport.Response + { try await sendClosure(request, client, timeout) } static var test: Self { TestSender { request, _, _ in XCTAssertEqual(request.headers.first(name: "x-request"), "yes") return HTTPClientResponse( status: .ok, - headers: [ - "content-type": "application/json" - ], + headers: ["content-type": "application/json"], body: .bytes(Test_AsyncHTTPClientTransport.testBuffer) ) }