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

Capability to retry the api request as well as handle the cancellation #5049

Open
7 of 14 tasks
sagnik-sanyal opened this issue Jun 23, 2024 · 9 comments
Open
7 of 14 tasks
Labels
feature-request A request for a new feature or an enhancement to an existing API or category. REST API Issues related to the API (REST) Category

Comments

@sagnik-sanyal
Copy link

Description

I have an important requirement to be able to retry the http request based upon some condition for specified number of attempts for which i am using the extension below , also note that .retry is a custom extension on future to retry the future. So here if the exception is HttpStatusException with status code 500 or some other code i need to retry the request.

extension RestOperationTimeout on RestOperation {
  RestOperation applyRetries({int maxAttempts = 3}) {
    return this
      ..response.retry(
        maxAttempts: maxAttempts,
        retryIf: (Exception e) => // some condition here
        onRetry: // do something like log errors on retry,
      );
  }
}

But i guess that as the package internally relies on CancellableCompleter, if the first request fails and perform retry i get an error below on the second request ->

E/flutter (20132): #0      AmplifyAuthorizationRestClient.transformResponse (package:amplify_api_dart/src/util/amplify_authorization_rest_client.dart:65:7)
E/flutter (20132): <asynchronous suspension>
E/flutter (20132): #1      CancelableCompleter.complete.<anonymous closure> (package:async/src/cancelable_operation.dart:425:16)
E/flutter (20132): <asynchronous suspension>

Do i need to create a new RestOperation object everytime a new request is sent or am i missing something ?
This function is going to be used all across the application so that the retry gets cancelled if request was cancelled and also perform retry if the request fails and the operation is still active.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

N/A

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.22.1

Amplify Flutter Version

2.1.0

Deployment Method

Amplify CLI

Schema

No response

@tyllark
Copy link
Contributor

tyllark commented Jun 23, 2024

Hello @sagnik-sanyal thank you for submitting this issue. We will look into this and get back to you!

@tyllark tyllark added pending-triage This issue is in the backlog of issues to triage REST API Issues related to the API (REST) Category labels Jun 23, 2024
@sagnik-sanyal
Copy link
Author

Hi @tyllark any update on this issue ?

@Jordan-Nelson Jordan-Nelson added question A question about the Amplify Flutter libraries and removed pending-triage This issue is in the backlog of issues to triage labels Jun 25, 2024
@Jordan-Nelson
Copy link
Contributor

Hello @sagnik-sanyal - Currently you would need to create a new operation.

Alternatively, you may be able to provide your own http client to the API plugin and use an http client that retries the request. I would need to look into this a bit more to see if this would work.

Let me know if you have other questions.

@Jordan-Nelson Jordan-Nelson added the pending-response Issue is pending response from the issue requestor label Jun 25, 2024
@sagnik-sanyal
Copy link
Author

@Jordan-Nelson i would like to have an example of the second implementation that you mentioned, for retrying and cancelling the request

@Jordan-Nelson
Copy link
Contributor

@sagnik-sanyal - Here is an example of using a custom http client. This is a fairly simple example of adding a header to each request. I would need to confirm if this can be used to retry requests.

@sagnik-sanyal
Copy link
Author

Thanks @Jordan-Nelson for providing valuable insights, yeah I will try this to achieve retrying the requests, but can this be cancelled or how to access whether it has been cancelled from the custom client ( let's say the user left the screen while the request was retrying so there's no need to perform the request ) ?

@Jordan-Nelson
Copy link
Contributor

RestOperation should still be cancellable when using a custom client. RestOperation has a cancel() method.

@sagnik-sanyal
Copy link
Author

Thanks for helping us although we would love to have a utility or a resuable extension to retry the api calls @Jordan-Nelson

@Jordan-Nelson
Copy link
Contributor

@sagnik-sanyal I am going to label this as a feature request for the ability to retry the request.

@Jordan-Nelson Jordan-Nelson added feature-request A request for a new feature or an enhancement to an existing API or category. and removed question A question about the Amplify Flutter libraries pending-response Issue is pending response from the issue requestor labels Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A request for a new feature or an enhancement to an existing API or category. REST API Issues related to the API (REST) Category
Projects
None yet
Development

No branches or pull requests

3 participants