RequestAdapter
Request adapter for URL requests from “MovieLister” demo app (Swift for Good book, a chapter by Ben Scheirman)
The code is taken from:
Usage
1. Implement the desired functionality inside the adapter protocol methods
struct UserAuthAdapter : RequestAdapter {
func onResponse(response: URLResponse?, data: Data?) {
if let urlResponse = response as? HTTPURLResponse {
UserAuthData.saveToRepositoryFrom(urlResponse: urlResponse)
}
}
}
APIClient
as a property
2. Add the adapters to your private var adapters: [RequestAdapter]
APIClient
3. Call the adapters form the implementation of the self.adapters.forEach { $0.beforeSend(dataRequest.request) }
or
guard let data = data.data else {
let error = data.error ?? AFError.explicitlyCancelled
self.adapters.forEach { $0.onError(request: dataRequest.request, error: error) }