diff --git a/Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift b/Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift index 6946a1e0..dd6abb54 100644 --- a/Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift +++ b/Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift @@ -18,15 +18,12 @@ // import Foundation -import os #if canImport(FoundationNetworking) import FoundationNetworking #endif final class URLSessionHTTPClientAdapter: HTTPClient { - private static let logger = Logger(subsystem: "uk.co.adam-young.TMDb", category: "URLSessionHTTPClientAdapter") - private let urlSession: URLSession init(urlSession: URLSession) { @@ -83,7 +80,7 @@ extension URLSessionHTTPClientAdapter { extension URLSessionHTTPClientAdapter { #if canImport(FoundationNetworking) - private func perform(_ urlRequest: URLRequest, ignoringCache: Bool) async throws -> (Data, URLResponse) { + private func perform(_ urlRequest: URLRequest, ignoringCache _: Bool) async throws -> (Data, URLResponse) { try await withCheckedThrowingContinuation { continuation in urlSession.dataTask(with: urlRequest) { data, response, error in if let error { @@ -102,42 +99,41 @@ extension URLSessionHTTPClientAdapter { } } #else - private func perform(_ urlRequest: URLRequest, ignoringCache: Bool) async throws -> (Data, URLResponse) { - if !ignoringCache, let cachedResponse = cachedResponse(for: urlRequest) { - print("Cache HIT") - return (cachedResponse.data, cachedResponse.response) - } + private func perform(_ urlRequest: URLRequest, ignoringCache: Bool) async throws -> (Data, URLResponse) { + if !ignoringCache, let cachedResponse = cachedResponse(for: urlRequest) { + return (cachedResponse.data, cachedResponse.response) + } - let (data, response) = try await urlSession.data(for: urlRequest) - if !ignoringCache, Self.shouldCacheResponse(response) { - cacheResponse(response, data: data, for: urlRequest) - } + let (data, response) = try await urlSession.data(for: urlRequest) + if !ignoringCache, Self.shouldCacheResponse(response) { + cacheResponse(response, data: data, for: urlRequest) + } - return (data, response) - } -#endif + return (data, response) + } + #endif } #if !canImport(FoundationNetworking) -extension URLSessionHTTPClientAdapter { + extension URLSessionHTTPClientAdapter { + + private static func shouldCacheResponse(_ response: URLResponse) -> Bool { + guard let httpResponse = response as? HTTPURLResponse else { + return false + } - private static func shouldCacheResponse(_ response: URLResponse) -> Bool { - guard let httpResponse = response as? HTTPURLResponse else { - return false + return (200 ... 399).contains(httpResponse.statusCode) } - return (200...399).contains(httpResponse.statusCode) - } + private func cachedResponse(for request: URLRequest) -> CachedURLResponse? { + urlSession.configuration.urlCache?.cachedResponse(for: request) + } - private func cachedResponse(for request: URLRequest) -> CachedURLResponse? { - urlSession.configuration.urlCache?.cachedResponse(for: request) - } + private func cacheResponse(_ response: URLResponse, data: Data, for request: URLRequest) { + let cachedResponse = CachedURLResponse(response: response, data: data) + urlSession.configuration.urlCache?.storeCachedResponse(cachedResponse, for: request) + } - private func cacheResponse(_ response: URLResponse, data: Data, for request: URLRequest) { - let cachedResponse = CachedURLResponse(response: response, data: data) - urlSession.configuration.urlCache?.storeCachedResponse(cachedResponse, for: request) } - -} #endif diff --git a/Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift b/Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift index 4eb6c8e4..4b6665ab 100644 --- a/Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift +++ b/Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift @@ -70,11 +70,11 @@ class CodableAPIRequest: APIRe static func == (lhs: CodableAPIRequest, rhs: CodableAPIRequest) -> Bool { lhs.path == rhs.path - && lhs.queryItems == rhs.queryItems - && lhs.method == rhs.method - && lhs.headers == rhs.headers - && lhs.body == rhs.body - && lhs.ignoresCache == rhs.ignoresCache + && lhs.queryItems == rhs.queryItems + && lhs.method == rhs.method + && lhs.headers == rhs.headers + && lhs.body == rhs.body + && lhs.ignoresCache == rhs.ignoresCache } } diff --git a/Tests/TMDbTests/Networking/HTTPClient/HTTPRequestTests.swift b/Tests/TMDbTests/Networking/HTTPClient/HTTPRequestTests.swift index 55ced995..97eb78b3 100644 --- a/Tests/TMDbTests/Networking/HTTPClient/HTTPRequestTests.swift +++ b/Tests/TMDbTests/Networking/HTTPClient/HTTPRequestTests.swift @@ -1,8 +1,20 @@ // -// File.swift -// +// HTTPRequestTests.swift +// TMDb // -// Created by Adam Young on 17/05/2024. +// Copyright © 2024 Adam Young. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an AS IS BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. // @testable import TMDb diff --git a/Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift b/Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift index 683396ce..5da8cde6 100644 --- a/Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift +++ b/Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift @@ -29,11 +29,11 @@ final class MockURLProtocol: URLProtocol, @unchecked Sendable { @MainActor static var responseStatusCode: Int? @MainActor private(set) static var lastRequest: URLRequest? - override class func canInit(with _: URLRequest) -> Bool { + override static func canInit(with _: URLRequest) -> Bool { true } - override class func canonicalRequest(for request: URLRequest) -> URLRequest { + override static func canonicalRequest(for request: URLRequest) -> URLRequest { Task { await MainActor.run { lastRequest = request