diff --git a/Package.swift b/Package.swift index 253dbbb..d8eb681 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.7 +// swift-tools-version:5.5 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/Sources/Netswift/Core/HTTPPerformer.swift b/Sources/Netswift/Core/HTTPPerformer.swift index 232ff5e..6ee2aec 100644 --- a/Sources/Netswift/Core/HTTPPerformer.swift +++ b/Sources/Netswift/Core/HTTPPerformer.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// An HTTP Performer works with low-level URLRequests and validates the response's status code public protocol HTTPPerformer { diff --git a/Sources/Netswift/Core/NetswiftError/NetswiftError.swift b/Sources/Netswift/Core/NetswiftError/NetswiftError.swift index 1ab8309..51920c3 100644 --- a/Sources/Netswift/Core/NetswiftError/NetswiftError.swift +++ b/Sources/Netswift/Core/NetswiftError/NetswiftError.swift @@ -120,7 +120,7 @@ private extension Data { return "Invalid JSON\n\(plainString ?? plainDescription)" } return prettyString - } else if let plainString { + } else if let plainString = plainString { return plainString } else { return plainDescription diff --git a/Sources/Netswift/Core/NetswiftHTTPResponse.swift b/Sources/Netswift/Core/NetswiftHTTPResponse.swift index 814b93b..ec91655 100644 --- a/Sources/Netswift/Core/NetswiftHTTPResponse.swift +++ b/Sources/Netswift/Core/NetswiftHTTPResponse.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// Convenience wrapper for URLResponses public struct NetswiftHTTPResponse { diff --git a/Sources/Netswift/Core/NetswiftHeaders.swift b/Sources/Netswift/Core/NetswiftHeaders.swift index 9a41b87..179f421 100644 --- a/Sources/Netswift/Core/NetswiftHeaders.swift +++ b/Sources/Netswift/Core/NetswiftHeaders.swift @@ -61,8 +61,10 @@ public struct NetswiftHeaders { var all = additionalHeaders all.update(with: .accept(accept)) all.update(with: .contentType(contentType)) - if let authorization { all.update(with: .authorization(authorization)) } - + if let authorization = authorization { + all.update(with: .authorization(authorization)) + } + return all } } diff --git a/Sources/Netswift/Core/NetswiftNetworkPerformer.swift b/Sources/Netswift/Core/NetswiftNetworkPerformer.swift index 0c50460..060d567 100644 --- a/Sources/Netswift/Core/NetswiftNetworkPerformer.swift +++ b/Sources/Netswift/Core/NetswiftNetworkPerformer.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /** A `NetswiftNetworkPerformer` is the link between low-level url requests and high-level api requests diff --git a/Sources/Netswift/Core/NetswiftRequest.swift b/Sources/Netswift/Core/NetswiftRequest.swift index 1b0a59f..023d2e1 100644 --- a/Sources/Netswift/Core/NetswiftRequest.swift +++ b/Sources/Netswift/Core/NetswiftRequest.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// Generic structure that defines its own Response type. Comes with a few convenience extensions for common types such as JSON public protocol NetswiftRequest { diff --git a/Sources/Netswift/Core/NetswiftRoute.swift b/Sources/Netswift/Core/NetswiftRoute.swift index 972de75..cb70784 100644 --- a/Sources/Netswift/Core/NetswiftRoute.swift +++ b/Sources/Netswift/Core/NetswiftRoute.swift @@ -8,6 +8,9 @@ // swiftlint:disable force_unwrapping import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /** Protocol defining URL Routes for APIs. diff --git a/Sources/Netswift/Core/NetswiftSession.swift b/Sources/Netswift/Core/NetswiftSession.swift index bf50fab..145584c 100644 --- a/Sources/Netswift/Core/NetswiftSession.swift +++ b/Sources/Netswift/Core/NetswiftSession.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// Protocol that allows to mock URLSessions public protocol NetswiftSession { @@ -31,14 +34,35 @@ extension URLSession: NetswiftSession { return task } + #if os(Linux) /// Asynchronous data call made via NetswiftSession Protocol @available(iOS 13.0, tvOS 13.0, watchOS 6.0, *) public func perform(_ urlRequest: URLRequest) async -> NetswiftHTTPResponse { do { - let (data, response) = try await data(for: urlRequest) + let response: (Data?, URLResponse?) = try await withCheckedThrowingContinuation { continuation in + dataTask(with: urlRequest) { data, response, error in + if let error = error { + continuation.resume(throwing: error) + } + continuation.resume(returning: (data, response)) + } + } + + return NetswiftHTTPResponse(data: response.0, response: response.1) + } catch { + return NetswiftHTTPResponse(data: nil, response: nil, error: error) + } + } + #else + /// Asynchronous data call made via NetswiftSession Protocol + @available(iOS 13.0, tvOS 13.0, watchOS 6.0, *) + public func perform(_ urlRequest: URLRequest) async -> NetswiftHTTPResponse { + do { + let (data, response) = try await self.data(for: urlRequest) return NetswiftHTTPResponse(data: data, response: response) } catch { return NetswiftHTTPResponse(data: nil, response: nil, error: error) } } + #endif } diff --git a/Sources/Netswift/Core/NetswiftTask.swift b/Sources/Netswift/Core/NetswiftTask.swift index 0284a18..de65d32 100644 --- a/Sources/Netswift/Core/NetswiftTask.swift +++ b/Sources/Netswift/Core/NetswiftTask.swift @@ -6,6 +6,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif public protocol NetswiftTask { diff --git a/Sources/Netswift/Core/URLRequestExtension.swift b/Sources/Netswift/Core/URLRequestExtension.swift index 942b50c..78f5afb 100644 --- a/Sources/Netswift/Core/URLRequestExtension.swift +++ b/Sources/Netswift/Core/URLRequestExtension.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// Convenience wrapper functions public extension URLRequest { diff --git a/Sources/Netswift/NetswiftHTTPPerformer.swift b/Sources/Netswift/NetswiftHTTPPerformer.swift index 185c167..673168d 100644 --- a/Sources/Netswift/NetswiftHTTPPerformer.swift +++ b/Sources/Netswift/NetswiftHTTPPerformer.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// A generic HTTP Performer. For detailed doc please refer to HTTPPerformer protocol open class NetswiftHTTPPerformer: HTTPPerformer { diff --git a/Sources/Netswift/NetswiftPerformer.swift b/Sources/Netswift/NetswiftPerformer.swift index f8ae87f..5bd7a53 100644 --- a/Sources/Netswift/NetswiftPerformer.swift +++ b/Sources/Netswift/NetswiftPerformer.swift @@ -7,6 +7,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif /// Generic NetswiftRequest performer. For detailed doc please refer to NetswiftNetworkPerformer protocol open class NetswiftPerformer: NetswiftNetworkPerformer {