Search in sources :

Example 1 with Method

use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.

the class JavaDiscoGapicRequestToViewTransformer method generateRequestClass.

private StaticLangApiMessageView generateRequestClass(SchemaTransformationContext context, MethodModel method, RequestObjectParamView resourceNameView) {
    StaticLangApiMessageView.Builder requestView = StaticLangApiMessageView.newBuilder();
    SymbolTable symbolTable = SymbolTable.fromSeed(reservedKeywords);
    String requestClassId = context.getNamer().privateFieldName(DiscoGapicParser.getRequestName(((DiscoveryMethodModel) method).getDiscoMethod()));
    String requestName = nameFormatter.privateFieldName(Name.anyCamel(symbolTable.getNewSymbol(requestClassId)));
    boolean hasRequiredProperties = false;
    requestView.name(requestName);
    requestView.description(method.getDescription());
    String requestTypeName = nameFormatter.publicClassName(Name.anyCamel(requestClassId));
    requestView.typeName(requestTypeName);
    requestView.innerTypeName(requestTypeName);
    List<StaticLangApiMessageView> properties = new LinkedList<>();
    // Add the standard query parameters.
    for (String param : STANDARD_QUERY_PARAMS.keySet()) {
        if (method.getInputField(param) != null) {
            continue;
        }
        StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
        paramView.description(STANDARD_QUERY_PARAMS.get(param));
        paramView.name(symbolTable.getNewSymbol(param));
        paramView.typeName("String");
        paramView.innerTypeName("String");
        paramView.isRequired(false);
        paramView.canRepeat(false);
        paramView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(DiscoGapicParser.stringToName(param), SurfaceNamer.MapType.NOT_MAP, SurfaceNamer.Cardinality.NOT_REPEATED));
        paramView.fieldSetFunction(context.getDiscoGapicNamer().getResourceSetterName(param, SurfaceNamer.Cardinality.NOT_REPEATED, context.getNamer()));
        paramView.properties(Collections.emptyList());
        paramView.isRequestMessage(false);
        paramView.hasRequiredProperties(false);
        properties.add(paramView.build());
    }
    for (FieldModel entry : method.getInputFields()) {
        if (entry.mayBeInResourceName()) {
            hasRequiredProperties = true;
            continue;
        }
        String parameterName = entry.getNameAsParameter();
        properties.add(schemaToParamView(context, entry, parameterName, symbolTable, EscapeName.ESCAPE_NAME));
        if (entry.isRequired()) {
            hasRequiredProperties = true;
        }
    }
    StaticLangApiMessageView.Builder paramView = StaticLangApiMessageView.newBuilder();
    Method discoMethod = ((DiscoveryMethodModel) method).getDiscoMethod();
    String resourceName = DiscoGapicParser.getResourceIdentifier(discoMethod.path()).toLowerCamel();
    StringBuilder description = new StringBuilder(discoMethod.parameters().get(resourceName).description());
    description.append(String.format("\nIt must have the format `%s`. ", discoMethod.path()));
    description.append(String.format("\\`{%s}\\` must start with a letter,\n", resourceName));
    description.append("and contain only letters (\\`[A-Za-z]\\`), numbers (\\`[0-9]\\`), dashes (\\`-\\`),\n" + "     * underscores (\\`_\\`), periods (\\`.\\`), tildes (\\`~\\`), plus (\\`+\\`) or percent\n" + "     * signs (\\`%\\`). It must be between 3 and 255 characters in length, and it\n" + "     * must not start with \\`\"goog\"\\`.");
    paramView.description(description.toString());
    paramView.name(symbolTable.getNewSymbol(resourceNameView.name()));
    paramView.typeName("String");
    paramView.innerTypeName("String");
    paramView.isRequired(true);
    paramView.canRepeat(false);
    paramView.fieldGetFunction(resourceNameView.getCallName());
    paramView.fieldSetFunction(resourceNameView.setCallName());
    paramView.properties(new LinkedList<>());
    paramView.isRequestMessage(false);
    paramView.hasRequiredProperties(false);
    properties.add(paramView.build());
    Collections.sort(properties);
    requestView.canRepeat(false);
    requestView.isRequired(true);
    requestView.properties(properties);
    requestView.hasRequiredProperties(hasRequiredProperties);
    requestView.isRequestMessage(true);
    requestView.pathAsResourceName(resourceNameView);
    Schema requestBodyDef = ((DiscoveryMethodModel) method).getDiscoMethod().request();
    if (requestBodyDef != null && !Strings.isNullOrEmpty(requestBodyDef.reference())) {
        FieldModel requestBody = DiscoveryField.create(requestBodyDef, context.getDocContext().getApiModel());
        requestView.requestBodyType(schemaToParamView(context, requestBody, DiscoGapicParser.getSchemaNameAsParameter(requestBodyDef).toLowerCamel(), symbolTable, EscapeName.NO_ESCAPE_NAME));
    }
    return requestView.build();
}
Also used : Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) LinkedList(java.util.LinkedList) StaticLangApiMessageView(com.google.api.codegen.viewmodel.StaticLangApiMessageView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 2 with Method

use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.

the class ApiCallableTransformer method generateHttpFields.

private HttpMethodView generateHttpFields(MethodContext context) {
    if (context.getProductConfig().getTransportProtocol().equals(TransportProtocol.HTTP)) {
        Method method = ((DiscoveryMethodModel) context.getMethodModel()).getDiscoMethod();
        HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
        httpMethodView.fullMethodName(method.id());
        httpMethodView.httpMethod(method.httpMethod());
        List<String> pathParams = new ArrayList<>(method.pathParams().keySet());
        List<String> queryParams = new ArrayList<>(method.queryParams().keySet());
        Collections.sort(pathParams);
        Collections.sort(queryParams);
        httpMethodView.pathParams(pathParams);
        httpMethodView.queryParams(queryParams);
        httpMethodView.pathTemplate(method.path());
        // TODO(andrealin): handle multiple resource names.
        DiscoGapicInterfaceConfig interfaceConfig = (DiscoGapicInterfaceConfig) context.getSurfaceInterfaceContext().getInterfaceConfig();
        SingleResourceNameConfig nameConfig = interfaceConfig.methodToResourceNameMap().get(context.getMethodConfig());
        httpMethodView.resourceNameTypeName(context.getNamer().publicClassName(DiscoGapicParser.getResourceNameName(nameConfig)));
        // Find the field with the resource name config.
        for (FieldConfig fieldConfig : context.getMethodConfig().getRequiredFieldConfigs()) {
            if (fieldConfig.getResourceNameConfig() != null && fieldConfig.getResourceNameConfig().equals(nameConfig)) {
                httpMethodView.resourceNameFieldName(context.getNamer().privateFieldName(Name.anyCamel(fieldConfig.getField().getNameAsParameter())));
            }
        }
        return httpMethodView.build();
    } else {
        return null;
    }
}
Also used : FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 3 with Method

use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.

the class DiscoConfigTransformer method generateResourceNameGenerations.

private List<ResourceNameGenerationView> generateResourceNameGenerations(Document model, Map<Method, Name> methodToResourceNameAndPatternMap) {
    ImmutableList.Builder<ResourceNameGenerationView> resourceNames = ImmutableList.builder();
    for (Map.Entry<String, List<Method>> resource : model.resources().entrySet()) {
        for (Method method : resource.getValue()) {
            if (!Strings.isNullOrEmpty(method.path())) {
                ResourceNameGenerationView.Builder view = ResourceNameGenerationView.newBuilder();
                view.messageName(DiscoGapicParser.getRequestName(method).toUpperCamel());
                String parameterName = DiscoGapicParser.getResourceIdentifier(method.flatPath()).toLowerCamel();
                String resourceName = methodToResourceNameAndPatternMap.get(method).toLowerCamel();
                Map<String, String> fieldEntityMap = new HashMap<>();
                fieldEntityMap.put(parameterName, resourceName);
                view.fieldEntities(fieldEntityMap);
                resourceNames.add(view.build());
            }
        }
    }
    return resourceNames.build();
}
Also used : ResourceNameGenerationView(com.google.api.codegen.configgen.viewmodel.ResourceNameGenerationView) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 4 with Method

use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.

the class DiscoGapicInterfaceConfig method createMethodConfigMap.

private static ImmutableMap<String, DiscoGapicMethodConfig> createMethodConfigMap(DiscoApiModel model, String language, InterfaceConfigProto interfaceConfigProto, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {
    ImmutableMap.Builder<String, DiscoGapicMethodConfig> methodConfigMapBuilder = ImmutableMap.builder();
    for (MethodConfigProto methodConfigProto : interfaceConfigProto.getMethodsList()) {
        Method method = lookupMethod(model.getDocument(), methodConfigProto.getName());
        if (method == null) {
            model.getDiagCollector().addDiag(Diag.error(SimpleLocation.TOPLEVEL, "method not found: %s", methodConfigProto.getName()));
            continue;
        }
        DiscoGapicMethodConfig methodConfig = DiscoGapicMethodConfig.createDiscoGapicMethodConfig(model, language, methodConfigProto, method, messageConfigs, resourceNameConfigs, retryCodesConfigNames, retryParamsConfigNames);
        if (methodConfig == null) {
            continue;
        }
        methodConfigMapBuilder.put(methodConfigProto.getName(), methodConfig);
    }
    if (model.getDiagCollector().getErrorCount() > 0) {
        return null;
    } else {
        return methodConfigMapBuilder.build();
    }
}
Also used : MethodConfigProto(com.google.api.codegen.MethodConfigProto) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with Method

use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.

the class DiscoGapicInterfaceConfig method createInterfaceConfig.

static DiscoGapicInterfaceConfig createInterfaceConfig(DiscoApiModel model, String language, InterfaceConfigProto interfaceConfigProto, String interfaceNameOverride, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs) {
    ImmutableMap<String, ImmutableSet<String>> retryCodesDefinition = RetryDefinitionsTransformer.createRetryCodesDefinition(model.getDiagCollector(), interfaceConfigProto);
    ImmutableMap<String, RetrySettings> retrySettingsDefinition = RetryDefinitionsTransformer.createRetrySettingsDefinition(model.getDiagCollector(), interfaceConfigProto);
    List<DiscoGapicMethodConfig> methodConfigs = null;
    ImmutableMap<String, DiscoGapicMethodConfig> methodConfigMap = null;
    if (retryCodesDefinition != null && retrySettingsDefinition != null) {
        methodConfigMap = createMethodConfigMap(model, language, interfaceConfigProto, messageConfigs, resourceNameConfigs, retryCodesDefinition.keySet(), retrySettingsDefinition.keySet());
        methodConfigs = GapicInterfaceConfig.createMethodConfigs(methodConfigMap, interfaceConfigProto);
    }
    // TODO(andrealin)  Make non-null smokeTestConfig.
    SmokeTestConfig smokeTestConfig = null;
    // TODO(andrealin) IAM permissions configs.
    ImmutableList<String> requiredConstructorParams = ImmutableList.copyOf(interfaceConfigProto.getRequiredConstructorParamsList());
    for (String param : interfaceConfigProto.getRequiredConstructorParamsList()) {
        if (!CONSTRUCTOR_PARAMS.contains(param)) {
            model.getDiagCollector().addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Unsupported constructor param: %s", param));
        }
    }
    ImmutableList.Builder<SingleResourceNameConfig> resourcesBuilder = ImmutableList.builder();
    for (CollectionConfigProto collectionConfigProto : interfaceConfigProto.getCollectionsList()) {
        String entityName = collectionConfigProto.getEntityName();
        ResourceNameConfig resourceName = resourceNameConfigs.get(entityName);
        if (resourceName == null || !(resourceName instanceof SingleResourceNameConfig)) {
            model.getDiagCollector().addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Inconsistent configuration - single resource name %s specified for interface, " + " but was not found in GapicProductConfig configuration.", entityName));
            return null;
        }
        resourcesBuilder.add((SingleResourceNameConfig) resourceName);
    }
    ImmutableList<SingleResourceNameConfig> singleResourceNames = resourcesBuilder.build();
    ImmutableMap.Builder<MethodConfig, SingleResourceNameConfig> methodToSingleResourceNameMap = ImmutableMap.builder();
    if (methodConfigs != null) {
        for (MethodConfig methodConfig : methodConfigs) {
            Method method = ((DiscoveryMethodModel) methodConfig.getMethodModel()).getDiscoMethod();
            String canonicalMethodPath = DiscoGapicParser.getCanonicalPath(method);
            for (SingleResourceNameConfig nameConfig : singleResourceNames) {
                if (nameConfig.getNamePattern().equals(canonicalMethodPath)) {
                    methodToSingleResourceNameMap.put(methodConfig, nameConfig);
                }
            }
        }
    }
    String manualDoc = Strings.nullToEmpty(interfaceConfigProto.getLangDoc().get(language)).trim();
    String interfaceName = interfaceNameOverride != null ? interfaceNameOverride : DiscoGapicParser.getInterfaceName(interfaceConfigProto.getName()).toUpperCamel();
    if (model.getDiagCollector().hasErrors()) {
        return null;
    } else {
        return new AutoValue_DiscoGapicInterfaceConfig(methodConfigs, retryCodesDefinition, retrySettingsDefinition, requiredConstructorParams, manualDoc, interfaceNameOverride, new DiscoInterfaceModel(interfaceName, model), smokeTestConfig, methodToSingleResourceNameMap.build(), methodConfigMap, singleResourceNames);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap) RetrySettings(com.google.api.gax.retrying.RetrySettings) ImmutableSet(com.google.common.collect.ImmutableSet) CollectionConfigProto(com.google.api.codegen.CollectionConfigProto)

Aggregations

Method (com.google.api.codegen.discovery.Method)8 ImmutableMap (com.google.common.collect.ImmutableMap)5 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)3 HashMap (java.util.HashMap)3 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)2 Schema (com.google.api.codegen.discovery.Schema)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)1 MethodConfigProto (com.google.api.codegen.MethodConfigProto)1 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)1 DiscoGapicInterfaceConfig (com.google.api.codegen.config.DiscoGapicInterfaceConfig)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1 FieldModel (com.google.api.codegen.config.FieldModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 ResourceNameGenerationView (com.google.api.codegen.configgen.viewmodel.ResourceNameGenerationView)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 DiscoGapicNamer (com.google.api.codegen.discogapic.transformer.DiscoGapicNamer)1 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1