http icon indicating copy to clipboard operation
http copied to clipboard

Update headers on retry

Open SeriousMonk opened this issue 4 years ago • 1 comments

Hi, I'm using the RetryClient() to refresh the access token if expired and then try the request again. However when retrying the request, the old access token is sent and not the new one. This is how I configured my RetryClient:

RetryClient client = RetryClient(
  http.Client(),
  retries: 1,
  when: (response) => response.statusCode == 401,
  onRetry: (req, res, retryCount) async{
    await authService.refreshToken();
    req.headers['Authorization'] = sharedPrefs.accessToken;
    }
);

I'm guessing that the req element passed to onRetry can't be used to update the req that is actually sent when retrying. How can I solve this?

Thanks :)

SeriousMonk avatar Dec 02 '21 12:12 SeriousMonk

Found the problem. Currently the _onRetry function is a void Function, wheras it should be a Future function in order to accept future functions as well.

This

final void Function(BaseRequest, BaseResponse?, int)? _onRetry;

should be changed to this

final Future<void> Function(BaseRequest, BaseResponse?, int)? _onRetry;

Then at line 121 of RetryClient, _onRetry should be called like this instead:

await Future.delayed(_delay(i));
//_onRetry?.call(request, response, i); THIS DOESN'T SUPPORT FUTURE FUNCTION
if(_onRetry != null) await _onRetry!(request, response, i);
i++;

SeriousMonk avatar Dec 02 '21 14:12 SeriousMonk