|
1 | 1 | import _StringProcessing |
2 | 2 | import AsyncHTTPClient |
3 | 3 | import Foundation |
| 4 | +import HTTPTypes |
4 | 5 | import NIO |
5 | 6 | import NIOFoundationCompat |
6 | 7 | import NIOHTTP1 |
7 | 8 | import OpenAPIAsyncHTTPClient |
8 | 9 | import OpenAPIRuntime |
9 | 10 |
|
| 11 | +extension Components.Schemas.SwiftlyRelease { |
| 12 | + public var swiftlyVersion: SwiftlyVersion { |
| 13 | + get throws { |
| 14 | + guard let releaseVersion = try? SwiftlyVersion(parsing: self.version) else { |
| 15 | + throw SwiftlyError(message: "Invalid swiftly version reported: \(self.version)") |
| 16 | + } |
| 17 | + |
| 18 | + return releaseVersion |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +extension Components.Schemas.SwiftlyReleasePlatformArtifacts { |
| 24 | + public var isDarwin: Bool { |
| 25 | + self.platform.value1 == .darwin |
| 26 | + } |
| 27 | + |
| 28 | + public var isLinux: Bool { |
| 29 | + self.platform.value1 == .linux |
| 30 | + } |
| 31 | + |
| 32 | + public var x86_64URL: URL { |
| 33 | + get throws { |
| 34 | + guard let url = URL(string: self.x8664) else { |
| 35 | + throw SwiftlyError(message: "The swiftly x86_64 URL is invalid: \(self.x8664)") |
| 36 | + } |
| 37 | + |
| 38 | + return url |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public var arm64URL: URL { |
| 43 | + get throws { |
| 44 | + guard let url = URL(string: self.arm64) else { |
| 45 | + throw SwiftlyError(message: "The swiftly arm64 URL is invalid: \(self.arm64)") |
| 46 | + } |
| 47 | + |
| 48 | + return url |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
10 | 53 | public protocol HTTPRequestExecutor { |
11 | 54 | func execute(_ request: HTTPClientRequest, timeout: TimeAmount) async throws -> HTTPClientResponse |
12 | 55 | func getCurrentSwiftlyRelease() async throws -> Components.Schemas.SwiftlyRelease |
13 | 56 | } |
14 | 57 |
|
| 58 | +internal struct SwiftlyUserAgentMiddleware: ClientMiddleware { |
| 59 | + package func intercept( |
| 60 | + _ request: HTTPRequest, |
| 61 | + body: HTTPBody?, |
| 62 | + baseURL: URL, |
| 63 | + operationID _: String, |
| 64 | + next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?) |
| 65 | + ) async throws -> (HTTPResponse, HTTPBody?) { |
| 66 | + var request = request |
| 67 | + // Adds the `Authorization` header field with the provided value. |
| 68 | + request.headerFields[.userAgent] = "swiftly/\(SwiftlyCore.version)" |
| 69 | + return try await next(request, body, baseURL) |
| 70 | + } |
| 71 | +} |
| 72 | + |
15 | 73 | /// An `HTTPRequestExecutor` backed by the shared `HTTPClient`. |
16 | 74 | internal class HTTPRequestExecutorImpl: HTTPRequestExecutor { |
17 | 75 | let httpClient: HTTPClient |
@@ -59,11 +117,12 @@ internal class HTTPRequestExecutorImpl: HTTPRequestExecutor { |
59 | 117 |
|
60 | 118 | public func getCurrentSwiftlyRelease() async throws -> Components.Schemas.SwiftlyRelease { |
61 | 119 | let config = AsyncHTTPClientTransport.Configuration(client: self.httpClient, timeout: .seconds(30)) |
| 120 | + let swiftlyUserAgent = SwiftlyUserAgentMiddleware() |
62 | 121 |
|
63 | | - // FIXME: use a middleware that adds the swiftly user agent header (problem is how to do this with async http client) |
64 | 122 | let client = Client( |
65 | 123 | serverURL: URL(string: "https://swift.org/api")!, |
66 | | - transport: AsyncHTTPClientTransport(configuration: config) |
| 124 | + transport: AsyncHTTPClientTransport(configuration: config), |
| 125 | + middlewares: [swiftlyUserAgent] |
67 | 126 | ) |
68 | 127 |
|
69 | 128 | let response = try await client.getCurrentSwiftlyRelease() |
|
0 commit comments