Search in sources :

Example 1 with RestDescription

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);
}
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 RestDescription

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);
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) Icons(com.google.api.services.discovery.model.DirectoryList.Items.Icons) Items(com.google.api.services.discovery.model.DirectoryList.Items) RestDescription(com.google.api.services.discovery.model.RestDescription) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) DirectoryList(com.google.api.services.discovery.model.DirectoryList)

Example 3 with RestDescription

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();
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) ImmutableSet(com.google.common.collect.ImmutableSet) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) RestDescription(com.google.api.services.discovery.model.RestDescription) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with RestDescription

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/");
}
Also used : RestDescription(com.google.api.services.discovery.model.RestDescription) Test(org.junit.Test)

Example 5 with RestDescription

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);
}
Also used : RestDescription(com.google.api.services.discovery.model.RestDescription) Test(org.junit.Test)

Aggregations

RestDescription (com.google.api.services.discovery.model.RestDescription)23 Test (org.junit.Test)16 DiscoveryContext (com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext)10 ApiKey (com.google.api.server.spi.config.model.ApiKey)5 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)3 JsonSchema (com.google.api.services.discovery.model.JsonSchema)2 File (java.io.File)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 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 ServiceContext (com.google.api.server.spi.ServiceContext)1