resendRequestUntilParsedResponseIsValid function
- RequestFuncType requestFunc,
- String requestAdditionalParams,
- ParseFuncMapType parseFunc,
- bool validateFunc(),
- 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;
}