[ ] I've read, understood, and done my best to follow the CONTRIBUTING guidelines.
Created a RequestInterceptor
session.request(route, interceptor: EnvironmentInterceptor())
struct EnvironmentInterceptor: RequestInterceptor {
func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest>) -> Void) {
var adaptedRequest = urlRequest
guard let token = AtraqService.shared.user?.token.accessToken else {
completion(.success(adaptedRequest))
return
}
adaptedRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
completion(.success(adaptedRequest))
}
// Not called on 401
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401{
}
}
retry() to be called on 401 error
adapt was called first...
retry was not called on error
5.0.0beta3
10.1
4.2
12.1.4
10.14.3
none
Retry is only triggered when an error is produced. By default, there's no error produced by a 401 response, by URLSession or Alamofire. If you want one, you'll need add a validate() call, either using the default setting or customized with the behavior you want.
@jshier Yes that works.
Most helpful comment
Retry is only triggered when an error is produced. By default, there's no error produced by a 401 response, by
URLSessionor Alamofire. If you want one, you'll need add avalidate()call, either using the default setting or customized with the behavior you want.