use of com.google.api.codegen.config.DiscoveryMethodModel 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.config.DiscoveryMethodModel 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.config.DiscoveryMethodModel in project toolkit by googleapis.
the class DiscoveryMethodTransformer method generatePageStreamingResponse.
/**
* Returns a non-null PageStreamingResponseView iff the method has a response object that contains
* a nextPageToken child property.
*/
@Nullable
@Override
public PageStreamingResponseView generatePageStreamingResponse(MethodModel methodModel) {
DiscoveryMethodModel method = (DiscoveryMethodModel) methodModel;
String resourcesField = null;
boolean hasNextPageToken = false;
for (DiscoveryField field : method.getOutputFields()) {
String fieldName = field.getSimpleName();
if (!fieldName.equals(pagingParameters.getNameForNextPageToken())) {
for (Schema property : field.getDiscoveryField().properties().values()) {
if (property.getIdentifier().equals(pagingParameters.getNameForNextPageToken())) {
hasNextPageToken = true;
resourcesField = Name.anyCamel(fieldName).toUpperCamel();
break;
}
}
}
}
if (resourcesField == null || !hasNextPageToken) {
return null;
}
return PageStreamingResponseView.newBuilder().tokenField(pagingParameters.getNameForNextPageToken()).resourcesField(resourcesField).build();
}
use of com.google.api.codegen.config.DiscoveryMethodModel in project toolkit by googleapis.
the class JavaDiscoGapicResourceNameToViewTransformer method transform.
@Override
public List<ViewModel> transform(DiscoApiModel apiModel, GapicProductConfig productConfig) {
List<ViewModel> surfaceRequests = new ArrayList<>();
String packageName = productConfig.getPackageName();
SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(apiModel, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.newBuilder().enableStringFormatFunctions(true).build());
// Keep track of which name patterns have been generated to avoid duplicate classes.
Set<String> namePatterns = new HashSet<>();
for (String interfaceName : productConfig.getInterfaceConfigMap().keySet()) {
SchemaTransformationContext requestContext = SchemaTransformationContext.create(interfaceName, context.getSchemaTypeTable(), context);
// Maps a canonical resource name pattern to any method that uses that pattern.
Map<String, Method> namePatternsToMethod = new HashMap<>();
for (MethodConfig methodConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getMethodConfigs()) {
Method method = ((DiscoveryMethodModel) methodConfig.getMethodModel()).getDiscoMethod();
namePatternsToMethod.put(DiscoGapicParser.getCanonicalPath(method), method);
}
for (SingleResourceNameConfig nameConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getSingleResourceNameConfigs()) {
String namePattern = nameConfig.getNamePattern();
if (namePatterns.contains(namePattern)) {
continue;
}
Method method = namePatternsToMethod.get(namePattern);
StaticLangApiResourceNameView resourceNameView = generateResourceNameClass(requestContext, method, nameConfig);
surfaceRequests.add(generateResourceNameFile(requestContext, resourceNameView));
namePatterns.add(nameConfig.getNamePattern());
}
}
Collections.sort(surfaceRequests, new Comparator<ViewModel>() {
@Override
public int compare(ViewModel o1, ViewModel o2) {
return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
}
});
return surfaceRequests;
}
Aggregations