Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Update header when retrying #21

Open
SeriousMonk opened this issue Dec 2, 2021 · 2 comments
Open

Update header when retrying #21

SeriousMonk opened this issue Dec 2, 2021 · 2 comments

Comments

@SeriousMonk
Copy link

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
Copy link
Author

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++;

@niyonx
Copy link

niyonx commented Mar 16, 2022

@SeriousMonk I have the same issue, did you find a fix?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants