#✅ - Able to Cancel API Requests Amplify Flutter

13 messages · Page 1 of 1 (latest)

wooden oasis
#

Guys how to do i cancel the api request using aws amplify api as rest operation is the object that has .cancel() property but in our architecture we have a repository layer that has the rest api endpoints defined within the function and the rest option object is not available from outside, as we only want the api url and body as the parameters to passed into the methods of the api class. Below is a sample of the mentioned code: Future<Map<String, dynamic>> get( String path, { String? body, Duration? customTimeoutDuration, Map<String, String>? queryParameters, Map<String, String>? headers, }) async { try { RestOptions getOptions = RestOptions( path: path, body: body.isNull() ? null : Uint8List.fromList(body!.codeUnits), headers: headers, queryParameters: queryParameters, ); RestOperation getOperation = Amplify.API.get(restOptions: getOptions); RestResponse getResponse = await getOperation.response .timeout(customTimeoutDuration ?? _defaultTimeoutDuration); if (getResponse.statusCode >= 200 && getResponse.statusCode < 300) { return jsonDecode(String.fromCharCodes(getResponse.data)); } } on ApiException catch (e) { //Do something on error }

To access cancel method we need to take out the RestOptions class, which we dont want to, is there any other option like Dio that passes the cancelToken inside the function param

#

Also note that We are using clean architecture and riverpod in our application

carmine sigil
#

Hi @wooden oasis I noticed you are using version 0.x of the Amplify Flutter libraries. We recently released version 1.0 and I would highly recommend making the switch when you get a chance. We've prepared an upgrade guide here: https://docs.amplify.aws/lib/project-setup/upgrade-guide/q/platform/flutter/

To your question, the way to cancel an operation is by calling getOperation.cancel() in your example. There is currently no option for a cancel token.

wooden oasis
#

Yeah thanks for the info, But the problem is when lets say, if there are nested futures with http requests or serially added requests which are present one after the another do we need to call .cancel() for each of them or is there is any util provided to cancel all of them at once

#

like lets say a cancel method that cancels all the operations within the request of a group

carmine sigil
wooden oasis
#

Thanks, for the info. Can you please provide a small code sample for the same ?

#

Like i am mainly wondering about the chaining part

#

Is there any sample readily available ?

carmine sigil
#

Here's an example:

#
final firstOp = Amplify.API.get('/').operation;
final chainedOp = firstOp.thenOperation((resp, completer) {
  return completer.completeOperation(
    Amplify.API.get('/another').operation,
  );
});
#

This creates a chained operation which first calls GET / then GET /another. The chainedOp can be canceled at any time and will affect the whole chain.

candid lakeBOT
#

✅ - Able to Cancel API Requests Amplify Flutter