Skip to content

improve registry client handling of alternative locations #4142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Basics/HTPClient+URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import struct TSCUtility.Versioning
import FoundationNetworking
#endif

public struct URLSessionHTTPClient: HTTPClientProtocol {
public struct URLSessionHTTPClient {
private let dataTasksManager: DataTaskManager
private let downloadsTasksManager: DownloadTaskManager

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

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

public func execute(_ request: HTTPClient.Request, observabilityScope: ObservabilityScope? = nil, progress: ProgressHandler?, completion: @escaping CompletionHandler) {
public func execute(_ request: HTTPClient.Request, observabilityScope: ObservabilityScope? = nil, progress: HTTPClient.ProgressHandler?, completion: @escaping HTTPClient.CompletionHandler) {
switch request.kind {
case .generic:
let task = self.dataTasksManager.makeTask(request: request, progress: progress, completion: completion)
Expand Down
28 changes: 10 additions & 18 deletions Sources/Basics/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ import Glibc
import CRT
#endif

public protocol HTTPClientProtocol {
typealias ProgressHandler = (_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void
typealias CompletionHandler = (Result<HTTPClientResponse, Error>) -> Void

/// Execute an HTTP request asynchronously
///
/// - Parameters:
/// - request: The `HTTPClientRequest` to perform.
/// - observabilityScope: the observability scope to emit diagnostics on
/// - progress: A closure to handle progress for example for downloads
/// - callback: A closure to be notified of the completion of the request.
func execute(_ request: HTTPClientRequest,
observabilityScope: ObservabilityScope?,
progress: ProgressHandler?,
completion: @escaping CompletionHandler)
}

public enum HTTPClientError: Error, Equatable {
case invalidResponse
case badResponseStatusCode(Int)
Expand All @@ -51,11 +34,13 @@ public enum HTTPClientError: Error, Equatable {

// MARK: - HTTPClient

public struct HTTPClient: HTTPClientProtocol {
public struct HTTPClient {
public typealias Configuration = HTTPClientConfiguration
public typealias Request = HTTPClientRequest
public typealias Response = HTTPClientResponse
public typealias Handler = (Request, ProgressHandler?, @escaping (Result<Response, Error>) -> Void) -> Void
public typealias ProgressHandler = (_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void
public typealias CompletionHandler = (Result<HTTPClientResponse, Error>) -> Void

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

/// Execute an HTTP request asynchronously
///
/// - Parameters:
/// - request: The `HTTPClientRequest` to perform.
/// - observabilityScope: the observability scope to emit diagnostics on
/// - progress: A progress handler to handle progress for example for downloads
/// - completion: A completion handler to be notified of the completion of the request.
public func execute(_ request: Request, observabilityScope: ObservabilityScope? = nil, progress: ProgressHandler? = nil, completion: @escaping CompletionHandler) {
// merge configuration
var request = request
Expand Down
8 changes: 8 additions & 0 deletions Sources/Basics/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ extension String {
public var sha256Checksum: String {
return SHA256().hash(self).hexadecimalRepresentation
}

/// Drops the given suffix from the string, if present.
public func spm_dropPrefix(_ prefix: String) -> String {
if self.hasPrefix(prefix) {
return String(self.dropFirst(prefix.count))
}
return self
}
}
Loading