Search in sources :

Example 1 with JsonHttpContent

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

the class HttpStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) {
    try {
        Insert req = storage.objects().insert(object.getBucket(), object);
        GenericUrl url = req.buildHttpRequest().getUrl();
        String scheme = url.getScheme();
        String host = url.getHost();
        String path = "/upload" + url.getRawPath();
        url = new GenericUrl(scheme + "://" + host + path);
        url.set("uploadType", "resumable");
        url.set("name", object.getName());
        for (Option option : options.keySet()) {
            Object content = option.get(options);
            if (content != null) {
                url.set(option.value(), content.toString());
            }
        }
        JsonFactory jsonFactory = storage.getJsonFactory();
        HttpRequestFactory requestFactory = storage.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
        HttpHeaders requestHeaders = httpRequest.getHeaders();
        requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
        String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
        if (key != null) {
            BaseEncoding base64 = BaseEncoding.base64();
            HashFunction hashFunction = Hashing.sha256();
            requestHeaders.set("x-goog-encryption-algorithm", "AES256");
            requestHeaders.set("x-goog-encryption-key", key);
            requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
        }
        HttpResponse response = httpRequest.execute();
        if (response.getStatusCode() != 200) {
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(response.getStatusCode());
            error.setMessage(response.getStatusMessage());
            throw translate(error);
        }
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with JsonHttpContent

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

the class HttpTranslateRpc method detect.

@Override
public List<List<DetectionsResourceItems>> detect(List<String> texts) {
    try {
        Map<String, ?> content = ImmutableMap.of("q", texts);
        HttpRequest httpRequest = translate.getRequestFactory().buildPostRequest(buildTargetUrl("detect"), new JsonHttpContent(translate.getJsonFactory(), content)).setParser(translate.getObjectParser());
        List<List<DetectionsResourceItems>> detections = httpRequest.execute().parseAs(DetectionsListResponse.class).getDetections();
        //    translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
        return detections != null ? detections : ImmutableList.<List<DetectionsResourceItems>>of();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) DetectionsResourceItems(com.google.api.services.translate.model.DetectionsResourceItems) DetectionsListResponse(com.google.api.services.translate.model.DetectionsListResponse) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException)

Example 3 with JsonHttpContent

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

the class HttpBigQueryRpc method open.

@Override
public String open(JobConfiguration configuration) {
    try {
        Job loadJob = new Job().setConfiguration(configuration);
        StringBuilder builder = new StringBuilder().append(BASE_RESUMABLE_URI).append(options.getProjectId()).append("/jobs");
        GenericUrl url = new GenericUrl(builder.toString());
        url.set("uploadType", "resumable");
        JsonFactory jsonFactory = bigquery.getJsonFactory();
        HttpRequestFactory requestFactory = bigquery.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, loadJob));
        httpRequest.getHeaders().set("X-Upload-Content-Value", "application/octet-stream");
        HttpResponse response = httpRequest.execute();
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Job(com.google.api.services.bigquery.model.Job)

Example 4 with JsonHttpContent

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

Aggregations

HttpRequest (com.google.api.client.http.HttpRequest)4 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)4 IOException (java.io.IOException)4 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 HttpResponse (com.google.api.client.http.HttpResponse)2 JsonFactory (com.google.api.client.json.JsonFactory)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 Job (com.google.api.services.bigquery.model.Job)1 Insert (com.google.api.services.storage.Storage.Objects.Insert)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 DetectionsListResponse (com.google.api.services.translate.model.DetectionsListResponse)1 DetectionsResourceItems (com.google.api.services.translate.model.DetectionsResourceItems)1 LanguagesListResponse (com.google.api.services.translate.model.LanguagesListResponse)1 LanguagesResource (com.google.api.services.translate.model.LanguagesResource)1 ImmutableList (com.google.common.collect.ImmutableList)1 HashFunction (com.google.common.hash.HashFunction)1 BaseEncoding (com.google.common.io.BaseEncoding)1