amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

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

Open sagnik-sanyal opened this issue 1 year ago • 9 comments

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
  • [X] API (REST)
  • [ ] API (GraphQL)
  • [ ] Auth
  • [ ] Authenticator
  • [ ] DataStore
  • [ ] Notifications (Push)
  • [ ] Storage

Steps to Reproduce

N/A

Screenshots

No response

Platforms

  • [X] iOS
  • [X] Android
  • [X] Web
  • [X] macOS
  • [X] Windows
  • [X] Linux

Flutter Version

3.22.1

Amplify Flutter Version

2.1.0

Deployment Method

Amplify CLI

Schema

No response

sagnik-sanyal avatar Jun 23 '24 18:06 sagnik-sanyal

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

tyllark avatar Jun 23 '24 22:06 tyllark

Hi @tyllark any update on this issue ?

sagnik-sanyal avatar Jun 25 '24 15:06 sagnik-sanyal

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 avatar Jun 25 '24 16:06 Jordan-Nelson

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

sagnik-sanyal avatar Jun 26 '24 10:06 sagnik-sanyal

@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.

Jordan-Nelson avatar Jun 26 '24 15:06 Jordan-Nelson

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 ) ?

sagnik-sanyal avatar Jun 27 '24 03:06 sagnik-sanyal

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

Jordan-Nelson avatar Jun 27 '24 14:06 Jordan-Nelson

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

sagnik-sanyal avatar Jul 05 '24 14:07 sagnik-sanyal

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

Jordan-Nelson avatar Jul 24 '24 18:07 Jordan-Nelson