From 1ec2debf1853065754a526255d22da31cc65ace2 Mon Sep 17 00:00:00 2001 From: Denil Chungath <95201442+denil-ct@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:28:09 +0530 Subject: [PATCH 1/8] Update URLSessionTransport.swift --- .../OpenAPIURLSession/URLSessionTransport.swift | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/OpenAPIURLSession/URLSessionTransport.swift b/Sources/OpenAPIURLSession/URLSessionTransport.swift index eadddb6..8e78b10 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -90,7 +90,18 @@ public struct URLSessionTransport: ClientTransport { } private func invokeSession(_ urlRequest: URLRequest) async throws -> (Data, URLResponse) { - #if canImport(FoundationNetworking) + #if canImport(FoundationNetworking) + return try await performDataTask(with: urlRequest) + #else + if #available(iOS 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) { return try await withCheckedThrowingContinuation { continuation in configuration.session .dataTask(with: urlRequest) { data, response, error in @@ -104,9 +115,6 @@ public struct URLSessionTransport: ClientTransport { } .resume() } - #else - return try await configuration.session.data(for: urlRequest) - #endif } } From 6e6c3ef413e8a75e202ddbb5fbc7a463f9327a61 Mon Sep 17 00:00:00 2001 From: Denil Chungath <95201442+denil-ct@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:29:00 +0530 Subject: [PATCH 2/8] Update Package.swift --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 70fae98..0a68701 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(.v11), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), ], products: [ .library( From fdb76266f0dbe6d2201f664d1945e4f669224eec Mon Sep 17 00:00:00 2001 From: Denil Chungath <95201442+denil-ct@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:46:46 +0530 Subject: [PATCH 3/8] Update Package.swift --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 0a68701..92b3f6c 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ import PackageDescription let package = Package( name: "swift-openapi-urlsession", platforms: [ - .macOS(.v11), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), + .macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), ], products: [ .library( From 3147bc8862da95631c99e2d29c1c2abddbee5246 Mon Sep 17 00:00:00 2001 From: Denil Chungath <95201442+denil-ct@users.noreply.github.com> Date: Tue, 13 Jun 2023 02:29:42 +0530 Subject: [PATCH 4/8] Add all platforms to available check --- Sources/OpenAPIURLSession/URLSessionTransport.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OpenAPIURLSession/URLSessionTransport.swift b/Sources/OpenAPIURLSession/URLSessionTransport.swift index 8e78b10..8d17e45 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -93,7 +93,7 @@ public struct URLSessionTransport: ClientTransport { #if canImport(FoundationNetworking) return try await performDataTask(with: urlRequest) #else - if #available(iOS 15.0, *) { + 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) From 21df998dc15e507e841f30fe544eaf75b8a47687 Mon Sep 17 00:00:00 2001 From: Denil Chungath Date: Tue, 13 Jun 2023 14:07:27 +0530 Subject: [PATCH 5/8] Implement PR suggestions --- Package.swift | 2 +- README.md | 5 ++++ .../URLSessionTransport.swift | 30 ++++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Package.swift b/Package.swift index 92b3f6c..bc7230a 100644 --- a/Package.swift +++ b/Package.swift @@ -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: [ 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/URLSessionTransport.swift b/Sources/OpenAPIURLSession/URLSessionTransport.swift index 8d17e45..41eface 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -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() @@ -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 { @@ -178,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 ?? "")" } } } From 78a675d406bccdbf27489eb11ad74ff1030e3173 Mon Sep 17 00:00:00 2001 From: Denil Chungath Date: Tue, 13 Jun 2023 14:44:37 +0530 Subject: [PATCH 6/8] Address Suggestions --- .../OpenAPIURLSession/Documentation.docc/Documentation.md | 5 +++++ Sources/OpenAPIURLSession/URLSessionTransport.swift | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) 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 41eface..d3780dc 100644 --- a/Sources/OpenAPIURLSession/URLSessionTransport.swift +++ b/Sources/OpenAPIURLSession/URLSessionTransport.swift @@ -179,7 +179,7 @@ extension URLSessionTransportError: CustomStringConvertible { 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 ?? "")" + return "Received a nil response for \(url?.absoluteString ?? "")" } } } From 7544bb69b5387edc6e4730f7505a7a86764c7525 Mon Sep 17 00:00:00 2001 From: Denil Chungath Date: Tue, 13 Jun 2023 17:08:08 +0530 Subject: [PATCH 7/8] update package --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index bc7230a..92b3f6c 100644 --- a/Package.swift +++ b/Package.swift @@ -27,7 +27,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/andrewse02/swift-openapi-runtime", branch: "ae/reduce-deployment-targets"), + .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.0")), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ From 0bedb7532e6c4c31648eb5306b8147df26389bf2 Mon Sep 17 00:00:00 2001 From: Denil Chungath <95201442+denil-ct@users.noreply.github.com> Date: Tue, 13 Jun 2023 17:32:58 +0530 Subject: [PATCH 8/8] Update runtime minimum version to 0.1.3 --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 92b3f6c..ef018de 100644 --- a/Package.swift +++ b/Package.swift @@ -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: [