yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
627 stars 147 forks source link

Alamofire RequestRetrier doesn't work #151

Open chaseacton opened 5 years ago

chaseacton commented 5 years ago

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()
yonaskolb commented 5 years ago

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

chaseacton commented 5 years ago

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 commented 5 years ago

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

cerupcat commented 5 years ago

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