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();
}
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;
}
}
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();
}
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();
}
}
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);
}
}
Aggregations