Skip to content

Commit

Permalink
Remove logger
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung committed May 21, 2024
1 parent 98de24f commit 9684f9a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
56 changes: 26 additions & 30 deletions Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Check warning on line 104 in Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift#L104

Added line #L104 was not covered by tests
}

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

Check warning on line 123 in Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Adapters/URLSessionHTTPClientAdapter.swift#L123

Added line #L123 was not covered by tests
}

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
10 changes: 5 additions & 5 deletions Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ class CodableAPIRequest<Body: Encodable & Equatable, Response: Decodable>: APIRe

static func == (lhs: CodableAPIRequest<Body, Response>, rhs: CodableAPIRequest<Body, Response>) -> 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
}

}
18 changes: 15 additions & 3 deletions Tests/TMDbTests/Networking/HTTPClient/HTTPRequestTests.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9684f9a

Please sign in to comment.