Skip to content

Commit

Permalink
Add Linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 3, 2022
1 parent 460e022 commit edaee2c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
6 changes: 6 additions & 0 deletions Sources/Get/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions Sources/Get/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Selector>
#endif
private let loader: DataLoader

static func make(loader: DataLoader, delegate: URLSessionDelegate?) -> URLSessionDelegate {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Tests/GetTests/ClientSessionDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -48,3 +49,4 @@ private final class SessionDelegate: NSObject, URLSessionTaskDelegate {
self.metrics[task] = metrics
}
}
#endif

0 comments on commit edaee2c

Please sign in to comment.