Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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/andrewse02/swift-openapi-runtime", branch: "ae/reduce-deployment-targets"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
22 changes: 16 additions & 6 deletions Sources/OpenAPIURLSession/URLSessionTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -170,6 +178,8 @@ extension URLSessionTransportError: CustomStringConvertible {
"Invalid request URL from request path: \(request.path), query: \(request.query ?? "<nil>") 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 ?? "")"
}
}
}