use of com.google.api.client.http.HttpResponse in project beam by apache.
the class RetryHttpRequestInitializerTest method testErrorCodeForbidden.
/**
* Tests that a non-retriable error is not retried.
*/
@Test
public void testErrorCodeForbidden() throws IOException {
when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse);
when(mockLowLevelResponse.getStatusCode()).thenReturn(// Non-retryable error.
403).thenReturn(// Shouldn't happen.
200);
try {
Storage.Buckets.Get result = storage.buckets().get("test");
HttpResponse response = result.executeUnparsed();
assertNotNull(response);
} catch (HttpResponseException e) {
Assert.assertThat(e.getMessage(), Matchers.containsString("403"));
}
verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class));
verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString());
verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt());
verify(mockLowLevelRequest).execute();
verify(mockLowLevelResponse).getStatusCode();
}
use of com.google.api.client.http.HttpResponse in project beam by apache.
the class RetryHttpRequestInitializerTest method testRetryableErrorRetryEnoughTimes.
/**
* Tests that a retryable error is retried enough times.
*/
@Test
public void testRetryableErrorRetryEnoughTimes() throws IOException {
when(mockLowLevelRequest.execute()).thenReturn(mockLowLevelResponse);
final int retries = 10;
when(mockLowLevelResponse.getStatusCode()).thenAnswer(new Answer<Integer>() {
int n = 0;
@Override
public Integer answer(InvocationOnMock invocation) {
return (n++ < retries - 1) ? 503 : 200;
}
});
Storage.Buckets.Get result = storage.buckets().get("test");
HttpResponse response = result.executeUnparsed();
assertNotNull(response);
verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class));
verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString());
verify(mockLowLevelRequest, times(retries)).setTimeout(anyInt(), anyInt());
verify(mockLowLevelRequest, times(retries)).execute();
verify(mockLowLevelResponse, times(retries)).getStatusCode();
}
use of com.google.api.client.http.HttpResponse in project beam by apache.
the class PackageUtilTest method googleJsonResponseException.
/**
* Builds a fake GoogleJsonResponseException for testing API error handling.
*/
private static GoogleJsonResponseException googleJsonResponseException(final int status, final String reason, final String message) throws IOException {
final JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setReason(reason);
errorInfo.setMessage(message);
errorInfo.setFactory(jsonFactory);
GenericJson error = new GenericJson();
error.set("code", status);
error.set("errors", Arrays.asList(errorInfo));
error.setFactory(jsonFactory);
GenericJson errorResponse = new GenericJson();
errorResponse.set("error", error);
errorResponse.setFactory(jsonFactory);
return new MockLowLevelHttpRequest().setResponse(new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString()).setContentType(Json.MEDIA_TYPE).setStatusCode(status));
}
};
HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
return GoogleJsonResponseException.from(jsonFactory, response);
}
use of com.google.api.client.http.HttpResponse in project local-data-aragopedia by aragonopendata.
the class GoogleDriveAPI method downloadAllFiles.
public void downloadAllFiles(String path) {
List<File> files = listOwnerFiles();
for (File file : files) {
try {
String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
InputStream input = resp.getContent();
java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
FileUtils.copyInputStreamToFile(input, f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.google.api.client.http.HttpResponse in project local-data-aragopedia by aragonopendata.
the class GoogleDriveAPI method downloadFilesAfterDate.
public void downloadFilesAfterDate(String path, String stringDateLastChange) {
List<File> files = listOwnerFilesAfterDate(stringDateLastChange);
for (File file : files) {
try {
String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
InputStream input = resp.getContent();
java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
FileUtils.copyInputStreamToFile(input, f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aggregations