Search in sources :

Example 1 with ScopesElement

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

the class DiscoveryGenerator method writeApi.

private RestDescription writeApi(ApiKey apiKey, Iterable<ApiConfig> apiConfigs, DiscoveryContext context, SchemaRepository schemaRepo) {
    // 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());
    final AuthScopeRepository scopeRepo = new AuthScopeRepository();
    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());
        }
        scopeRepo.add(config.getScopeExpression());
        for (ApiMethodConfig methodConfig : config.getApiClassConfig().getMethods().values()) {
            if (!methodConfig.isIgnored()) {
                writeApiMethod(config, servicePath, doc, methodConfig, schemaRepo, scopeRepo);
            }
        }
    }
    Map<String, ScopesElement> scopeElements = new LinkedHashMap<>();
    for (Entry<String, String> entry : scopeRepo.getDescriptionsByScope().entrySet()) {
        scopeElements.put(entry.getKey(), new ScopesElement().setDescription(entry.getValue()));
    }
    doc.setAuth(new Auth().setOauth2(new Oauth2().setScopes(scopeElements)));
    List<Schema> schemas = schemaRepo.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 : ScopesElement(com.google.api.services.discovery.model.RestDescription.Auth.Oauth2.ScopesElement) Oauth2(com.google.api.services.discovery.model.RestDescription.Auth.Oauth2) 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) LinkedHashMap(java.util.LinkedHashMap) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Auth(com.google.api.services.discovery.model.RestDescription.Auth) AuthScopeRepository(com.google.api.server.spi.config.model.AuthScopeRepository)

Aggregations

ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)1 ApiNamespaceConfig (com.google.api.server.spi.config.model.ApiNamespaceConfig)1 AuthScopeRepository (com.google.api.server.spi.config.model.AuthScopeRepository)1 Schema (com.google.api.server.spi.config.model.Schema)1 JsonSchema (com.google.api.services.discovery.model.JsonSchema)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1 Auth (com.google.api.services.discovery.model.RestDescription.Auth)1 Oauth2 (com.google.api.services.discovery.model.RestDescription.Auth.Oauth2)1 ScopesElement (com.google.api.services.discovery.model.RestDescription.Auth.Oauth2.ScopesElement)1 LinkedHashMap (java.util.LinkedHashMap)1