Skip to content

Commit

Permalink
Add HTTPClientProtocol to allow easier mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Dec 10, 2024
1 parent c4f089a commit abfe62b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/SpellbookHTTP/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public protocol HTTPClientProtocol {
func data(for request: () throws -> URLRequest, delegate: URLSessionTaskDelegate?) async throws -> HTTPResult<Data>
}

@available(macOS 12.0, iOS 15, tvOS 15.0, watchOS 8.0, *)
extension HTTPClientProtocol {
public func data(for request: () throws -> URLRequest, completion: @escaping (Result<HTTPResult<Data>, any Error>) -> Void) {
let request = Result(catching: request)
Task {
do {
let result = try await data(for: request.get, delegate: nil)
completion(.success(result))
} catch {
completion(.failure(error))
}
}
}
}

open class HTTPClient: HTTPClientProtocol {
private var additionalHeaders = Synchronized<HTTPParameters<HTTPHeader>>(.unfair, .init())
private let session: URLSession
Expand Down

0 comments on commit abfe62b

Please sign in to comment.