aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 243 forks source link

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

Open sagnik-sanyal opened 3 months ago

sagnik-sanyal commented 3 months ago

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

Steps to Reproduce

N/A

Screenshots

No response

Platforms

Flutter Version

3.22.1

Amplify Flutter Version

2.1.0

Deployment Method

Amplify CLI

Schema

No response

tyllark commented 3 months ago

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

sagnik-sanyal commented 3 months ago

Hi @tyllark any update on this issue ?

Jordan-Nelson commented 3 months ago

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.

sagnik-sanyal commented 3 months ago

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

Jordan-Nelson commented 3 months ago

@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 commented 3 months ago

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 commented 3 months ago

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

sagnik-sanyal commented 3 months ago

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 commented 2 months ago

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