Search in sources :

Example 26 with HttpResponse

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();
}
Also used : HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Example 27 with HttpResponse

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();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) Test(org.junit.Test)

Example 28 with HttpResponse

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);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) ErrorInfo(com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 29 with HttpResponse

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 30 with HttpResponse

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();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Aggregations

HttpResponse (com.google.api.client.http.HttpResponse)33 IOException (java.io.IOException)22 HttpRequest (com.google.api.client.http.HttpRequest)20 GenericUrl (com.google.api.client.http.GenericUrl)19 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)11 HttpTransport (com.google.api.client.http.HttpTransport)8 InputStream (java.io.InputStream)8 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 Test (org.junit.Test)7 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)6 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)4 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)4 HttpResponseException (com.google.api.client.http.HttpResponseException)4 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)4 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)4 JsonFactory (com.google.api.client.json.JsonFactory)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)4