use of com.google.api.client.http.HttpResponse in project elasticsearch by elastic.
the class RetryHttpInitializerWrapperTests method testIOExceptionRetry.
public void testIOExceptionRetry() throws Exception {
FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 1, true);
MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
MockSleeper mockSleeper = new MockSleeper();
RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueMillis(500));
Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
HttpResponse response = request.execute();
assertThat(mockSleeper.getCount(), equalTo(1));
assertThat(response.getStatusCode(), equalTo(200));
}
use of com.google.api.client.http.HttpResponse in project AndroidSDK-RecipeBook by gabu.
the class EditActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
mEditText = (EditText) findViewById(R.id.edit_text);
// インテントからIDを取得
Intent intent = getIntent();
mDocId = intent.getStringExtra("docId");
// GoogleTransportを取得して
mTransport = Recipe098.getTransport();
// URLを生成して
String url = GOOGLE_DOCS_API_URL + "download/documents/Export?docId=" + mDocId + "&exportFormat=txt";
// GoogleTransportからGETリクエストを生成
HttpRequest request = mTransport.buildGetRequest();
// URLをセット
request.setUrl(url);
HttpResponse response;
try {
// HTTPリクエストを実行!
response = request.execute();
// HTTPレスポンスをStringにパースします。
// Googleドキュメントの文書をtxtでダウンロードすると
// 改行コードが"\r\n"なので"\n"に置換します。
mEditText.setText(response.parseAsString().replaceAll("\r\n", "\n"));
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.google.api.client.http.HttpResponse in project AndroidSDK-RecipeBook by gabu.
the class Recipe101 method debug.
// リクエストを実行して、結果をログ出力します。
private void debug(HttpRequest request, String url) {
try {
Log.d(TAG, url);
request.setUrl(url);
HttpResponse response = RedirectHandler.execute(request);
CalendarEventFeed eventFeed = response.parseAs(CalendarEventFeed.class);
for (CalendarEventEntry event : eventFeed.entries) {
Log.d(TAG, event.title);
if (event.when != null) {
Log.d(TAG, event.when.startTime);
}
}
} catch (IOException e) {
handleException(e);
}
}
use of com.google.api.client.http.HttpResponse in project beam by apache.
the class GcsUtilTest 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 beam by apache.
the class RetryHttpInitializerWrapper method initialize.
/**
* Initializes the given request.
*/
@Override
public final void initialize(final HttpRequest request) {
// 2 minutes read timeout
request.setReadTimeout(2 * ONEMINITUES);
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException {
if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
// and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of
// our internal backoff handler.
LOG.info("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
Aggregations