Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.
http: throw on unsuccessful responses (#246)
Rob76b005f6d817
diff --git a/Sources/XKit/HTTPClientProtocol/HTTPClientProtocol.swift b/Sources/XKit/HTTPClientProtocol/HTTPClientProtocol.swiftindex 2e90522..a8dc664 100644--- a/Sources/XKit/HTTPClientProtocol/HTTPClientProtocol.swift+++ b/Sources/XKit/HTTPClientProtocol/HTTPClientProtocol.swift@@ -41,28 +41,72 @@ extension HTTPClientProtocol {public func makeRequest(_ request: HTTPRequest,body: Data? = nil,+ requireHTTPSuccess: Bool = true,onProgress: @isolated(any) (Double?) -> Void = { _ in }) async throws -> (response: HTTPResponse, body: Data) {await onProgress(0)- let (response, body) = try await send(request, body: body.map { HTTPBody($0) })- guard let body else {- return (response, Data())+ let (response, responseBody) = try await send(request, body: body.map { HTTPBody($0) })+ guard !requireHTTPSuccess || response.status.kind == .successful else {+ let errorBody = (try? await responseBody.collect()) ?? Data()+ throw HTTPResponseError(+ method: request.method,+ url: "\(request.scheme ?? "https")://\(request.authority ?? "")\(request.path ?? "")",+ status: response.status,+ // we don't use `decoding:as:` because we want to validate the UTF8+ body: String(data: errorBody, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)+ )}- switch body.length {+ return (response, try await responseBody.collect(onProgress: onProgress))+ }+}++extension HTTPBody? {+ fileprivate func collect(+ onProgress: @isolated(any) (Double?) -> Void = { _ in }+ ) async throws -> Data {+ try await self?.collect(onProgress: onProgress) ?? Data()+ }+}++extension HTTPBody {+ fileprivate func collect(+ onProgress: @isolated(any) (Double?) -> Void+ ) async throws -> Data {+ switch self.length {case .unknown:- return (response, try await body.reduce(into: Data()) { $0 += $1 })+ return try await self.reduce(into: Data()) { $0 += $1 }case .known(let length):var data = Data(capacity: Int(length))let total = Double(length)- for try await chunk in body {+ for try await chunk in self {data += chunk- await onProgress(min(Double(data.count) / total, 1))+ await onProgress(Swift.min(Double(data.count) / total, 1))}- return (response, data)+ return data}}}+public struct HTTPResponseError: Error, LocalizedError, Sendable, CustomStringConvertible {+ public let method: HTTPRequest.Method+ public let url: String+ public let status: HTTPResponse.Status+ public let body: String?++ public var description: String {+ var description = "\(method.rawValue) \(url) failed: HTTP \(status.code)"+ if !status.reasonPhrase.isEmpty {+ description += " \(status.reasonPhrase)"+ }+ if let body, !body.isEmpty {+ description += ". Details:\n\(body)"+ }+ return description+ }++ public var errorDescription: String? { description }+}+private struct UnimplementedHTTPClient: HTTPClientProtocol, ClientTransport {public var asOpenAPITransport: ClientTransport { self }