Search in sources :

Example 1 with RestMethod

use of com.google.api.services.discovery.model.RestMethod 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)

Example 2 with RestMethod

use of com.google.api.services.discovery.model.RestMethod in project endpoints-java by cloudendpoints.

the class DiscoveryGenerator method writeApiMethod.

private void writeApiMethod(ApiConfig config, String servicePath, RestDescription doc, ApiMethodConfig methodConfig, SchemaRepository repo) {
    List<String> parts = DOT_SPLITTER.splitToList(methodConfig.getFullMethodName());
    Map<String, RestMethod> methods = getMethodMapFromDoc(doc, parts);
    Map<String, JsonSchema> parameters = convertMethodParameters(methodConfig);
    RestMethod method = new RestMethod().setDescription(methodConfig.getDescription()).setHttpMethod(methodConfig.getHttpMethod()).setId(methodConfig.getFullMethodName()).setPath(methodConfig.getCanonicalPath().substring(servicePath.length())).setScopes(AuthScopeExpressions.encodeMutable(methodConfig.getScopeExpression()));
    List<String> parameterOrder = computeParameterOrder(methodConfig);
    if (!parameterOrder.isEmpty()) {
        method.setParameterOrder(parameterOrder);
    }
    if (!parameters.isEmpty()) {
        method.setParameters(parameters);
    }
    ApiParameterConfig requestParamConfig = getAndCheckMethodRequestResource(methodConfig);
    if (requestParamConfig != null) {
        TypeToken<?> requestType = requestParamConfig.getSchemaBaseType();
        Schema schema = repo.getOrAdd(requestType, config);
        method.setRequest(new Request().set$ref(schema.name()).setParameterName("resource"));
    }
    if (methodConfig.hasResourceInResponse()) {
        TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), config);
        Schema schema = repo.getOrAdd(returnType, config);
        method.setResponse(new Response().set$ref(schema.name()));
    }
    methods.put(parts.get(parts.size() - 1), method);
}
Also used : Response(com.google.api.services.discovery.model.RestMethod.Response) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Schema(com.google.api.server.spi.config.model.Schema) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Request(com.google.api.services.discovery.model.RestMethod.Request) RestMethod(com.google.api.services.discovery.model.RestMethod)

Example 3 with RestMethod

use of com.google.api.services.discovery.model.RestMethod in project endpoints-java by cloudendpoints.

the class DiscoveryGenerator method getMethodMapFromDoc.

/**
 * Gets the correct map in a {@link RestDescription} to add a method to, based on its name. This
 * is slightly complicated due to the fact that methods can exist outside of resources and on the
 * {@link RestDescription} object itself.
 */
private Map<String, RestMethod> getMethodMapFromDoc(RestDescription doc, List<String> parts) {
    if (parts.size() == 2) {
        if (doc.getMethods() == null) {
            doc.setMethods(new TreeMap<String, RestMethod>());
        }
        return doc.getMethods();
    }
    RestResource resource = null;
    Map<String, RestResource> resources = doc.getResources();
    if (resources == null) {
        resources = new TreeMap<>();
        doc.setResources(resources);
    }
    for (int i = 1; i < parts.size() - 1; i++) {
        String part = parts.get(i);
        if (resources == null) {
            resources = new TreeMap<>();
            resource.setResources(resources);
        }
        resource = resources.get(part);
        if (resource == null) {
            resource = new RestResource();
            resources.put(part, resource);
        }
        resources = resource.getResources();
    }
    if (resource.getMethods() == null) {
        resource.setMethods(new TreeMap<String, RestMethod>());
    }
    return resource.getMethods();
}
Also used : RestResource(com.google.api.services.discovery.model.RestResource) RestMethod(com.google.api.services.discovery.model.RestMethod)

Example 4 with RestMethod

use of com.google.api.services.discovery.model.RestMethod in project endpoints-java by cloudendpoints.

the class DiscoveryGenerator method writeApiMethod.

private void writeApiMethod(ApiConfig config, String servicePath, RestDescription doc, ApiMethodConfig methodConfig, SchemaRepository schemaRepo, AuthScopeRepository scopeRepo) {
    List<String> parts = DOT_SPLITTER.splitToList(methodConfig.getFullMethodName());
    Map<String, RestMethod> methods = getMethodMapFromDoc(doc, parts);
    Map<String, JsonSchema> parameters = convertMethodParameters(methodConfig);
    AuthScopeExpression scopeExpression = methodConfig.getScopeExpression();
    RestMethod method = new RestMethod().setDescription(methodConfig.getDescription()).setHttpMethod(methodConfig.getHttpMethod()).setId(methodConfig.getFullMethodName()).setPath(methodConfig.getCanonicalPath().substring(servicePath.length())).setScopes(AuthScopeExpressions.encodeMutable(scopeExpression));
    scopeRepo.add(scopeExpression);
    List<String> parameterOrder = computeParameterOrder(methodConfig);
    if (!parameterOrder.isEmpty()) {
        method.setParameterOrder(parameterOrder);
    }
    if (!parameters.isEmpty()) {
        method.setParameters(parameters);
    }
    ApiParameterConfig requestParamConfig = getAndCheckMethodRequestResource(methodConfig);
    if (requestParamConfig != null) {
        TypeToken<?> requestType = requestParamConfig.getSchemaBaseType();
        Schema schema = schemaRepo.getOrAdd(requestType, config);
        method.setRequest(new Request().set$ref(schema.name()).setParameterName("resource"));
    }
    if (methodConfig.hasResourceInResponse()) {
        TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), config);
        Schema schema = schemaRepo.getOrAdd(returnType, config);
        method.setResponse(new Response().set$ref(schema.name()));
    }
    methods.put(parts.get(parts.size() - 1), method);
}
Also used : Response(com.google.api.services.discovery.model.RestMethod.Response) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) AuthScopeExpression(com.google.api.server.spi.config.scope.AuthScopeExpression) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Schema(com.google.api.server.spi.config.model.Schema) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Request(com.google.api.services.discovery.model.RestMethod.Request) RestMethod(com.google.api.services.discovery.model.RestMethod)

Aggregations

RestMethod (com.google.api.services.discovery.model.RestMethod)4 JsonSchema (com.google.api.services.discovery.model.JsonSchema)3 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)2 Schema (com.google.api.server.spi.config.model.Schema)2 Request (com.google.api.services.discovery.model.RestMethod.Request)2 Response (com.google.api.services.discovery.model.RestMethod.Response)2 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 AuthScopeExpression (com.google.api.server.spi.config.scope.AuthScopeExpression)1 Discovery (com.google.api.services.discovery.Discovery)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1 RestResource (com.google.api.services.discovery.model.RestResource)1 File (java.io.File)1