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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Howto retry request #179

Open
bluewalk opened this issue Jan 14, 2021 · 2 comments
Open

Howto retry request #179

bluewalk opened this issue Jan 14, 2021 · 2 comments

Comments

@bluewalk
Copy link

First of all, great work, saves a bunch of work on my side 馃憤

What I'm unable to figure out is how to retry a request.
For example I have the following, a class that extends Resource which overrides

  • getHeaders (to implement the bearer token)
  • getUrl (to get the baseUrl from the environment)
  • handleSuccessResponse (to hide the loading bar)
  • handleErrorResponse (to hide the loading bar and on 401 do a refresh of the bearer token and logout upon 403)

After refreshing the bearer token (which returns a boolean) I would like to re-execute the original request so the user doesn't have to click the button twice.

How would I go and achieve that? Is there a way to send the original request back to this.requestHandler? Or is there some other way?

Below the handleErrorResponse method

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }
    
    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }
@troyanskiy
Copy link
Owner

Hi @bluewalk

Thank you for warm words :)
It's a log time I did not develop with the library since I switched to Flutter.

But I think you can try to call return this.$restAction(options) in that case (not sure).

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
          return this.$restAction(options); // hope it will help you
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }
    
    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }

Let me know if that will help you.

@bluewalk
Copy link
Author

bluewalk commented Jan 14, 2021

I was thinking of something like that as well, unfortunately it doesn't work:
Error: Uncaught (in promise): [object Undefined]

The methods are IResourceMethodPromise's

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

2 participants