Skip to content
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

Android support #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ jobs:
- uses: actions/checkout@v2
- name: Run Tests
run: swift test
android-run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: "Test Swift Package on Android"
uses: skiptools/swift-android-action@v2

2 changes: 1 addition & 1 deletion Sources/Get/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public actor APIClient {
}
}

#if !os(Linux)
#if !os(Linux) && !os(Android)

// MARK: Downloads

Expand Down
30 changes: 15 additions & 15 deletions Sources/Get/DataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
// MARK: - URLSessionDelegate

func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
#if os(Linux)
#if os(Linux) || os(Android)
userSessionDelegate?.urlSession(session, didBecomeInvalidWithError: error)
#else
userSessionDelegate?.urlSession?(session, didBecomeInvalidWithError: error)
#endif
}

#if !os(Linux)
#if !os(Linux) && !os(Android)
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
if #available(macOS 11.0, *) {
userSessionDelegate?.urlSessionDidFinishEvents?(forBackgroundURLSession: session)
Expand All @@ -95,7 +95,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let handler = handlers[task] else { return assertionFailure() }
handlers[task] = nil
#if os(Linux)
#if os(Linux) || os(Android)
handler.delegate?.urlSession(session, task: task, didCompleteWithError: error)
userTaskDelegate?.urlSession(session, task: task, didCompleteWithError: error)
#else
Expand Down Expand Up @@ -126,7 +126,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
let handler = handlers[task]
handler?.metrics = metrics
#if os(Linux)
#if os(Linux) || os(Android)
handler?.delegate?.urlSession(session, task: task, didFinishCollecting: metrics)
userTaskDelegate?.urlSession(session, task: task, didFinishCollecting: metrics)
#else
Expand All @@ -136,7 +136,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
}

func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
#if os(Linux)
#if os(Linux) || os(Android)
handlers[task]?.delegate?.urlSession(session, task: task, willPerformHTTPRedirection: response, newRequest: request, completionHandler: completionHandler) ??
userTaskDelegate?.urlSession(session, task: task, willPerformHTTPRedirection: response, newRequest: request, completionHandler: completionHandler) ??
completionHandler(request)
Expand All @@ -147,7 +147,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
#endif
}

#if !os(Linux)
#if !os(Linux) && !os(Android)
func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
handlers[task]?.delegate?.urlSession?(session, taskIsWaitingForConnectivity: task)
userTaskDelegate?.urlSession?(session, taskIsWaitingForConnectivity: task)
Expand All @@ -166,7 +166,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
#endif

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
#if os(Linux)
#if os(Linux) || os(Android)
handlers[task]?.delegate?.urlSession(session, task: task, didReceive: challenge, completionHandler: completionHandler) ??
userTaskDelegate?.urlSession(session, task: task, didReceive: challenge, completionHandler: completionHandler) ??
completionHandler(.performDefaultHandling, nil)
Expand All @@ -178,7 +178,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
}

func urlSession(_ session: URLSession, task: URLSessionTask, willBeginDelayedRequest request: URLRequest, completionHandler: @escaping (URLSession.DelayedRequestDisposition, URLRequest?) -> Void) {
#if os(Linux)
#if os(Linux) || os(Android)
handlers[task]?.delegate?.urlSession(session, task: task, willBeginDelayedRequest: request, completionHandler: completionHandler) ??
userTaskDelegate?.urlSession(session, task: task, willBeginDelayedRequest: request, completionHandler: completionHandler) ??
completionHandler(.continueLoading, nil)
Expand All @@ -190,7 +190,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
#if os(Linux)
#if os(Linux) || os(Android)
handlers[task]?.delegate?.urlSession(session, task: task, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend) ??
userTaskDelegate?.urlSession(session, task: task, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend)
#else
Expand All @@ -202,7 +202,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
// MARK: - URLSessionDataDelegate

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
#if os(Linux)
#if os(Linux) || os(Android)
(handlers[dataTask] as? DataTaskHandler)?.dataDelegate?.urlSession(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler) ??
userDataDelegate?.urlSession(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler) ??
completionHandler(.allow)
Expand All @@ -215,7 +215,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
guard let handler = handlers[dataTask] as? DataTaskHandler else { return }
#if os(Linux)
#if os(Linux) || os(Android)
handler.dataDelegate?.urlSession(session, dataTask: dataTask, didReceive: data)
userDataDelegate?.urlSession(session, dataTask: dataTask, didReceive: data)
#else
Expand All @@ -228,7 +228,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
handler.data!.append(data)
}

