Search in sources :

Example 26 with HttpRequest

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

the class HttpTransportOptions method getHttpRequestInitializer.

/**
   * Returns a request initializer responsible for initializing requests according to service
   * options.
   */
public HttpRequestInitializer getHttpRequestInitializer(final ServiceOptions<?, ?> serviceOptions) {
    Credentials scopedCredentials = serviceOptions.getScopedCredentials();
    final HttpRequestInitializer delegate = scopedCredentials != null && scopedCredentials != NoCredentials.getInstance() ? new HttpCredentialsAdapter(scopedCredentials) : null;
    return new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            if (delegate != null) {
                delegate.initialize(httpRequest);
            }
            if (connectTimeout >= 0) {
                httpRequest.setConnectTimeout(connectTimeout);
            }
            if (readTimeout >= 0) {
                httpRequest.setReadTimeout(readTimeout);
            }
            HttpHeaders headers = httpRequest.getHeaders();
            headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
        }
    };
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) NoCredentials(com.google.cloud.NoCredentials) Credentials(com.google.auth.Credentials)

Example 27 with HttpRequest

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

the class HttpDatastoreRpc method getHttpRequestInitializer.

private HttpRequestInitializer getHttpRequestInitializer(final DatastoreOptions options, HttpTransportOptions httpTransportOptions) {
    final HttpRequestInitializer delegate = httpTransportOptions.getHttpRequestInitializer(options);
    return new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            delegate.initialize(httpRequest);
            httpRequest.getHeaders().setUserAgent(options.getApplicationName());
        }
    };
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 28 with HttpRequest

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

the class HttpTranslateRpc method listSupportedLanguages.

@Override
public List<LanguagesResource> listSupportedLanguages(Map<Option, ?> optionMap) {
    try {
        Map<String, ?> content = ImmutableMap.of("target", firstNonNull(Option.TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()));
        HttpRequest httpRequest = translate.getRequestFactory().buildPostRequest(buildTargetUrl("languages"), new JsonHttpContent(translate.getJsonFactory(), content)).setParser(translate.getObjectParser());
        List<LanguagesResource> languages = httpRequest.execute().parseAs(LanguagesListResponse.class).getLanguages();
        //     .execute().getLanguages();
        return languages != null ? languages : ImmutableList.<LanguagesResource>of();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) LanguagesListResponse(com.google.api.services.translate.model.LanguagesListResponse) LanguagesResource(com.google.api.services.translate.model.LanguagesResource) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException)

Example 29 with HttpRequest

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

the class HttpStorageRpc method write.

@Override
public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
    try {
        if (length == 0 && !last) {
            return;
        }
        GenericUrl url = new GenericUrl(uploadId);
        HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
        long limit = destOffset + length;
        StringBuilder range = new StringBuilder("bytes ");
        if (length == 0) {
            range.append('*');
        } else {
            range.append(destOffset).append('-').append(limit - 1);
        }
        range.append('/');
        if (last) {
            range.append(limit);
        } else {
            range.append('*');
        }
        httpRequest.getHeaders().setContentRange(range.toString());
        int code;
        String message;
        IOException exception = null;
        try {
            HttpResponse response = httpRequest.execute();
            code = response.getStatusCode();
            message = response.getStatusMessage();
        } catch (HttpResponseException ex) {
            exception = ex;
            code = ex.getStatusCode();
            message = ex.getStatusMessage();
        }
        if (!last && code != 308 || last && !(code == 200 || code == 201)) {
            if (exception != null) {
                throw exception;
            }
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(code);
            error.setMessage(message);
            throw translate(error);
        }
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 30 with HttpRequest

use of com.google.api.client.http.HttpRequest 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)

Aggregations

HttpRequest (com.google.api.client.http.HttpRequest)31 IOException (java.io.IOException)21 HttpResponse (com.google.api.client.http.HttpResponse)19 GenericUrl (com.google.api.client.http.GenericUrl)15 HttpTransport (com.google.api.client.http.HttpTransport)9 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)8 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)8 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)7 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)6 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)5 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)5 JsonFactory (com.google.api.client.json.JsonFactory)5 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)5 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 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)4