diff --git a/Package.swift b/Package.swift index 70fae98..ef018de 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ import PackageDescription let package = Package( name: "swift-openapi-urlsession", platforms: [ - .macOS(.v13), .iOS(.v16), .tvOS(.v16), .watchOS(.v9), + .macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), ], products: [ .library( @@ -27,7 +27,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.0")), + .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.3")), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ diff --git a/README.md b/README.md index 9b7af80..9468934 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ A client transport that uses the [URLSession](https://developer.apple.com/docume Use the transport with client code generated by [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). +## Supported platforms and minimum versions + | macOS | Linux | iOS | tvOS | watchOS | + | :-: | :-: | :-: | :-: | :-: | + | ✅ 10.15+ | ✅ | ✅ 13+ | ✅ 13+ | ✅ 6+ | + ## Usage Add the package dependency in your `Package.swift`: diff --git a/Sources/OpenAPIURLSession/Documentation.docc/Documentation.md b/Sources/OpenAPIURLSession/Documentation.docc/Documentation.md index 1dfa523..fcccf32 100644 --- a/Sources/OpenAPIURLSession/Documentation.docc/Documentation.md +++ b/Sources/OpenAPIURLSession/Documentation.docc/Documentation.md @@ -8,6 +8,11 @@ A client transport that uses the [URLSession](https://developer.apple.com/docume Use the transport with client code generated by [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). +### Supported platforms and minimum versions +| macOS | Linux | iOS | tvOS | watchOS | +| :-: | :-: | :-: | :-: | :-: | +| ✅ 10.15+ | ✅ | ✅ 13+ | ✅ 13+ | ✅ 6+ | + ### Usage Add the package dependency in your `Package.swift`: diff --git a/Sources/OpenAPIURLSession/URLSessionTransport.swift b/Sources/OpenAPIURLSession/URLSessionTransport.swift index eadddb6..d3780dc 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -90,23 +90,28 @@ public struct URLSessionTransport: ClientTransport { } private func invokeSession(_ urlRequest: URLRequest) async throws -> (Data, URLResponse) { - #if canImport(FoundationNetworking) + // Using `dataTask(with:completionHandler:)` instead of the async method `data(for:)` of URLSession because the latter is not available on linux platforms return try await withCheckedThrowingContinuation { continuation in configuration.session .dataTask(with: urlRequest) { data, response, error in - if let error = error { + if let error { continuation.resume(with: .failure(error)) return } + + guard let response else { + continuation.resume( + with: .failure(URLSessionTransportError.noResponse(url: urlRequest.url)) + ) + return + } + continuation.resume( - with: .success((data ?? Data(), response!)) + with: .success((data ?? Data(), response)) ) } .resume() } - #else - return try await configuration.session.data(for: urlRequest) - #endif } } @@ -118,6 +123,9 @@ internal enum URLSessionTransportError: Error { /// Returned `URLResponse` could not be converted to `HTTPURLResponse`. case notHTTPResponse(URLResponse) + + /// Returned `URLResponse` was nil + case noResponse(url: URL?) } extension OpenAPIRuntime.Response { @@ -170,6 +178,8 @@ extension URLSessionTransportError: CustomStringConvertible { "Invalid request URL from request path: \(request.path), query: \(request.query ?? "") relative to base URL: \(baseURL.absoluteString)" case .notHTTPResponse(let response): return "Received a non-HTTP response, of type: \(String(describing: type(of: response)))" + case .noResponse(let url): + return "Received a nil response for \(url?.absoluteString ?? "")" } } }