Search in sources :

Example 1 with Discovery

use of com.google.api.services.discovery.Discovery in project java-docs-samples by GoogleCloudPlatform.

the class OnlinePredictionSample method main.

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());
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
    HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);
    String response = request.execute().parseAsString();
    System.out.println(response);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonSchema(com.google.api.services.discovery.model.JsonSchema) JsonFactory(com.google.api.client.json.JsonFactory) Discovery(com.google.api.services.discovery.Discovery) RestDescription(com.google.api.services.discovery.model.RestDescription) GenericUrl(com.google.api.client.http.GenericUrl) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) FileContent(com.google.api.client.http.FileContent) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) File(java.io.File) HttpContent(com.google.api.client.http.HttpContent) RestMethod(com.google.api.services.discovery.model.RestMethod)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 FileContent (com.google.api.client.http.FileContent)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Discovery (com.google.api.services.discovery.Discovery)1 JsonSchema (com.google.api.services.discovery.model.JsonSchema)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1 RestMethod (com.google.api.services.discovery.model.RestMethod)1 File (java.io.File)1