SwagGen icon indicating copy to clipboard operation
SwagGen copied to clipboard

Alamofire RequestRetrier doesn't work

Open chaseacton opened this issue 6 years ago • 4 comments

I am attempting to implement Alamofire's RequestRetrier and RequestAdapter protocols. The RequestAdapter works, but RequestRetrier doesn't; func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) is never called.

I am setting them like this:

APIClient.default.sessionManager.adapter = MyRequestAdapter()
APIClient.default.sessionManager.retrier = MyRequestRetrier()

I've also tried:

Alamofire.SessionManager.default.adapter = MyRequestAdapter()
Alamofire.SessionManager.default.retrier = MyRequestRetrier()

chaseacton avatar Apr 09 '19 21:04 chaseacton

Not sure why that isn't working for you. The APIClient just uses sessionManager.request() so anything past that is up to Alamofire.

yonaskolb avatar Apr 10 '19 08:04 yonaskolb

Upon further investigation, the retrier isn't getting called on 401s because by default, they aren't considered errors. To make it work, .validate() needs to be called on the request:

let networkRequest = sessionManager.request(urlRequest).validate().responseData(queue: decodingQueue)

Is this possible to do with SwagGen? .validate() is called in swagger-codegen's implementation (https://github.com/swagger-api/swagger-codegen)

chaseacton avatar Apr 12 '19 23:04 chaseacton

@yonaskolb As an alternative, it would be nice if RequestBehaviour had an additional protocol function to validate the response code/contents so we can tell SwagGen/Alamofire if the response should be treated as an error.

chaseacton avatar Apr 15 '19 17:04 chaseacton

I had to modify the APIService to add .validate().

cerupcat avatar May 21 '19 00:05 cerupcat