Skip to content

Commit

Permalink
chore: expose response provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gajddo00 committed Feb 3, 2025
1 parent d019c8e commit b445b06
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
18 changes: 18 additions & 0 deletions Networking.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
"version" : "1.2.3"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "85e4bb4e1cd62cec64a4b8e769dcefdf0c5b9d64",
"version" : "1.4.3"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
20 changes: 16 additions & 4 deletions Sources/Networking/Core/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ open class APIManager: APIManaging, Retryable {
private let requestAdapters: [RequestAdapting]
private let responseProcessors: [ResponseProcessing]
private let errorProcessors: [ErrorProcessing]
private let responseProvider: ResponseProviding
private let sessionId: String
private var _responseProvider: ResponseProviding

internal var retryCounter = Counter()

public init(
urlSession: URLSession = .init(configuration: .default),
requestAdapters: [RequestAdapting] = [],
Expand All @@ -69,7 +69,7 @@ open class APIManager: APIManaging, Retryable {
sessionId = Date().ISO8601Format()
}

self.responseProvider = urlSession
self._responseProvider = urlSession
self.requestAdapters = requestAdapters
self.responseProcessors = responseProcessors
self.errorProcessors = errorProcessors
Expand All @@ -89,7 +89,7 @@ open class APIManager: APIManaging, Retryable {
} else {
sessionId = Date().ISO8601Format()
}
self.responseProvider = responseProvider
self._responseProvider = responseProvider
self.requestAdapters = requestAdapters
self.responseProcessors = responseProcessors
self.errorProcessors = errorProcessors
Expand All @@ -103,6 +103,18 @@ open class APIManager: APIManaging, Retryable {
}
}

// MARK: Response provider

public extension APIManager {
var responseProvider: ResponseProviding {
_responseProvider
}

func setResponseProvider(_ provider: ResponseProviding) {
self._responseProvider = provider
}
}

private extension APIManager {
func request(_ endpointRequest: EndpointRequest, retryConfiguration: RetryConfiguration?) async throws -> Response {
do {
Expand Down
8 changes: 7 additions & 1 deletion Sources/Networking/Core/APIManaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
public protocol APIManaging {
/// A default `JSONDecoder` used for all requests.
var defaultDecoder: JSONDecoder { get }

/// Creates a network request for an API endpoint defined by ``Requestable``.
/// - Parameters:
/// - endpoint: API endpoint requestable definition.
Expand All @@ -34,6 +34,12 @@ public protocol APIManaging {
decoder: JSONDecoder,
retryConfiguration: RetryConfiguration?
) async throws -> DecodableResponse

/// Invalidates and response provider to gracefully clear out all its tasks.
var responseProvider: ResponseProviding { get }

/// Replaces the response provider instance used by APIManager.
func setResponseProvider(_ provider: ResponseProviding)
}

// MARK: - Provide request with default json decoder, retry configuration
Expand Down
11 changes: 11 additions & 0 deletions Sources/Networking/Core/ResponseProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import Foundation
public protocol ResponseProviding {
/// Creates a ``Response`` for a given `URLRequest`.
func response(for request: URLRequest) async throws -> Response

/// Invalidates and response provider to gracefully clear out all its tasks.
func invalidate() async
}

extension URLSession: ResponseProviding {
/// Creates a ``Response`` for a given `URLRequest` by firing a network request.
public func response(for request: URLRequest) async throws -> Response {
try await data(for: request)
}

/// Invalidates and response provider to gracefully clear out all its tasks.
/// Warning: URLSession can no longer be used after it's been invalidated and any usage
/// will lead to a crash.
public func invalidate() async {
await allTasks.forEach { $0.cancel() }
invalidateAndCancel()
}
}
2 changes: 2 additions & 0 deletions Sources/Networking/Core/StoredResponseProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ open class StoredResponseProvider: ResponseProviding {
self.sessionId = sessionId
}

public func invalidate() async {}

/// Creates a ``Response`` for a given `URLRequest` based on data from a corresponding file stored in Assets.
/// - Parameter request: URL request.
public func response(for request: URLRequest) async throws -> Response {
Expand Down

0 comments on commit b445b06

Please sign in to comment.