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());
}
}
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);
}
}
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);
}
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));
}
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;
}
Aggregations