Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPClient for Web API Client Library #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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