ixx
ixx

Reputation: 32269

Limit retry count of Authenticator

I extended Authenticator to retrieve the authorization and authentication tokens when I get the authentication challenge (401). It retries 20 times. Is there a way to set a different count (I'd like to set it to 3)?

Just in case it's relevant, I'm using OkHttp(3) with retrofit2.

Upvotes: 4

Views: 2658

Answers (2)

nilsi
nilsi

Reputation: 10761

I made an extension function for the Response object in Kotlin. Since I'm only interested in trying three times before giving up.

private val Response.reachedMaxAttempts: Boolean
    get() = this.priorResponse?.priorResponse?.priorResponse != null

Upvotes: 2

Jesse Wilson
Jesse Wilson

Reputation: 40623

See the Handling Authentication documentation:

private int responseCount(Response response) {
  int result = 1;
  while ((response = response.priorResponse()) != null) {
    result++;
  }
  return result;
}

Upvotes: 7

Related Questions