Java Code Examples for com.google.api.client.http.HttpRequestFactory#buildRequest()

The following examples show how to use com.google.api.client.http.HttpRequestFactory#buildRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleJsonResponseExceptionHelper.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
public static HttpResponse createHttpResponse(int statusCode, InputStream content)
    throws IOException {
  FakeHttpTransport transport = new FakeHttpTransport(statusCode, content);
  HttpRequestFactory factory = transport.createRequestFactory();
  HttpRequest request =
      factory.buildRequest(
          "foo", new GenericUrl("http://example.com/bar"), new EmptyHttpContent());
  request.setThrowExceptionOnExecuteError(false);
  return request.execute();
}
 
Example 2
Source File: GoogleJsonResponseExceptionHelper.java    From nomulus with Apache License 2.0 5 votes vote down vote up
public static HttpResponse createHttpResponse(int statusCode, InputStream content)
    throws IOException {
  FakeHttpTransport transport = new FakeHttpTransport(statusCode, content);
  HttpRequestFactory factory = transport.createRequestFactory();
  HttpRequest request =
      factory.buildRequest(
          "foo", new GenericUrl("http://example.com/bar"), new EmptyHttpContent());
  request.setThrowExceptionOnExecuteError(false);
  return request.execute();
}
 
Example 3
Source File: OnlinePredictionSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();

  RestDescription api = discovery.apis().getRest("ml", "v1").execute();
  RestMethod method = api.getResources().get("projects").getMethods().get("predict");

  JsonSchema param = new JsonSchema();
  String projectId = "YOUR_PROJECT_ID";
  // You should have already deployed a model and a version.
  // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
  String modelId = "YOUR_MODEL_ID";
  String versionId = "YOUR_VERSION_ID";
  param.set(
      "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

  GenericUrl url =
      new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
  System.out.println(url);

  String contentType = "application/json";
  File requestBodyFile = new File("input.txt");
  HttpContent content = new FileContent(contentType, requestBodyFile);
  System.out.println(content.getLength());

  List<String> scopes = new ArrayList<>();
  scopes.add("https://www.googleapis.com/auth/cloud-platform");

  GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(scopes);
  HttpRequestFactory requestFactory =
      httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));
  HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

  String response = request.execute().parseAsString();
  System.out.println(response);
}
 
Example 4
Source File: AbstractOAuthGetToken.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the HTTP request for a temporary or long-lived token.
 *
 * @return OAuth credentials response object
 */
public final OAuthCredentialsResponse execute() throws IOException {
  HttpRequestFactory requestFactory = transport.createRequestFactory();
  HttpRequest request =
      requestFactory.buildRequest(usePost ? HttpMethods.POST : HttpMethods.GET, this, null);
  createParameters().intercept(request);
  HttpResponse response = request.execute();
  response.setContentLoggingLimit(0);
  OAuthCredentialsResponse oauthResponse = new OAuthCredentialsResponse();
  UrlEncodedParser.parse(response.parseAsString(), oauthResponse);
  return oauthResponse;
}