Search in sources :

Example 16 with RegionTag

use of com.google.api.generator.gapic.model.RegionTag in project gapic-generator-java by googleapis.

the class ServiceClientCallableMethodSampleComposer method composePagedCallableMethod.

// Compose sample code for the method where it is CallableMethodKind.PAGED.
public static Sample composePagedCallableMethod(Method method, TypeNode clientType, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
    // Assign method's request variable with the default value.
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
    Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
    Preconditions.checkNotNull(requestMessage, String.format("Could not find the message type %s.", method.inputType().reference().fullName()));
    Expr requestBuilderExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
    AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(requestBuilderExpr).build();
    List<Expr> bodyExprs = new ArrayList<>();
    bodyExprs.add(requestAssignmentExpr);
    // Find the repeated field.
    Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
    Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapPaginatedRepeatedField();
    Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated field found on message %s for method %s", methodOutputMessage.name(), method.name()));
    TypeNode repeatedResponseType = repeatedPagedResultsField.type();
    // Create ApiFuture Variable Expression with assign value by invoking client paged callable
    // method.
    // e.g. ApiFuture<ListExclusionsPagedResponse> future =
    // configServiceV2Client.listExclusionsPagedCallable().futureCall(request);
    TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(repeatedResponseType.reference()).build());
    VariableExpr apiFutureVarExpr = VariableExpr.withVariable(Variable.builder().setName("future").setType(apiFutureType).build());
    MethodInvocationExpr pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(String.format("%sPagedCallable", JavaStyle.toLowerCamelCase(method.name()))).build();
    pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedCallableFutureMethodExpr).setMethodName("futureCall").setArguments(requestVarExpr).setReturnType(apiFutureType).build();
    AssignmentExpr apiFutureAssignmentExpr = AssignmentExpr.builder().setVariableExpr(apiFutureVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(pagedCallableFutureMethodExpr).build();
    bodyExprs.add(apiFutureAssignmentExpr);
    List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    bodyExprs.clear();
    // Add line comment
    bodyStatements.add(CommentStatement.withComment(LineComment.withComment("Do something.")));
    // For-loop on repeated response element
    // e.g. for (ListExclusionsResponse element : future.get().iterateAll()) {
    // // doThingsWith(element);
    // }
    VariableExpr repeatedResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("element").setType(repeatedResponseType).build());
    MethodInvocationExpr futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(apiFutureVarExpr).setMethodName("get").build();
    futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(futureGetIterateAllMethodExpr).setMethodName("iterateAll").setReturnType(repeatedResponseType).build();
    CommentStatement lineCommentStatement = CommentStatement.withComment(LineComment.withComment("doThingsWith(element);"));
    ForStatement repeatedResponseForStatement = ForStatement.builder().setLocalVariableExpr(repeatedResponseVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(futureGetIterateAllMethodExpr).setBody(Arrays.asList(lineCommentStatement)).build();
    bodyStatements.add(repeatedResponseForStatement);
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    RegionTag regionTag = RegionTag.builder().setServiceName(clientType.reference().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) OperationFuture(com.google.api.gax.longrunning.OperationFuture) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) Operation(com.google.longrunning.Operation) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Strings(com.google.common.base.Strings) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) Method(com.google.api.generator.gapic.model.Method) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ServerStream(com.google.api.gax.rpc.ServerStream) LineComment(com.google.api.generator.engine.ast.LineComment) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ApiFuture(com.google.api.core.ApiFuture) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) BidiStream(com.google.api.gax.rpc.BidiStream) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) Message(com.google.api.generator.gapic.model.Message) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Message(com.google.api.generator.gapic.model.Message) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Field(com.google.api.generator.gapic.model.Field) ApiFuture(com.google.api.core.ApiFuture) Expr(com.google.api.generator.engine.ast.Expr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ForStatement(com.google.api.generator.engine.ast.ForStatement)

Example 17 with RegionTag

use of com.google.api.generator.gapic.model.RegionTag in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposer method composeShowcaseMethodSample.

public static Sample composeShowcaseMethodSample(Method method, TypeNode clientType, List<MethodArgument> arguments, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
    // Assign method's arguments variable with the default values.
    List<VariableExpr> rpcMethodArgVarExprs = createArgumentVariableExprs(arguments);
    List<Expr> rpcMethodArgDefaultValueExprs = createArgumentDefaultValueExprs(arguments, resourceNames);
    List<Expr> rpcMethodArgAssignmentExprs = createAssignmentsForVarExprsWithValueExprs(rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs);
    List<Expr> bodyExprs = new ArrayList<>();
    bodyExprs.addAll(rpcMethodArgAssignmentExprs);
    List<Statement> bodyStatements = new ArrayList<>();
    RegionTag regionTag;
    if (method.isPaged()) {
        Sample unaryPagedRpc = ServiceClientMethodSampleComposer.composePagedSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes);
        bodyStatements.addAll(unaryPagedRpc.body());
        regionTag = unaryPagedRpc.regionTag();
    } else if (method.hasLro()) {
        Sample unaryLroRpc = ServiceClientMethodSampleComposer.composeLroSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryLroRpc.body());
        regionTag = unaryLroRpc.regionTag();
    } else {
        // e.g. echoClient.echo(), echoClient.echo(...)
        Sample unaryRpc = ServiceClientMethodSampleComposer.composeSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryRpc.body());
        regionTag = unaryRpc.regionTag();
    }
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}
Also used : Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 18 with RegionTag

use of com.google.api.generator.gapic.model.RegionTag in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposer method composeSetCredentialsSample.

