Search in sources :

Example 11 with JsonFactory

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

Example 12 with JsonFactory

use of com.google.api.client.json.JsonFactory in project beam by apache.

the class BigqueryMatcher method newBigqueryClient.

@VisibleForTesting
Bigquery newBigqueryClient(String applicationName) {
    HttpTransport transport = Transport.getTransport();
    JsonFactory jsonFactory = Transport.getJsonFactory();
    Credentials credential = getDefaultCredential();
    return new Bigquery.Builder(transport, jsonFactory, new HttpCredentialsAdapter(credential)).setApplicationName(applicationName).build();
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with JsonFactory

use of com.google.api.client.json.JsonFactory in project gatk by broadinstitute.

the class ReferenceAPISource method writeObject.

// TODO: Move these to a CustomCoder. That will allow us to do something else (possibly better) for Spark.
// TODO: See Issue #849.
// implement methods for Java serialization, since Reference does not implement Serializable
private void writeObject(ObjectOutputStream stream) throws IOException {
    JsonFactory jsonFactory = com.google.api.client.googleapis.util.Utils.getDefaultJsonFactory();
    stream.writeInt(referenceMap.size());
    for (Map.Entry<String, Reference> e : referenceMap.entrySet()) {
        stream.writeUTF(e.getKey());
        stream.writeUTF(jsonFactory.toString(e.getValue()));
    }
    stream.writeObject(referenceNameToIdTable);
    stream.writeObject(apiKey);
}
Also used : JsonFactory(com.google.api.client.json.JsonFactory)

Aggregations

JsonFactory (com.google.api.client.json.JsonFactory)13 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)6 HttpRequest (com.google.api.client.http.HttpRequest)5 HttpTransport (com.google.api.client.http.HttpTransport)5 HttpResponse (com.google.api.client.http.HttpResponse)4 GenericJson (com.google.api.client.json.GenericJson)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4 GenericUrl (com.google.api.client.http.GenericUrl)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)3 Storage (com.google.api.services.storage.Storage)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)2 IOException (java.io.IOException)2 Test (org.junit.Test)2