Following the Google Drive REST API documentation, I'm implementing an exponential backoff strategy for recoverable API errors. I'd like to retry my request if the error code is 500 or 403 (and maybe 429), but I noticed that not all 403 errors are recoverable, so I'd like to retry only for those with a reason in a selected list. The error reason is not provided as a separate information in the response, but it's contained in the content inputstream, which I parsed and processed:
private boolean isRateLimitExceeded(com.google.api.client.http.HttpResponse response) {
int statusCode = response.getStatusCode();
if (statusCode == 403) {
response.getRequest().setParser(Utils.getDefaultJsonFactory().createJsonObjectParser());
GoogleJsonErrorContainer jsonResponse = response.parseAs(GoogleJsonErrorContainer.class);
List<ErrorInfo> errors = jsonResponse.getError().getErrors();
if (errors != null && !errors.isEmpty()) {
List<String> recoverableErrors = Arrays.asList("limitExceeded", "concurrentLimitExceeded", "rateLimitExceeded", "userRateLimitExceeded", "servingLimitExceeded");
for (ErrorInfo errorInfo : errors) {
if (!recoverableErrors.contains(errorInfo.getReason())) {
return false;
}
}
return true;
}
}
return false;
}
This works fine, but consumes the content. During the execution of the batch request, the google client tries to read and parse the content again, finds nothing left, and throws an exception. I tried marking and resetting the response.getContent() inputstream, but Google's HttpResponse doesn't support marking. Is there any other way I can make the content available again, or reading it without consuming it?
If it matters, I'm using a GoogleNetHttpTransport.