-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c54bac
commit ce6de4d
Showing
11 changed files
with
171 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
import OSLog | ||
|
||
//MARK: Logging | ||
|
||
extension APIClient { | ||
|
||
func logRequest(request: URLRequest) { | ||
let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Request") | ||
switch loggingConfiguration.requestBehaviour { | ||
case .all: | ||
let httpMethod = request.httpMethod ?? "GET" | ||
let path = request.url?.path ?? "" | ||
logger.debug("Method: \(httpMethod) Path: \(path)") | ||
if let data = request.httpBody, let prettyString = String(data: data, encoding: .utf8) { | ||
logger.debug("Body: \(prettyString)") | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
|
||
func logResponse(_ response: Response) { | ||
let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Response") | ||
let isError = !(200..<300).contains(response.httpResponse.statusCode) | ||
let shouldLogThis: Bool = { | ||
switch loggingConfiguration.responseBehaviour { | ||
case .all: | ||
return true | ||
case .none: | ||
return false | ||
case .onlyFailing: | ||
return isError | ||
} | ||
}() | ||
guard shouldLogThis else { return } | ||
let logType: OSLogType = isError ? .error : .debug | ||
let path = response.httpResponse.url?.path ?? "" | ||
logger.log(level: logType, "StatusCode: \(response.httpResponse.statusCode) Path: \(path)") | ||
if isError, let errorString = String(data: response.data, encoding: .utf8) { | ||
logger.log(level: logType, "Error Message: \(errorString)") | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Sources/BSWFoundation/APIClient/APIClient+URLSession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Created by Pierluigi Cifani on 8/1/25. | ||
// | ||
|
||
import Foundation | ||
|
||
//MARK: APIClientNetworkFetcher | ||
|
||
extension URLSession: APIClientNetworkFetcher { | ||
|
||
public func fetchData(with urlRequest: URLRequest) async throws -> APIClient.Response { | ||
let tuple = try await data(for: urlRequest) | ||
guard let httpResponse = tuple.1 as? HTTPURLResponse else { | ||
throw APIClient.Error.malformedResponse | ||
} | ||
return .init(data: tuple.0, httpResponse: httpResponse) | ||
} | ||
|
||
public func uploadFile(with urlRequest: URLRequest, fileURL: URL) async throws -> APIClient.Response { | ||
let task = Task.detached { | ||
try await self.upload(for: urlRequest, fromFile: fileURL) | ||
} | ||
let cancelTask: @Sendable () -> () = { | ||
task.cancel() | ||
} | ||
let wrapper = APIClient.ApplicationWrapper() | ||
let backgroundTaskID = await wrapper.generateBackgroundTaskID(cancelTask: cancelTask) | ||
let (data, response) = try await task.value | ||
await wrapper.endBackgroundTask(id: backgroundTaskID) | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw APIClient.Error.malformedResponse | ||
} | ||
return .init(data: data, httpResponse: httpResponse) | ||
} | ||
} | ||
|
||
public typealias HTTPHeaders = [String: String] | ||
public struct VoidResponse: Decodable, Hashable, Sendable {} | ||
|
||
// MARK: UIApplicationWrapper | ||
/// This is here just to make sure that on non-UIKit | ||
/// platforms we have a nice API to call to. | ||
#if canImport(UIKit) | ||
import UIKit | ||
private extension APIClient { | ||
class ApplicationWrapper { | ||
func generateBackgroundTaskID(cancelTask: @escaping (@MainActor @Sendable () -> Void)) async -> UIBackgroundTaskIdentifier { | ||
return await UIApplication.shared.beginBackgroundTask(expirationHandler: cancelTask) | ||
} | ||
|
||
func endBackgroundTask(id: UIBackgroundTaskIdentifier) async { | ||
await UIApplication.shared.endBackgroundTask(id) | ||
} | ||
} | ||
} | ||
#else | ||
private extension APIClient { | ||
class ApplicationWrapper { | ||
func generateBackgroundTaskID(cancelTask: @escaping (@MainActor @Sendable () -> Void)) async -> Int { | ||
return 0 | ||
} | ||
|
||
func endBackgroundTask(id: Int) async { | ||
|
||
} | ||
} | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
Sources/BSWFoundation/PropertyWrappers/CodableKeychainBacked.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.