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( diff --git a/Sources/D1KitFoundation/URLSession+Linux.swift b/Sources/D1KitFoundation/URLSession+Linux.swift index f2bc388..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 @@ -15,5 +17,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 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) } }