Search in sources :

Example 21 with GenericUrl

use of com.google.api.client.http.GenericUrl 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 22 with GenericUrl

use of com.google.api.client.http.GenericUrl in project beam by apache.

the class UploadIdResponseInterceptor method interceptResponse.

@Override
public void interceptResponse(HttpResponse response) throws IOException {
    if (!LOG.isDebugEnabled()) {
        return;
    }
    String uploadId = response.getHeaders().getFirstHeaderStringValue(UPLOAD_HEADER);
    if (uploadId == null) {
        return;
    }
    GenericUrl url = response.getRequest().getUrl();
    // The check for upload type makes sure this is an upload and not a read.
    if (url.get(UPLOAD_ID_PARAM) == null && url.get(UPLOAD_TYPE_PARAM) != null) {
        LOG.debug("Upload ID for url {} on worker {} is {}", url, System.getProperty("worker_id"), uploadId);
    }
}
Also used : GenericUrl(com.google.api.client.http.GenericUrl)

Example 23 with GenericUrl

use of com.google.api.client.http.GenericUrl in project beam by apache.

the class UploadIdResponseInterceptorTest method buildHttpResponse.

/**
   * Builds a HttpResponse with the given string response.
   *
   * @param header header value to provide or null if none.
   * @param uploadId upload id to provide in the url upload id param or null if none.
   * @param uploadType upload type to provide in url upload type param or null if none.
   * @return HttpResponse with the given parameters
   * @throws IOException
   */
private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType) throws IOException {
    MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
    MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
    builder.setLowLevelHttpResponse(resp);
    resp.setStatusCode(200);
    GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
    if (header != null) {
        resp.addHeader("X-GUploader-UploadID", header);
    }
    if (uploadId != null) {
        url.put("upload_id", uploadId);
    }
    if (uploadType != null) {
        url.put("uploadType", uploadType);
    }
    return builder.build().createRequestFactory().buildGetRequest(url).execute();
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl)

Example 24 with GenericUrl

use of com.google.api.client.http.GenericUrl in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadAllFiles.

public void downloadAllFiles(String path) {
    List<File> files = listOwnerFiles();
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            InputStream input = resp.getContent();
            java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 25 with GenericUrl

use of com.google.api.client.http.GenericUrl in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadFilesAfterDate.

public void downloadFilesAfterDate(String path, String stringDateLastChange) {
    List<File> files = listOwnerFilesAfterDate(stringDateLastChange);
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            InputStream input = resp.getContent();
            java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Aggregations

GenericUrl (com.google.api.client.http.GenericUrl)28 HttpResponse (com.google.api.client.http.HttpResponse)19 HttpRequest (com.google.api.client.http.HttpRequest)15 IOException (java.io.IOException)15 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)8 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)7 InputStream (java.io.InputStream)7 HttpTransport (com.google.api.client.http.HttpTransport)6 HttpResponseException (com.google.api.client.http.HttpResponseException)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 File (com.google.api.services.drive.model.File)4 GeneralSecurityException (java.security.GeneralSecurityException)4 MockGoogleCredential (com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential)3 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 JsonFactory (com.google.api.client.json.JsonFactory)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 MockSleeper (com.google.api.client.testing.util.MockSleeper)3