Skip to content

Commit dee6c2c

Browse files
authored
improve registry client handling of alternative locations (#4142)
motication: preparation work for mixed graphs changes: * rename RegistryClient::listVersions API to getPackageMetadata and revise it to parse the link header that contains the alternative URLs for the package * enhance RegistryClient::getIdentifers API to use codable for parsing the server result * add small utility to drop prefx from string to Basics * cleanup redundant HTTPClientProtocol which was used for RegistryClient testing but since deprecated * add and adjust tests
1 parent a06b331 commit dee6c2c

File tree

7 files changed

+312
-198
lines changed

7 files changed

+312
-198
lines changed

Sources/Basics/HTPClient+URLSession.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import struct TSCUtility.Versioning
1717
import FoundationNetworking
1818
#endif
1919

20-
public struct URLSessionHTTPClient: HTTPClientProtocol {
20+
public struct URLSessionHTTPClient {
2121
private let dataTasksManager: DataTaskManager
2222
private let downloadsTasksManager: DownloadTaskManager
2323

@@ -26,11 +26,11 @@ public struct URLSessionHTTPClient: HTTPClientProtocol {
2626
self.downloadsTasksManager = DownloadTaskManager(configuration: configuration)
2727
}
2828

29-
public func execute(_ request: HTTPClient.Request, progress: ProgressHandler?, completion: @escaping CompletionHandler) {
29+
public func execute(_ request: HTTPClient.Request, progress: HTTPClient.ProgressHandler?, completion: @escaping HTTPClient.CompletionHandler) {
3030
self.execute(request, observabilityScope: nil, progress: progress, completion: completion)
3131
}
3232

33-
public func execute(_ request: HTTPClient.Request, observabilityScope: ObservabilityScope? = nil, progress: ProgressHandler?, completion: @escaping CompletionHandler) {
33+
public func execute(_ request: HTTPClient.Request, observabilityScope: ObservabilityScope? = nil, progress: HTTPClient.ProgressHandler?, completion: @escaping HTTPClient.CompletionHandler) {
3434
switch request.kind {
3535
case .generic:
3636
let task = self.dataTasksManager.makeTask(request: request, progress: progress, completion: completion)

Sources/Basics/HTTPClient.swift

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ import Glibc
2424
import CRT
2525
#endif
2626

27-
public protocol HTTPClientProtocol {
28-
typealias ProgressHandler = (_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void
29-
typealias CompletionHandler = (Result<HTTPClientResponse, Error>) -> Void
30-
31-
/// Execute an HTTP request asynchronously
32-
///
33-
/// - Parameters:
34-
/// - request: The `HTTPClientRequest` to perform.
35-
/// - observabilityScope: the observability scope to emit diagnostics on
36-
/// - progress: A closure to handle progress for example for downloads
37-
/// - callback: A closure to be notified of the completion of the request.
38-
func execute(_ request: HTTPClientRequest,
39-
observabilityScope: ObservabilityScope?,
40-
progress: ProgressHandler?,
41-
completion: @escaping CompletionHandler)
42-
}
43-
4427
public enum HTTPClientError: Error, Equatable {
4528
case invalidResponse
4629
case badResponseStatusCode(Int)
@@ -51,11 +34,13 @@ public enum HTTPClientError: Error, Equatable {
5134

5235
// MARK: - HTTPClient
5336

54-
public struct HTTPClient: HTTPClientProtocol {
37+
public struct HTTPClient {
5538
public typealias Configuration = HTTPClientConfiguration
5639
public typealias Request = HTTPClientRequest
5740
public typealias Response = HTTPClientResponse
5841
public typealias Handler = (Request, ProgressHandler?, @escaping (Result<Response, Error>) -> Void) -> Void
42+
public typealias ProgressHandler = (_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void
43+
public typealias CompletionHandler = (Result<HTTPClientResponse, Error>) -> Void
5944

6045
public var configuration: HTTPClientConfiguration
6146
private let underlying: Handler
@@ -70,6 +55,13 @@ public struct HTTPClient: HTTPClientProtocol {
7055
self.underlying = handler ?? URLSessionHTTPClient().execute
7156
}
7257

58+
/// Execute an HTTP request asynchronously
59+
///
60+
/// - Parameters:
61+
/// - request: The `HTTPClientRequest` to perform.
62+
/// - observabilityScope: the observability scope to emit diagnostics on
63+
/// - progress: A progress handler to handle progress for example for downloads
64+
/// - completion: A completion handler to be notified of the completion of the request.
7365
public func execute(_ request: Request, observabilityScope: ObservabilityScope? = nil, progress: ProgressHandler? = nil, completion: @escaping CompletionHandler) {
7466
// merge configuration
7567
var request = request

Sources/Basics/String+Extensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ extension String {
1717
public var sha256Checksum: String {
1818
return SHA256().hash(self).hexadecimalRepresentation
1919
}
20+
21+
/// Drops the given suffix from the string, if present.
22+
public func spm_dropPrefix(_ prefix: String) -> String {
23+
if self.hasPrefix(prefix) {
24+
return String(self.dropFirst(prefix.count))
25+
}
26+
return self
27+
}
2028
}

0 commit comments

Comments
 (0)