public static Sample composeSetCredentialsSample(TypeNode clientType, TypeNode settingsType) {
    // Initialize clientSettings with builder() method.
    // e.g. EchoSettings echoSettings =
    // EchoSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create("myCredentials")).build();
    String settingsName = JavaStyle.toLowerCamelCase(settingsType.reference().name());
    String clientName = JavaStyle.toLowerCamelCase(clientType.reference().name());
    TypeNode myCredentialsType = TypeNode.withReference(VaporReference.builder().setName("myCredentials").setPakkage(clientType.reference().pakkage()).build());
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(settingsName).setType(settingsType).build());
    MethodInvocationExpr newBuilderMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
    TypeNode fixedCredentialProvideType = TypeNode.withReference(ConcreteReference.withClazz(FixedCredentialsProvider.class));
    MethodInvocationExpr credentialArgExpr = MethodInvocationExpr.builder().setStaticReferenceType(fixedCredentialProvideType).setArguments(VariableExpr.withVariable(Variable.builder().setName("myCredentials").setType(myCredentialsType).build())).setMethodName("create").build();
    MethodInvocationExpr credentialsMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderMethodExpr).setArguments(credentialArgExpr).setMethodName("setCredentialsProvider").build();
    MethodInvocationExpr buildMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(credentialsMethodExpr).setReturnType(settingsType).setMethodName("build").build();
    Expr initSettingsVarExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(buildMethodExpr).build();
    // Initialized client with create() method.
    // e.g. EchoClient echoClient = EchoClient.create(echoSettings);
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(clientName).setType(clientType).build());
    MethodInvocationExpr createMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(clientType).setArguments(settingsVarExpr).setMethodName("create").setReturnType(clientType).build();
    String rpcName = createMethodExpr.methodIdentifier().name();
    Expr initClientVarExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createMethodExpr).build();
    List<Statement> sampleBody = Arrays.asList(ExprStatement.withExpr(initSettingsVarExpr), ExprStatement.withExpr(initClientVarExpr));
    RegionTag regionTag = RegionTag.builder().setServiceName(clientName).setRpcName(rpcName).setOverloadDisambiguation("setCredentialsProvider").build();
    return Sample.builder().setBody(sampleBody).setRegionTag(regionTag).build();
}
Also used : FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 19 with RegionTag

use of com.google.api.generator.gapic.model.RegionTag in project gapic-generator-java by googleapis.

the class SettingsSampleComposer method composeSettingsSample.

public static Optional<Sample> composeSettingsSample(Optional<String> methodNameOpt, String settingsClassName, TypeNode classType) {
    if (!methodNameOpt.isPresent()) {
        return Optional.empty();
    }
    // Initialize services settingsBuilder with newBuilder()
    // e.g. FoobarSettings.Builder foobarSettingsBuilder = FoobarSettings.newBuilder();
    TypeNode builderType = TypeNode.withReference(VaporReference.builder().setEnclosingClassNames(classType.reference().name()).setName("Builder").setPakkage(classType.reference().pakkage()).build());
    VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(String.format("%sBuilder", settingsClassName))).setType(builderType).build());
    MethodInvocationExpr settingsBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(classType).setMethodName("newBuilder").setReturnType(builderType).build();
    AssignmentExpr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderMethodInvocationExpr).build();
    // Builder with set value method
    // e.g foobarSettingBuilder.fooSetting().setRetrySettings(
    // echoSettingsBuilder.echoSettings().getRetrySettings().toBuilder().setTotalTimeout(Duration.ofSeconds(30)).build());
    MethodInvocationExpr settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sSettings", methodNameOpt.get()))).build();
    String disambiguation = "Settings";
    MethodInvocationExpr retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("getRetrySettings").build();
    retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("toBuilder").build();
    MethodInvocationExpr ofSecondMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Duration.class))).setMethodName("ofSeconds").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("30").build())).build();
    retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("setTotalTimeout").setArguments(ofSecondMethodInvocationExpr).build();
    retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("build").build();
    settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("setRetrySettings").setArguments(retrySettingsArgExpr).build();
    // Initialize clientSetting with builder() method.
    // e.g: Foobar<Stub>Settings foobarSettings = foobarSettingsBuilder.build();
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(classType).setName(JavaStyle.toLowerCamelCase(settingsClassName)).build());
    AssignmentExpr settingBuildAssignmentExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName("build").setReturnType(classType).build()).build();
    List<Statement> statements = Arrays.asList(initLocalSettingsExpr, settingBuilderMethodInvocationExpr, settingBuildAssignmentExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    RegionTag regionTag = RegionTag.builder().setServiceName(classType.reference().name()).setRpcName(methodNameOpt.get()).build();
    return Optional.of(Sample.builder().setBody(statements).setRegionTag(regionTag).build());
}
Also used : AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Duration(java.time.Duration) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) Optional(java.util.Optional) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Duration(java.time.Duration) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)19 RegionTag (com.google.api.generator.gapic.model.RegionTag)19 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)18 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)17 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)17 Statement (com.google.api.generator.engine.ast.Statement)17 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)16 Expr (com.google.api.generator.engine.ast.Expr)15 ArrayList (java.util.ArrayList)14 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)12 ForStatement (com.google.api.generator.engine.ast.ForStatement)12 TypeNode (com.google.api.generator.engine.ast.TypeNode)12 Sample (com.google.api.generator.gapic.model.Sample)11 Message (com.google.api.generator.gapic.model.Message)10 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)9 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)8 BreakStatement (com.google.api.generator.engine.ast.BreakStatement)8 IfStatement (com.google.api.generator.engine.ast.IfStatement)8 UnaryOperationExpr (com.google.api.generator.engine.ast.UnaryOperationExpr)8 WhileStatement (com.google.api.generator.engine.ast.WhileStatement)8