Skip to content

Support platforms back to macOS 10.15, iOS 13, tvOS 13, and watchOS 6 #7

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 8 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 16 additions & 14 deletions Sources/OpenAPIURLSession/URLSessionTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,24 @@ public struct URLSessionTransport: ClientTransport {
}

private func invokeSession(_ urlRequest: URLRequest) async throws -> (Data, URLResponse) {
#if canImport(FoundationNetworking)
return try await performDataTask(with: urlRequest)
#else
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
return try await configuration.session.data(for: urlRequest)
} else {
return try await performDataTask(with: urlRequest)
}
#endif
}

private func performDataTask(with urlRequest: URLRequest) async throws -> (Data, URLResponse) {
// 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()
Expand All @@ -126,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 @@ -178,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 ?? "")"
}
}
}