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);
}
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);
}
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();
}
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);
}
Aggregations