com.google.api.client.http.UriTemplate Java Examples

The following examples show how to use com.google.api.client.http.UriTemplate. 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: GoogleIdTokenAuth.java    From styx with Apache License 2.0 5 votes vote down vote up
private String getDefaultGCEIdToken(String targetAudience) throws IOException {
  // https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
  final String metadataHost = System.getenv().getOrDefault("GCE_METADATA_HOST", DEFAULT_GCE_METADATA_HOST);
  final String uriTemplate = "http://" + metadataHost + GCE_METADATA_IDENTITY_PATH;
  final String identityUri = UriTemplate.expand(uriTemplate, ImmutableMap.of(
      "audience", targetAudience,
      "format", "full"),
      false);
  return httpTransport.createRequestFactory()
      .buildGetRequest(new GenericUrl(identityUri))
      .setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"))
      .execute()
      .parseAsString();
}
 
Example #2
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 #3
Source File: AbstractGoogleClientRequest.java    From google-api-java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance of {@link GenericUrl} suitable for use against this service.
 *
 * <p>
 * Subclasses may override by calling the super implementation.
 * </p>
 *
 * @return newly created {@link GenericUrl}
 */
public GenericUrl buildHttpRequestUrl() {
  return new GenericUrl(
      UriTemplate.expand(abstractGoogleClient.getBaseUrl(), uriTemplate, this, true));
}