Search in sources :

Example 31 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project copybara by google.

the class GitHubApiTransportImpl method post.

@SuppressWarnings("unchecked")
@Override
public <T> T post(String path, Object request, Type responseType) throws RepoException, ValidationException {
    HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentials());
    GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
    try {
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(JSON_FACTORY, request));
        HttpResponse response = httpRequest.execute();
        return (T) response.parseAs(responseType);
    } catch (HttpResponseException e) {
        try {
            throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, JSON_FACTORY.toPrettyString(request), e.getContent());
        } catch (IOException e1) {
            logger.log(Level.SEVERE, "Error serializing request for error", e1);
            throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, "unknown request", e.getContent());
        }
    } catch (IOException e) {
        throw new RepoException("Error running GitHub API operation " + path, e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException)

Example 32 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project copybara by google.

the class GitHubApiTransportImpl method get.

@SuppressWarnings("unchecked")
@Override
public <T> T get(String path, Type responseType) throws RepoException, ValidationException {
    HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentialsIfPresent());
    GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
    try {
        HttpRequest httpRequest = requestFactory.buildGetRequest(url);
        HttpResponse response = httpRequest.execute();
        return (T) response.parseAs(responseType);
    } catch (HttpResponseException e) {
        throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "GET", path, null, e.getContent());
    } catch (IOException e) {
        throw new RepoException("Error running GitHub API operation " + path, e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException)

Example 33 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project kork by spinnaker.

the class StackdriverWriter method writeRegistryHelper.

/**
 * Implementation of writeRegistry wrapped for timing.
 */
private void writeRegistryHelper(Registry registry) {
    MonitoredResource resource = determineMonitoredResource();
    if (resource == null) {
        log.warn("Cannot determine the managed resource - not flushing metrics.");
        return;
    }
    List<TimeSeries> tsList = registryToTimeSeries(registry);
    if (tsList.isEmpty()) {
        log.debug("No metric data points.");
        return;
    }
    CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest();
    int offset = 0;
    int failed = 0;
    List<TimeSeries> nextN;
    log.debug("Writing metrics...");
    while (offset < tsList.size()) {
        if (offset + MAX_TS_PER_REQUEST < tsList.size()) {
            nextN = tsList.subList(offset, offset + MAX_TS_PER_REQUEST);
            offset += MAX_TS_PER_REQUEST;
        } else {
            nextN = tsList.subList(offset, tsList.size());
            offset = tsList.size();
        }
        tsRequest.setTimeSeries(nextN);
        try {
            service.projects().timeSeries().create(projectResourceName, tsRequest).execute();
        } catch (HttpResponseException rex) {
            handleTimeSeriesResponseException(rex, "creating time series", nextN);
            failed += nextN.size();
        } catch (IOException ioex) {
            log.error("Caught Exception creating time series " + ioex);
            failed += nextN.size();
        }
    }
    log.debug("Wrote {} values", tsList.size() - failed);
}
Also used : TimeSeries(com.google.api.services.monitoring.v3.model.TimeSeries) CreateTimeSeriesRequest(com.google.api.services.monitoring.v3.model.CreateTimeSeriesRequest) MonitoredResource(com.google.api.services.monitoring.v3.model.MonitoredResource) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) Point(com.google.api.services.monitoring.v3.model.Point)

Example 34 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException_invalidStatusCode.

@Test
public void testHandleHttpResponseException_invalidStatusCode() throws RegistryErrorException {
    HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
    Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(-1);
    try {
        testBlobChecker.handleHttpResponseException(mockHttpResponseException);
        Assert.fail("Non-404 status codes should not be handled");
    } catch (HttpResponseException ex) {
        Assert.assertEquals(mockHttpResponseException, ex);
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Example 35 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException_hasOtherErrors.

@Test
public void testHandleHttpResponseException_hasOtherErrors() throws IOException, RegistryErrorException {
    HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
    Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")).addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message"));
    Mockito.when(mockHttpResponseException.getContent()).thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
    try {
        testBlobChecker.handleHttpResponseException(mockHttpResponseException);
        Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
    } catch (HttpResponseException ex) {
        Assert.assertEquals(mockHttpResponseException, ex);
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Test(org.junit.Test)

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)38 GenericUrl (com.google.api.client.http.GenericUrl)15 IOException (java.io.IOException)15 HttpResponse (com.google.api.client.http.HttpResponse)13 Test (org.junit.Test)13 HttpRequest (com.google.api.client.http.HttpRequest)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)6 HttpHeaders (com.google.api.client.http.HttpHeaders)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)4 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)3 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PServiceCall (net.morimekta.providence.PServiceCall)3 Request (net.morimekta.test.providence.client.Request)3 TestService (net.morimekta.test.providence.client.TestService)3 AccountManager (android.accounts.AccountManager)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)2 HttpTransport (com.google.api.client.http.HttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2