yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alamofire RequestRetrier doesn't work

chaseacton opened this issue · comments

commented

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()

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

commented

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)

commented

@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.

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