resendRequestUntilParsedResponseIsValid function

Future<JsonMap> resendRequestUntilParsedResponseIsValid(
  1. RequestFuncType requestFunc,
  2. String requestAdditionalParams,
  3. ParseFuncMapType parseFunc,
  4. bool validateFunc(
    1. JsonMap
    ),
  5. int maxRetries,
)

Resends the requestFunc until the validateFunc returns true.

  • maxRetries How often to resend.

Implementation

Future<JsonMap> resendRequestUntilParsedResponseIsValid(
  RequestFuncType requestFunc,
  String requestAdditionalParams,
  ParseFuncMapType parseFunc,
  bool Function(JsonMap) validateFunc,
  int maxRetries,
) async {
  JsonMap response = await requestFunc(requestAdditionalParams);
  JsonMap parsedObject = await parseFunc(response);
  int retryCounter = 0;

  while (!validateFunc(parsedObject) && retryCounter < maxRetries) {
    response = await requestFunc(requestAdditionalParams);
    final attempt = await parseFunc(response);
    if ((attempt['parsed'] as List).length >
        (parsedObject['parsed'] as List).length) {
      parsedObject = attempt;
    }
    retryCounter += 1;
  }

  return parsedObject;
}