@@ -123,46 +123,33 @@ class HTTPRequestExecutorImpl: HTTPRequestExecutor {
123123 try await self . httpClient. execute ( request, timeout: timeout)
124124 }
125125
126- public func getCurrentSwiftlyRelease( ) async throws -> Components . Schemas . SwiftlyRelease {
127- let config = AsyncHTTPClientTransport . Configuration ( client: self . httpClient, timeout: . seconds( 30 ) )
126+ private func client( ) throws -> Client {
128127 let swiftlyUserAgent = SwiftlyUserAgentMiddleware ( )
128+ let transport : ClientTransport
129+
130+ let config = AsyncHTTPClientTransport . Configuration ( client: self . httpClient, timeout: . seconds( 30 ) )
131+ transport = AsyncHTTPClientTransport ( configuration: config)
129132
130- let client = Client (
133+ return Client (
131134 serverURL: try Servers . Server1. url ( ) ,
132- transport: AsyncHTTPClientTransport ( configuration : config ) ,
135+ transport: transport ,
133136 middlewares: [ swiftlyUserAgent]
134137 )
138+ }
135139
136- let response = try await client. getCurrentSwiftlyRelease ( )
140+ public func getCurrentSwiftlyRelease( ) async throws -> Components . Schemas . SwiftlyRelease {
141+ let response = try await client ( ) . getCurrentSwiftlyRelease ( )
137142 return try response. ok. body. json
138143 }
139144
140145 public func getReleaseToolchains( ) async throws -> [ Components . Schemas . Release ] {
141- let config = AsyncHTTPClientTransport . Configuration ( client: self . httpClient, timeout: . seconds( 30 ) )
142- let swiftlyUserAgent = SwiftlyUserAgentMiddleware ( )
143-
144- let client = Client (
145- serverURL: try Servers . Server1. url ( ) ,
146- transport: AsyncHTTPClientTransport ( configuration: config) ,
147- middlewares: [ swiftlyUserAgent]
148- )
149-
150- let response = try await client. listReleases ( )
146+ let response = try await client ( ) . listReleases ( )
151147
152148 return try response. ok. body. json
153149 }
154150
155151 public func getSnapshotToolchains( branch: Components . Schemas . SourceBranch , platform: Components . Schemas . PlatformIdentifier ) async throws -> Components . Schemas . DevToolchains {
156- let config = AsyncHTTPClientTransport . Configuration ( client: self . httpClient, timeout: . seconds( 30 ) )
157- let swiftlyUserAgent = SwiftlyUserAgentMiddleware ( )
158-
159- let client = Client (
160- serverURL: try Servers . Server1. url ( ) ,
161- transport: AsyncHTTPClientTransport ( configuration: config) ,
162- middlewares: [ swiftlyUserAgent]
163- )
164-
165- let response = try await client. listDevToolchains ( . init( path: . init( branch: branch, platform: platform) ) )
152+ let response = try await client ( ) . listDevToolchains ( . init( path: . init( branch: branch, platform: platform) ) )
166153
167154 return try response. ok. body. json
168155 }
@@ -295,52 +282,8 @@ extension Components.Schemas.DevToolchainForArch {
295282
296283/// HTTPClient wrapper used for interfacing with various REST APIs and downloading things.
297284public struct SwiftlyHTTPClient {
298- private struct Response {
299- let status : HTTPResponseStatus
300- let buffer : ByteBuffer
301- }
302-
303285 public init ( ) { }
304286
305- private func get( url: String , headers: [ String : String ] , maxBytes: Int ) async throws -> Response {
306- var request = makeRequest ( url: url)
307-
308- for (k, v) in headers {
309- request. headers. add ( name: k, value: v)
310- }
311-
312- let response = try await SwiftlyCore . httpRequestExecutor. execute ( request, timeout: . seconds( 30 ) )
313-
314- return Response ( status: response. status, buffer: try await response. body. collect ( upTo: maxBytes) )
315- }
316-
317- public struct JSONNotFoundError : LocalizedError {
318- public var url : String
319- }
320-
321- /// Decode the provided type `T` from the JSON body of the response from a GET request
322- /// to the given URL.
323- public func getFromJSON< T: Decodable > (
324- url: String ,
325- type: T . Type ,
326- headers: [ String : String ] = [ : ]
327- ) async throws -> T {
328- // Maximum expected size for a JSON payload for an API is 1MB
329- let response = try await self . get ( url: url, headers: headers, maxBytes: 1024 * 1024 )
330-
331- switch response. status {
332- case . ok:
333- break
334- case . notFound:
335- throw SwiftlyHTTPClient . JSONNotFoundError ( url: url)
336- default :
337- let json = String ( buffer: response. buffer)
338- throw SwiftlyError ( message: " Received \( response. status) when reaching \( url) for JSON: \( json) " )
339- }
340-
341- return try JSONDecoder ( ) . decode ( type. self, from: response. buffer)
342- }
343-
344287 /// Return the current Swiftly release using the swift.org API.
345288 public func getCurrentSwiftlyRelease( ) async throws -> Components . Schemas . SwiftlyRelease {
346289 try await SwiftlyCore . httpRequestExecutor. getCurrentSwiftlyRelease ( )
0 commit comments