Skip to content

Commit

Permalink
add HTTPClient
Browse files Browse the repository at this point in the history
add HTTPClientFoundation

combining Body and Data into one associatedtype.

remove associatedType
  • Loading branch information
zunda-pixel committed Oct 24, 2024
1 parent f9a647b commit f0d99d1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
7 changes: 7 additions & 0 deletions Sources/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 30 additions & 0 deletions Sources/HTTPClientFoundation/URLSession++HTTPClient.swift
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f0d99d1

Please sign in to comment.