Skip to content

Commit 85d68a3

Browse files
committed
chore: import libraries directly and reexport types
1 parent 3726c09 commit 85d68a3

22 files changed

+126
-4653
lines changed

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ let package = Package(
2828
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"),
2929
.package(url: "https://github.com/apple/swift-log", from: "1.0.0"),
3030
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
31+
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
32+
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
3133
.package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.0"),
3234
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
3335
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
@@ -46,6 +48,8 @@ let package = Package(
4648
.product(name: "DequeModule", package: "swift-collections"),
4749
.product(name: "Clocks", package: "swift-clocks"),
4850
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
51+
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
52+
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
4953
]
5054
),
5155
.testTarget(

Sources/Helpers/HTTP/Client.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import HTTPTypes
2+
import OpenAPIRuntime
3+
4+
#if canImport(Darwin)
5+
import struct Foundation.URL
6+
#else
7+
@preconcurrency import struct Foundation.URL
8+
#endif
9+
10+
/// A client that can send HTTP requests and receive HTTP responses.
11+
struct Client: Sendable {
12+
13+
/// The URL of the server, used as the base URL for requests made by the
14+
/// client.
15+
let serverURL: URL
16+
17+
/// A type capable of sending HTTP requests and receiving HTTP responses.
18+
var transport: any ClientTransport
19+
20+
/// The middlewares to be invoked before the transport.
21+
var middlewares: [any ClientMiddleware]
22+
23+
/// Creates a new client.
24+
init(
25+
serverURL: URL,
26+
transport: any ClientTransport,
27+
middlewares: [any ClientMiddleware] = []
28+
) {
29+
self.serverURL = serverURL
30+
self.transport = transport
31+
self.middlewares = middlewares
32+
}
33+
34+
/// Sends the HTTP request and returns the HTTP response.
35+
///
36+
/// - Parameters:
37+
/// - request: The HTTP request to send.
38+
/// - body: The HTTP request body to send.
39+
/// - Returns: The HTTP response and its body.
40+
/// - Throws: An error if any part of the HTTP operation process fails.
41+
func send(
42+
_ request: HTTPTypes.HTTPRequest,
43+
body: HTTPBody? = nil
44+
) async throws -> (HTTPTypes.HTTPResponse, HTTPBody?) {
45+
let baseURL = serverURL
46+
var next:
47+
@Sendable (HTTPTypes.HTTPRequest, HTTPBody?, URL) async throws -> (
48+
HTTPTypes.HTTPResponse, HTTPBody?
49+
) = {
50+
(_request, _body, _url) in
51+
try await transport.send(
52+
_request,
53+
body: _body,
54+
baseURL: _url,
55+
operationID: ""
56+
)
57+
}
58+
for middleware in middlewares.reversed() {
59+
let tmp = next
60+
next = { (_request, _body, _url) in
61+
try await middleware.intercept(
62+
_request,
63+
body: _body,
64+
baseURL: _url,
65+
operationID: "",
66+
next: tmp
67+
)
68+
}
69+
}
70+
return try await next(request, body, baseURL)
71+
}
72+
}

Sources/Helpers/HTTP/Exports.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@_exported import HTTPTypes
2+
@_exported import protocol OpenAPIRuntime.ClientTransport
3+
@_exported import class OpenAPIRuntime.HTTPBody

Sources/Helpers/HTTP/HTTPClient/Base/PrettyStringConvertible.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Sources/Helpers/HTTP/HTTPClient/Errors/ClientError.swift

Lines changed: 0 additions & 128 deletions
This file was deleted.

Sources/Helpers/HTTP/HTTPClient/Errors/RuntimeError.swift

Lines changed: 0 additions & 88 deletions
This file was deleted.

Sources/Helpers/HTTP/HTTPClient/Exports.swift

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)