Skip to content

Commit 0990fae

Browse files
committed
Remove some dead code and deduplicate some code in the HTTP request handling
Some of this was no longer used after #253
1 parent 351a278 commit 0990fae

File tree

2 files changed

+12
-111
lines changed

2 files changed

+12
-111
lines changed

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
297284
public 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()

Tests/SwiftlyTests/HTTPClientTests.swift

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,6 @@
33
import XCTest
44

55
final class HTTPClientTests: SwiftlyTests {
6-
func testGet() async throws {
7-
// GIVEN: we have a swiftly http client
8-
// WHEN: we make get request for a particular type of JSON
9-
var releases: [Components.Schemas.Release] = try await SwiftlyCore.httpClient.getFromJSON(
10-
url: "https://www.swift.org/api/v1/install/releases.json",
11-
type: [Components.Schemas.Release].self,
12-
headers: [:]
13-
)
14-
// THEN: we get a decoded JSON response
15-
XCTAssertTrue(releases.count > 0)
16-
17-
// GIVEN: we have a swiftly http client
18-
// WHEN: we make a request to an invalid URL path
19-
var exceptionThrown = false
20-
do {
21-
releases = try await SwiftlyCore.httpClient.getFromJSON(
22-
url: "https://www.swift.org/api/v1/install/releases-invalid.json",
23-
type: [Components.Schemas.Release].self,
24-
headers: [:]
25-
)
26-
} catch {
27-
exceptionThrown = true
28-
}
29-
// THEN: we receive an exception
30-
XCTAssertTrue(exceptionThrown)
31-
32-
// GIVEN: we have a swiftly http client
33-
// WHEN: we make a request to an invalid host path
34-
exceptionThrown = false
35-
do {
36-
releases = try await SwiftlyCore.httpClient.getFromJSON(
37-
url: "https://invalid.swift.org/api/v1/install/releases.json",
38-
type: [Components.Schemas.Release].self,
39-
headers: [:]
40-
)
41-
} catch {
42-
exceptionThrown = true
43-
}
44-
// THEN: we receive an exception
45-
XCTAssertTrue(exceptionThrown)
46-
}
47-
486
func testGetSwiftlyReleaseMetadataFromSwiftOrg() async throws {
497
let currentRelease = try await SwiftlyCore.httpClient.getCurrentSwiftlyRelease()
508
XCTAssertNoThrow(try currentRelease.swiftlyVersion)

0 commit comments

Comments
 (0)