From f0d99d1413bd314d6e0d0263cd7ba97d13840ab3 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Sat, 10 Aug 2024 00:45:58 +0900 Subject: [PATCH] add HTTPClient add HTTPClientFoundation combining Body and Data into one associatedtype. remove associatedType --- Package.swift | 12 ++++++++ Sources/HTTPClient/HTTPClient.swift | 7 +++++ .../URLSession++HTTPClient.swift | 30 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 Sources/HTTPClient/HTTPClient.swift create mode 100644 Sources/HTTPClientFoundation/URLSession++HTTPClient.swift diff --git a/Package.swift b/Package.swift index b52ea72..7c9b7ee 100644 --- a/Package.swift +++ b/Package.swift @@ -19,6 +19,18 @@ let package = Package( "HTTPTypes", ] ), + .target( + name: "HTTPClient", + dependencies: [ + .target(name: "HTTPTypes"), + ] + ), + .target( + name: "HTTPClientFoundation", + dependencies: [ + .target(name: "HTTPClient"), + ] + ), .testTarget( name: "HTTPTypesTests", dependencies: [ diff --git a/Sources/HTTPClient/HTTPClient.swift b/Sources/HTTPClient/HTTPClient.swift new file mode 100644 index 0000000..832053b --- /dev/null +++ b/Sources/HTTPClient/HTTPClient.swift @@ -0,0 +1,7 @@ +import Foundation +import HTTPTypes + +public protocol HTTPClientProtocol { + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + func execute(for request: HTTPRequest, from body: Data?) async throws -> (Data, HTTPResponse) +} diff --git a/Sources/HTTPClientFoundation/URLSession++HTTPClient.swift b/Sources/HTTPClientFoundation/URLSession++HTTPClient.swift new file mode 100644 index 0000000..e2e94ef --- /dev/null +++ b/Sources/HTTPClientFoundation/URLSession++HTTPClient.swift @@ -0,0 +1,30 @@ +import Foundation +import HTTPClient +import HTTPTypes +import HTTPTypesFoundation + +#if canImport(FoundationNetworking) + import FoundationNetworking +#endif + +#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS) || compiler(>=6) + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + extension URLSession: HTTPClientProtocol { + public func execute( + for request: HTTPRequest, + from bodyData: Data? + ) async throws -> (Data, HTTPResponse) { + if let bodyData { + try await self.upload(for: request, from: bodyData) + } else { + try await self.data(for: request) + } + } + } + + extension HTTPClientProtocol where Self == URLSession { + public static func urlSession(_ urlSession: Self) -> Self { + return urlSession + } + } +#endif