Search in sources :

Example 21 with RestDescription

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

the class LocalDiscoveryProvider method ensureDiscoveryResult.

private synchronized void ensureDiscoveryResult() {
    if (discoveryDocs == null) {
        DiscoveryGenerator.Result result = generator.writeDiscovery(getAllApiConfigs(), new DiscoveryContext().setApiRoot(PLACEHOLDER_ROOT), repository);
        directoryList = result.directory();
        ImmutableMap.Builder<ApiKey, RestDescription> builder = ImmutableMap.builder();
        for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {
            ApiKey rootedKey = entry.getKey();
            builder.put(new ApiKey(rootedKey.getName(), rootedKey.getVersion(), null), entry.getValue());
        }
        discoveryDocs = builder.build();
    }
}
Also used : DiscoveryContext(com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext) ApiKey(com.google.api.server.spi.config.model.ApiKey) RestDescription(com.google.api.services.discovery.model.RestDescription) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 22 with RestDescription

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

the class DiscoveryGenerator method writeApi.

private RestDescription writeApi(ApiKey apiKey, Iterable<ApiConfig> apiConfigs, DiscoveryContext context, SchemaRepository repo) {
    // The first step is to scan all methods and try to extract a base path, aka a common prefix
    // for all methods. This prefix must end in a slash and can't contain any path parameters.
    String servicePath = computeApiServicePath(apiConfigs);
    String basePath = context.basePath + "/" + servicePath;
    RestDescription doc = REST_SKELETON.clone().setBasePath(basePath).setBaseUrl(context.getApiRoot() + "/" + servicePath).setId(apiKey.getName() + ":" + apiKey.getVersion()).setName(apiKey.getName()).setRootUrl(context.getApiRoot() + "/").setServicePath(servicePath).setVersion(apiKey.getVersion());
    for (ApiConfig config : apiConfigs) {
        // precedence here if there happens to be divergence.
        if (config.getDescription() != null) {
            doc.setDescription(config.getDescription());
        }
        if (config.getTitle() != null) {
            doc.setTitle(config.getTitle());
        }
        if (config.getDocumentationLink() != null) {
            doc.setDocumentationLink(config.getDocumentationLink());
        }
        if (config.getNamespaceConfig() != null) {
            ApiNamespaceConfig namespaceConfig = config.getNamespaceConfig();
            if (!Strings.isEmptyOrWhitespace(namespaceConfig.getOwnerName())) {
                doc.setOwnerName(namespaceConfig.getOwnerName());
            }
            if (!Strings.isEmptyOrWhitespace(namespaceConfig.getOwnerDomain())) {
                doc.setOwnerDomain(namespaceConfig.getOwnerDomain());
            }
            if (!Strings.isEmptyOrWhitespace(namespaceConfig.getPackagePath())) {
                doc.setPackagePath(namespaceConfig.getPackagePath());
            }
        }
        if (config.getCanonicalName() != null) {
            doc.setCanonicalName(config.getCanonicalName());
        }
        for (ApiMethodConfig methodConfig : config.getApiClassConfig().getMethods().values()) {
            if (!methodConfig.isIgnored()) {
                writeApiMethod(config, servicePath, doc, methodConfig, repo);
            }
        }
    }
    List<Schema> schemas = repo.getAllSchemaForApi(apiKey);
    if (!schemas.isEmpty()) {
        Map<String, JsonSchema> docSchemas = Maps.newTreeMap();
        for (Schema schema : schemas) {
            docSchemas.put(schema.name(), convertToDiscoverySchema(schema));
        }
        doc.setSchemas(docSchemas);
    }
    return doc;
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Schema(com.google.api.server.spi.config.model.Schema) JsonSchema(com.google.api.services.discovery.model.JsonSchema) JsonSchema(com.google.api.services.discovery.model.JsonSchema) RestDescription(com.google.api.services.discovery.model.RestDescription) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) ApiNamespaceConfig(com.google.api.server.spi.config.model.ApiNamespaceConfig)

Example 23 with RestDescription

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

the class LocalDiscoveryProvider method getRestDocument.

@Override
public RestDescription getRestDocument(String root, String name, String version) throws NotFoundException {
    ensureDiscoveryResult();
    RestDescription doc = discoveryDocs.get(new ApiKey(name, version, null));
    if (doc == null) {
        throw new NotFoundException("Not Found");
    }
    return replaceRoot(doc, root);
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) NotFoundException(com.google.api.server.spi.response.NotFoundException) RestDescription(com.google.api.services.discovery.model.RestDescription)

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