Custom Network Manager using the latest Swift technology known as Async/Await, minimum compatible version: iOS 13.
Create a custom Client class and Routes enum:
- the Client class should have its protocol with all the API functions exposed
- the class should extends its protocol
- the class should implement its own networkManager var or let
private let networkManager: URLRequestProtocolwhich refers toURLRequestProtocolprotocol (the one from NetX) - perform a the class init by passing a custom networkManager or by using
URLRequestClient.sharedsingleton from NetX - each function will call the
performURLRequestfunction declared fromURLRequestProtocolprotocol
- the router enum must extend
APIConfigurationprotocol from NetX - write all the case and pass the needed data as argument, e.g:
case helloWorld(myName: String) - the compiler will ask you to put all the missing vars and funcs declared and required by the
APIConfigurationprotocol - if you want to create custom vars or to overwrite the existing ones, you just need to create an extension of
APIConfigurationand then re-write the var or func you want to override
import NetX
protocol FitmentFetchable {
func getFitment(car: String) async throws -> FitmentModel
}
final class FitmentClient: FitmentFetchable {
private let networkManager: URLRequestProtocol
init(networkManager: URLRequestProtocol = URLRequestClient(requestClientConfigurator: RequestClientConfigurator(urlSession: .shared, needsLoader: true, needsRetry: true, retryLimit: 3, timeoutInterval: 30.0, cachePolicy: .useProtocolCachePolicy))) {
self.networkManager = networkManager
}
func getFitment(car: String) async throws -> FitmentModel {
try await networkManager.performURLRequest(FitmentRouter.getFitment(car: car), cacheId: nil)
}
}import NetX
enum FitmentRouter: APIConfiguration {
case getFitment(car: String)
}