diff --git a/Sources/Clients/AmiiboClient.swift b/Sources/Clients/AmiiboClient.swift index 95cb114..ad87242 100644 --- a/Sources/Clients/AmiiboClient.swift +++ b/Sources/Clients/AmiiboClient.swift @@ -10,16 +10,18 @@ // //===----------------------------------------------------------------------===// -#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Communications import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + struct AmiiboClient { // MARK: Properties private let session: URLSession - private let decoder = JSONDecoder() private let makeURLRequest = MakeURLRequestUseCase() @@ -48,7 +50,7 @@ extension AmiiboClient: Client { as model: Model.Type ) async throws -> Model where Model : Decodable { let urlRequest = try makeURLRequest(endpoint: endpoint) - let (data, response) = try await session.data(for: urlRequest) + let (data, response) = try await data(from: session, for: urlRequest) try check(response) @@ -62,6 +64,27 @@ extension AmiiboClient: Client { private extension AmiiboClient { // MARK: Functions + + func data( + from session: URLSession, + for urlRequest: URLRequest + ) async throws -> (Data, URLResponse) { + #if canImport(FoundationNetworking) + try await withCheckedThrowingContinuation { continuation in + session.dataTask(with: urlRequest) { data, response, error in + if let error { + continuation.resume(with: .failure(error)) + } else if let data, let response { + continuation.resume(with: .success((data, response))) + } else { + continuation.resume(with: .failure(AmiiboClientError.dataOrResponseNotFound)) + } + } + } + #else + try await session.data(for: urlRequest) + #endif + } func check(_ response: URLResponse) throws { guard @@ -77,4 +100,3 @@ private extension AmiiboClient { } } -#endif diff --git a/Sources/Errors/AmiiboClientError.swift b/Sources/Errors/AmiiboClientError.swift index b008a9d..67d5118 100644 --- a/Sources/Errors/AmiiboClientError.swift +++ b/Sources/Errors/AmiiboClientError.swift @@ -12,6 +12,8 @@ /// This error definitions represents any error happening while the client makes a request to the remote API and handles the respective response, excluding the decoding of the retrieved data into a particular model. public enum AmiiboClientError: Error { + /// The data and/or response expected from an API call are not found. + case dataOrResponseNotFound /// The status code of the response is not the expected one, which is `.ok` (`200`). case responseCode(Int) /// The status code of the response was not received at all. diff --git a/Sources/Services/AmiiboService.swift b/Sources/Services/AmiiboService.swift index 89d45af..d50d608 100644 --- a/Sources/Services/AmiiboService.swift +++ b/Sources/Services/AmiiboService.swift @@ -10,9 +10,12 @@ // //===----------------------------------------------------------------------===// -#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + /// This service provides the interface to make remote API calls to the [Amiibo API](https://www.amiiboapi.com) and, subsequently, handle its responses. /// /// Given that this remote service is a read-only API, this service will exclusively return decoded models or entities in cases the requests are successful, or it will throw errors otherwise. @@ -125,5 +128,4 @@ extension AmiiboService: Service { ).timestamp } -} -#endif +} \ No newline at end of file diff --git a/Tests/Clients/AmiiboClientTests.swift b/Tests/Clients/AmiiboClientTests.swift index 773e091..ea88a62 100644 --- a/Tests/Clients/AmiiboClientTests.swift +++ b/Tests/Clients/AmiiboClientTests.swift @@ -10,11 +10,14 @@ // //===----------------------------------------------------------------------===// -#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Communications import Foundation import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + @testable import AmiiboService final class AmiiboClientTests: XCTestCase { @@ -156,4 +159,3 @@ final class AmiiboClientTests: XCTestCase { } } -#endif diff --git a/Tests/Helpers/Extensions/MockURLRequest+Init.swift b/Tests/Helpers/Extensions/MockURLRequest+Init.swift index e34dc66..6c9c682 100644 --- a/Tests/Helpers/Extensions/MockURLRequest+Init.swift +++ b/Tests/Helpers/Extensions/MockURLRequest+Init.swift @@ -10,10 +10,13 @@ // //===----------------------------------------------------------------------===// -#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Communications import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + extension MockURLRequest { // MARK: Initialisers @@ -23,4 +26,3 @@ extension MockURLRequest { } } -#endif diff --git a/Tests/Services/AmiiboServiceTests.swift b/Tests/Services/AmiiboServiceTests.swift index 48ea789..c71626e 100644 --- a/Tests/Services/AmiiboServiceTests.swift +++ b/Tests/Services/AmiiboServiceTests.swift @@ -10,11 +10,14 @@ // //===----------------------------------------------------------------------===// -#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) import Communications import Foundation import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + @testable import AmiiboService final class AmiiboServiceTests: XCTestCase { @@ -522,4 +525,3 @@ final class AmiiboServiceTests: XCTestCase { } } -#endif