From 57433eae129a300badda3a9cedc066a69ba83d61 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:49:42 +0900 Subject: [PATCH 1/4] add upload --- Sources/D1KitFoundation/URLSession+Linux.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/D1KitFoundation/URLSession+Linux.swift b/Sources/D1KitFoundation/URLSession+Linux.swift index f2bc388..5bdeb90 100644 --- a/Sources/D1KitFoundation/URLSession+Linux.swift +++ b/Sources/D1KitFoundation/URLSession+Linux.swift @@ -15,5 +15,17 @@ extension URLSession { task.resume() } } + func upload(for request: URLRequest, from data: Data?) async throws -> (Data, URLResponse) { + return try await withCheckedThrowingContinuation { continuation in + let task = self.uploadTask(with: request, from: data) { (data, response, error) in + guard let data = data, let response = response else { + let error = error ?? URLError(.badServerResponse) + return continuation.resume(throwing: error) + } + continuation.resume(returning: (data, response)) + } + task.resume() + } + } } #endif From 39c231270058f738fba44298e21f483cc383f5c7 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:50:26 +0900 Subject: [PATCH 2/4] add compiler check Swift 6 has URLSession async methods --- Sources/D1KitFoundation/URLSession+Linux.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/D1KitFoundation/URLSession+Linux.swift b/Sources/D1KitFoundation/URLSession+Linux.swift index 5bdeb90..8280a9f 100644 --- a/Sources/D1KitFoundation/URLSession+Linux.swift +++ b/Sources/D1KitFoundation/URLSession+Linux.swift @@ -1,7 +1,9 @@ import Foundation #if canImport(FoundationNetworking) import FoundationNetworking +#endif +#if compiler(<6) extension URLSession { func data(for request: URLRequest) async throws -> (Data, URLResponse) { return try await withCheckedThrowingContinuation { continuation in From 1cfe939f0b39471a6ed9f78bc99f0256d04c0f77 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:50:45 +0900 Subject: [PATCH 3/4] update swift-http-types version --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index a56c8de..0741619 100644 --- a/Package.swift +++ b/Package.swift @@ -9,7 +9,7 @@ let package = Package( .library(name: "D1Kit", targets: ["D1Kit"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"), ], targets: [ .target( From 1c91759fda3943b37b55c54824e7ff089e2461da Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:52:36 +0900 Subject: [PATCH 4/4] remove URLRequest convert --- .../D1KitFoundation/URLSessionHTTPClient.swift | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Sources/D1KitFoundation/URLSessionHTTPClient.swift b/Sources/D1KitFoundation/URLSessionHTTPClient.swift index 08c701f..9d69b21 100644 --- a/Sources/D1KitFoundation/URLSessionHTTPClient.swift +++ b/Sources/D1KitFoundation/URLSessionHTTPClient.swift @@ -6,22 +6,13 @@ import FoundationNetworking import HTTPTypes import HTTPTypesFoundation -private enum HTTPTypeConversionError: Error { - case failedToConvertHTTPRequestToURLRequest - case failedToConvertURLResponseToHTTPResponse -} - extension URLSession: HTTPClientProtocol { public func execute(_ request: HTTPRequest, body: Data?) async throws -> (Data, HTTPResponse) { - guard var urlRequest = URLRequest(httpRequest: request) else { - throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest - } - urlRequest.httpBody = body - let (data, urlResponse) = try await self.data(for: urlRequest) - guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { - throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + if let body { + try await self.upload(for: request, from: body) + } else { + try await self.data(for: request) } - return (data, response) } }