use of com.google.api.services.discovery.model.RestDescription 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.RestDescription in project endpoints-java by cloudendpoints.
the class DiscoveryGenerator method generateDirectory.
private DirectoryList generateDirectory(Map<ApiKey, RestDescription> discoveryDocs, ImmutableSet<ApiKey> preferred, DiscoveryContext context) {
DirectoryList directory = new DirectoryList().setDiscoveryVersion("v1").setKind("discovery#directoryList");
List<Items> items = Lists.newArrayList();
for (Map.Entry<ApiKey, RestDescription> entry : discoveryDocs.entrySet()) {
RestDescription doc = entry.getValue();
String relativePath = "/apis/" + doc.getName() + "/" + doc.getVersion() + "/rest";
items.add(new Items().setDescription(doc.getDescription()).setDiscoveryLink("." + relativePath).setDiscoveryRestUrl(context.getApiRoot() + "/discovery/v1" + relativePath).setIcons(new Icons().setX16("https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png").setX32("https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png")).setId(doc.getName() + ":" + doc.getVersion()).setKind("discovery#directoryItem").setName(doc.getName()).setPreferred(preferred.contains(entry.getKey())).setTitle(doc.getTitle()).setVersion(doc.getVersion()).setDocumentationLink(doc.getDocumentationLink()));
}
return directory.setItems(items);
}
use of com.google.api.services.discovery.model.RestDescription in project endpoints-java by cloudendpoints.
the class DiscoveryGenerator method writeDiscovery.
public Result writeDiscovery(Iterable<ApiConfig> configs, DiscoveryContext context, SchemaRepository schemaRepository) {
ImmutableListMultimap<ApiKey, ApiConfig> configsByKey = Multimaps.index(configs, new Function<ApiConfig, ApiKey>() {
@Override
public ApiKey apply(ApiConfig config) {
return config.getApiKey();
}
});
ImmutableMap.Builder<ApiKey, RestDescription> builder = ImmutableMap.builder();
// "Default" API versions were determined automagically in legacy endpoints.
// This version only allows to remove an API from default ones by adding
// defaultVersion = AnnotationBoolean.FALSE to @Api
ImmutableSet.Builder<ApiKey> preferred = ImmutableSet.builder();
for (ApiKey apiKey : configsByKey.keySet()) {
ImmutableList<ApiConfig> apiConfigs = configsByKey.get(apiKey);
builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository));
// last config takes precedence (same as writeApi)
if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) {
preferred.add(apiKey);
}
}
ImmutableMap<ApiKey, RestDescription> discoveryDocs = builder.build();
return Result.builder().setDiscoveryDocs(discoveryDocs).setDirectory(generateDirectory(discoveryDocs, preferred.build(), context)).build();
}
use of com.google.api.services.discovery.model.RestDescription in project endpoints-java by cloudendpoints.
the class LocalDiscoveryProviderTest method getRestDocument.
@Test
public void getRestDocument() throws Exception {
RestDescription doc = provider.getRestDocument(ROOT, NAME, VERSION);
assertThat(doc.getBaseUrl()).isEqualTo("https://root.appspot.com/api/root/v1/");
assertThat(doc.getRootUrl()).isEqualTo("https://root.appspot.com/api/");
}
use of com.google.api.services.discovery.model.RestDescription in project endpoints-java by cloudendpoints.
the class DiscoveryGeneratorTest method testWriteDiscovery_FooEndpoint.
@Test
public void testWriteDiscovery_FooEndpoint() throws Exception {
RestDescription doc = getDiscovery(context, FooEndpoint.class);
RestDescription expected = readExpectedAsDiscovery("foo_endpoint.json");
compareDiscovery(expected, doc);
}
Aggregations