Search in sources :

Example 1 with HttpResponseException

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

the class HexagonTools method trackFailedRequest.

public static void trackFailedRequest(Context context, String action, IOException e) {
    if (e instanceof HttpResponseException) {
        HttpResponseException responseException = (HttpResponseException) e;
        Utils.trackCustomEvent(context, HEXAGON_ERROR_CATEGORY, action, responseException.getStatusCode() + " " + responseException.getStatusMessage());
        // log like "action: 404 not found"
        Timber.e("%s: %s %s", action, responseException.getStatusCode(), responseException.getStatusMessage());
    } else {
        Utils.trackCustomEvent(context, HEXAGON_ERROR_CATEGORY, action, e.getMessage());
        // log like "action: Unable to resolve host"
        Timber.e("%s: %s", action, e.getMessage());
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException)

Example 2 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.

the class HttpBigQueryRpc method write.

@Override
public Job write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
    try {
        GenericUrl url = new GenericUrl(uploadId);
        HttpRequest httpRequest = bigquery.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
        httpRequest.setParser(bigquery.getObjectParser());
        long limit = destOffset + length;
        StringBuilder range = new StringBuilder("bytes ");
        range.append(destOffset).append('-').append(limit - 1).append('/');
        if (last) {
            range.append(limit);
        } else {
            range.append('*');
        }
        httpRequest.getHeaders().setContentRange(range.toString());
        int code;
        String message;
        IOException exception = null;
        HttpResponse response = null;
        try {
            response = httpRequest.execute();
            code = response.getStatusCode();
            message = response.getStatusMessage();
        } catch (HttpResponseException ex) {
            exception = ex;
            code = ex.getStatusCode();
            message = ex.getStatusMessage();
        }
        if (!last && code != HTTP_RESUME_INCOMPLETE || last && !(code == HTTP_OK || code == HTTP_CREATED)) {
            if (exception != null) {
                throw exception;
            }
            throw new BigQueryException(code, message);
        }
        return last && response != null ? response.parseAs(Job.class) : null;
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) 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) BigQueryException(com.google.cloud.bigquery.BigQueryException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Job(com.google.api.services.bigquery.model.Job)

Example 3 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project data-transfer-project by google.

the class SmugMugPhotoService method postRequest.

private <T> T postRequest(String url, HttpContent content, Map<String, String> headers, TypeReference<T> typeReference) throws IOException {
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    String fullUrl = url;
    if (!fullUrl.contains("://")) {
        fullUrl = BASE_URL + url;
    }
    HttpRequest postRequest = requestFactory.buildPostRequest(new GenericUrl(fullUrl), content);
    HttpHeaders httpHeaders = new HttpHeaders().setAccept("application/json").setContentType("application/json");
    for (Entry<String, String> entry : headers.entrySet()) {
        httpHeaders.put(entry.getKey(), entry.getValue());
    }
    postRequest.setHeaders(httpHeaders);
    try {
        postRequest = (HttpRequest) this.authConsumer.sign(postRequest).unwrap();
    } catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't create post request", e);
    }
    HttpResponse response;
    try {
        response = postRequest.execute();
    } catch (HttpResponseException e) {
        throw new IOException("Problem making request: " + postRequest.getUrl(), e);
    }
    int statusCode = response.getStatusCode();
    if (statusCode < 200 || statusCode >= 300) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
    return MAPPER.readValue(result, typeReference);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) InputStreamReader(java.io.InputStreamReader) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) 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) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 4 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project data-transfer-project by google.

the class MicrosoftCalendarService method getCalendars.

private CalendarModelWrapper getCalendars(Optional<PaginationInformation> pageInfo) throws IOException {
    URL url = new URL("https://outlook.office.com/api/v2.0/me/calendars");
    HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(url));
    getRequest.setParser(new JsonObjectParser(new JacksonFactory()));
    HttpResponse response;
    try {
        response = getRequest.execute();
    } catch (HttpResponseException e) {
        logger.debug("Error fetching content");
        logger.debug("response status code: {}", e.getStatusCode());
        logger.debug("response status message: {}", e.getStatusMessage());
        logger.debug("response headers: {}", e.getHeaders());
        logger.debug("response content: {}", e.getContent());
        e.printStackTrace();
        throw e;
    }
    int statusCode = response.getStatusCode();
    if (statusCode != 200) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    // Parse response into model
    OutlookCalendarList data = response.parseAs(OutlookCalendarList.class);
    List<CalendarModel> calendars = new ArrayList<>(data.list.size());
    List<Resource> resources = new ArrayList<>(data.list.size());
    for (OutlookCalendar calendar : data.list) {
        calendars.add(new CalendarModel(calendar.id, calendar.name, null));
        resources.add(new IdOnlyResource(calendar.id));
    }
    return new CalendarModelWrapper(calendars, null, new ContinuationInformation(resources, null));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) ArrayList(java.util.ArrayList) Resource(org.dataportabilityproject.dataModels.Resource) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) HttpResponse(com.google.api.client.http.HttpResponse) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) URL(java.net.URL) CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) JsonObjectParser(com.google.api.client.json.JsonObjectParser)

Example 5 with HttpResponseException

use of com.google.api.client.http.HttpResponseException in project data-transfer-project by google.

the class MicrosoftCalendarService method getCalendarEvents.

private CalendarModelWrapper getCalendarEvents(String calendarId, Optional<PaginationInformation> pageInfo) throws IOException {
    // The current date and time
    ZonedDateTime end = ZonedDateTime.now();
    ZonedDateTime begin = end.minusDays(90);
    String eventsUrl = String.format("https://outlook.office.com/api/v2.0/me/calendars/%s/calendarview?startDateTime=%s&endDateTime=%s", calendarId, formatTime(begin), formatTime(end));
    logger.debug("calendar: {}", calendarId);
    // TODO: Determine why this URL works in the MS Oauth Playground but not here
    logger.debug("eventsUrl: {}", eventsUrl);
    // Make requests for events
    HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(eventsUrl));
    HttpResponse response;
    try {
        response = getRequest.execute();
    } catch (HttpResponseException e) {
        logger.debug("Error fetching content");
        logger.debug("response status code: {}", e.getStatusCode());
        logger.debug("response status message: {}", e.getStatusMessage());
        logger.debug("response headers: {}", e.getHeaders());
        logger.debug("response content: {}", e.getContent());
        e.printStackTrace();
        throw e;
    }
    int statusCode = response.getStatusCode();
    if (statusCode != 200) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    // TODO: Parse with JSON and add to model
    // Currently this is not working as it is not returning events.
    logger.debug("response headers: {}", response.getHeaders());
    logger.debug("response status message: {}", response.getStatusMessage());
    logger.debug("events response: {}", response.parseAsString());
    // TODO(chuy): return actual results here.
    return null;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) ZonedDateTime(java.time.ZonedDateTime) 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)

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