From edaee2c2f8e174cb7ec7cad6d555646e24849824 Mon Sep 17 00:00:00 2001 From: kean Date: Sun, 3 Apr 2022 17:13:09 -0400 Subject: [PATCH] Add Linux support --- Sources/Get/APIClient.swift | 6 ++++++ Sources/Get/Helpers.swift | 20 ++----------------- .../GetTests/ClientSessionDelegateTests.swift | 2 ++ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/Sources/Get/APIClient.swift b/Sources/Get/APIClient.swift index 8334a08..dded82f 100644 --- a/Sources/Get/APIClient.swift +++ b/Sources/Get/APIClient.swift @@ -32,8 +32,10 @@ public actor APIClient { public var encoder: JSONEncoder? /// The (optional) client delegate. public var delegate: APIClientDelegate? +#if !os(Linux) /// The (optional) URLSession delegate that allows you to monitor the underlying URLSession. public var sessionDelegate: URLSessionDelegate? +#endif public init(host: String, sessionConfiguration: URLSessionConfiguration = .default, delegate: APIClientDelegate? = nil) { self.host = host @@ -56,7 +58,11 @@ public actor APIClient { public init(configuration: Configuration) { self.conf = configuration let queue = OperationQueue(maxConcurrentOperationCount: 1) +#if !os(Linux) let delegate = URLSessionProxyDelegate.make(loader: loader, delegate: configuration.sessionDelegate) +#else + let delegate = loader +#endif self.session = URLSession(configuration: configuration.sessionConfiguration, delegate: delegate, delegateQueue: queue) self.delegate = configuration.delegate ?? DefaultAPIClientDelegate() self.serializer = Serializer(decoder: configuration.decoder, encoder: configuration.encoder) diff --git a/Sources/Get/Helpers.swift b/Sources/Get/Helpers.swift index c67a6e1..35bb0c1 100644 --- a/Sources/Get/Helpers.swift +++ b/Sources/Get/Helpers.swift @@ -133,13 +133,12 @@ final class DataLoader: NSObject, URLSessionDataDelegate { } } +#if !os(Linux) /// Allows users to monitor URLSession. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) final class URLSessionProxyDelegate: NSObject, URLSessionTaskDelegate, URLSessionDataDelegate { private var delegate: URLSessionDelegate - #if !os(Linux) private let interceptedSelectors: Set - #endif private let loader: DataLoader static func make(loader: DataLoader, delegate: URLSessionDelegate?) -> URLSessionDelegate { @@ -150,47 +149,32 @@ final class URLSessionProxyDelegate: NSObject, URLSessionTaskDelegate, URLSessio init(loader: DataLoader, delegate: URLSessionDelegate) { self.loader = loader self.delegate = delegate - #if !os(Linux) self.interceptedSelectors = [ #selector(URLSessionDataDelegate.urlSession(_:dataTask:didReceive:)), #selector(URLSessionTaskDelegate.urlSession(_:task:didCompleteWithError:)), #selector(URLSessionTaskDelegate.urlSession(_:task:didFinishCollecting:)) ] - #endif } // MARK: URLSessionDelegate func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { loader.urlSession(session, dataTask: dataTask, didReceive: data) - #if !os(Linux) (delegate as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: data) - #else - (delegate as? URLSessionDataDelegate)?.urlSession(session, dataTask: dataTask, didReceive: data) - #endif } func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { loader.urlSession(session, task: task, didCompleteWithError: error) - #if !os(Linux) (delegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didCompleteWithError: error) - #else - (delegate as? URLSessionTaskDelegate)?.urlSession(session, task: task, didCompleteWithError: error) - #endif } func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) { loader.urlSession(session, task: task, didFinishCollecting: metrics) - #if !os(Linux) (delegate as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didFinishCollecting: metrics) - #else - (delegate as? URLSessionTaskDelegate)?.urlSession(session, task: task, didFinishCollecting: metrics) - #endif } // MARK: Proxy - #if !os(Linux) override func responds(to aSelector: Selector!) -> Bool { if interceptedSelectors.contains(aSelector) { return true @@ -201,8 +185,8 @@ final class URLSessionProxyDelegate: NSObject, URLSessionTaskDelegate, URLSessio override func forwardingTarget(for selector: Selector!) -> Any? { interceptedSelectors.contains(selector) ? nil : delegate } - #endif } +#endif extension OperationQueue { convenience init(maxConcurrentOperationCount: Int) { diff --git a/Tests/GetTests/ClientSessionDelegateTests.swift b/Tests/GetTests/ClientSessionDelegateTests.swift index 460994c..add6f29 100644 --- a/Tests/GetTests/ClientSessionDelegateTests.swift +++ b/Tests/GetTests/ClientSessionDelegateTests.swift @@ -6,6 +6,7 @@ import XCTest import Mocker @testable import Get +#if !os(Linux) final class APIClientSessionDelegateTests: XCTestCase { var client: APIClient! private var delegate: SessionDelegate! @@ -48,3 +49,4 @@ private final class SessionDelegate: NSObject, URLSessionTaskDelegate { self.metrics[task] = metrics } } +#endif