Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update headers on retry #645

Open
SeriousMonk opened this issue Dec 2, 2021 · 1 comment
Open

Update headers on retry #645

SeriousMonk opened this issue Dec 2, 2021 · 1 comment

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

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

No branches or pull requests

1 participant