#if !os(Linux)
#if !os(Linux) && !os(Android)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome downloadTask: URLSessionDownloadTask) {
(handlers[dataTask] as? DataTaskHandler)?.dataDelegate?.urlSession?(session, dataTask: dataTask, didBecome: downloadTask)
userDataDelegate?.urlSession?(session, dataTask: dataTask, didBecome: downloadTask)
Expand All @@ -241,7 +241,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
#endif

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Void) {
#if os(Linux)
#if os(Linux) || os(Android)
(handlers[dataTask] as? DataTaskHandler)?.dataDelegate?.urlSession(session, dataTask: dataTask, willCacheResponse: proposedResponse, completionHandler: completionHandler) ??
userDataDelegate?.urlSession(session, dataTask: dataTask, willCacheResponse: proposedResponse, completionHandler: completionHandler)
completionHandler(proposedResponse)
Expand All @@ -266,7 +266,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
#if os(Linux)
#if os(Linux) || os(Android)
(handlers[downloadTask] as? DownloadTaskHandler)?.downloadDelegate?.urlSession(session, downloadTask: downloadTask, didWriteData: bytesWritten, totalBytesWritten: totalBytesWritten, totalBytesExpectedToWrite: totalBytesExpectedToWrite)
userDownloadDelegate?.urlSession(session, downloadTask: downloadTask, didWriteData: bytesWritten, totalBytesWritten: totalBytesWritten, totalBytesExpectedToWrite: totalBytesExpectedToWrite)
#else
Expand All @@ -276,7 +276,7 @@ final class DataLoader: NSObject, URLSessionDataDelegate, URLSessionDownloadDele
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
#if os(Linux)
#if os(Linux) || os(Android)
(handlers[downloadTask] as? DownloadTaskHandler)?.downloadDelegate?.urlSession(session, downloadTask: downloadTask, didResumeAtOffset: fileOffset, expectedTotalBytes: expectedTotalBytes)
userDownloadDelegate?.urlSession(session, downloadTask: downloadTask, didResumeAtOffset: fileOffset, expectedTotalBytes: expectedTotalBytes)
#else
Expand Down
6 changes: 3 additions & 3 deletions Tests/GetTests/ClientSendingRequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class ClientSendingRequestsTests: XCTestCase {
XCTAssertEqual(response.data.count, 1321)
XCTAssertEqual(response.originalRequest?.url, url)
XCTAssertEqual(response.statusCode, 200)
#if !os(Linux)
#if !os(Linux) && !os(Android)
let metrics = try XCTUnwrap(response.metrics)
let transaction = try XCTUnwrap(metrics.transactionMetrics.first)
XCTAssertEqual(transaction.request.url, URL(string: "https://api.github.com/user"))
Expand Down Expand Up @@ -306,7 +306,7 @@ final class ClientSendingRequestsTests: XCTestCase {

// MARK: - Downloads

#if !os(Linux)
#if !os(Linux) && !os(Android)
func testDownloads() async throws {
// GIVEN
let url = URL(string: "https://api.github.com/user")!
Expand Down Expand Up @@ -493,7 +493,7 @@ final class ClientSendingRequestsTests: XCTestCase {
XCTAssertEqual(response.value.login, "kean")
}

#if !os(Linux) // This doesn't work on Linux
#if !os(Linux) && !os(Android) // This doesn't work on Linux
func testSetHTTPAdditionalHeaders() async throws {
// GIVEN
client = .mock {
Expand Down
2 changes: 1 addition & 1 deletion Tests/GetTests/ClientSessionDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import XCTest
@testable import Get

#if !os(Linux)
#if !os(Linux) && !os(Android)
final class APIClientSessionDelegateTests: XCTestCase {
private let delegate = SessionDelegate()
private var client: APIClient!
Expand Down