Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.api.generator.gapic.composer;

import com.google.api.core.ApiFuture;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.api.gax.rpc.BidiStream;
import com.google.api.gax.rpc.ServerStream;
Expand Down Expand Up @@ -46,6 +47,7 @@
import com.google.api.generator.gapic.model.MethodArgument;
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.longrunning.Operation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -64,6 +66,7 @@ public final class SampleCodeHelperComposer {
private static String UNARY_CALLABLE_NAME_PATTERN = "%sCallable";
private static String PAGED_CALLABLE_NAME_PATTERN = "%sPagedCallable";
private static String PAGED_RESPONSE_NAME_PATTERN = "%sPagedResponse";
private static String OPERATION_CALLABLE_NAME_PATTERN = "%sOperationCallable";

public static TryCatchStatement composeRpcMethodSampleCode(
Method method,
Expand Down Expand Up @@ -533,12 +536,117 @@ private static TryCatchStatement composeLroRpcCallableMethodSampleCode(
TypeNode clientType,
TypeNode returnType,
Map<String, ResourceName> resourceNames) {
// Initialize the method's arguments with default values.
List<MethodArgument> arguments =
method.methodSignatures().isEmpty()
? Collections.emptyList()
: method.methodSignatures().get(0);
List<Statement> bodyStatements =
arguments.stream()
.map(
methodArg ->
ExprStatement.withExpr(
assignMethodArgumentWithDefaultValue(methodArg, resourceNames)))
.collect(Collectors.toList());
// Build request Variable Epxr with set attributes.
bodyStatements.add(
ExprStatement.withExpr(createRequestBuilderExpr(method.inputType(), arguments)));

// If return type has 3 generics, add statements for OperationCallable method.
// otherwise, add statements for callable method.
if (returnType.reference().generics().size() == 3) {
// Initialize operation future variable with operation callable method.
// e.g.OperationFuture<WaitResponse, WaitMetadata> future =
// echoClient.waitOperationCallable().futureCall(request);
TypeNode operationFutureType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(OperationFuture.class)
.setGenerics(method.inputType().reference(), method.outputType().reference())
.build());
VariableExpr operationFutureVarExpr = createVariableExpr(FUTURE, operationFutureType);
MethodInvocationExpr futureCallMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
createVariableExpr(getClientName(clientType), clientType))
.setMethodName(getOperationCallableName(method.name()))
.build())
.setArguments(createVariableExpr(REQUEST, method.inputType()))
.setMethodName("futureCall")
.setReturnType(operationFutureType)
.build();
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(operationFutureVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(futureCallMethodExpr)
.build()));
// Create comment line
bodyStatements.add(createLineCommentStatement("Do something."));
// Assign response variable with get method.
// e.g. WaitResponse response = future.get();
TypeNode waitResponseType = TypeNode.withReference(returnType.reference().generics().get(1));
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(createVariableDeclExpr(RESPONSE, waitResponseType))
.setValueExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(operationFutureVarExpr)
.setMethodName("get")
.setReturnType(waitResponseType)
.build())
.build()));
} else {
// Initialize Api future variable with callable method.
// e.g ApiFuture<Operation> future = echoClient.waitCallable().futureCall(request);
TypeNode apiFutureType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(ApiFuture.class)
.setGenerics(ConcreteReference.withClazz(Operation.class))
.build());
VariableExpr apiFutureVarExpr = createVariableExpr("future", apiFutureType);
MethodInvocationExpr futureCallMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
createVariableExpr(getClientName(clientType), clientType))
.setMethodName(getCallableMethodName(method.name()))
.build())
.setMethodName("futureCall")
.setArguments(createVariableExpr(REQUEST, method.inputType()))
.setReturnType(apiFutureType)
.build();
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(apiFutureVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(futureCallMethodExpr)
.build()));
// Create comment line
bodyStatements.add(createLineCommentStatement("Do something."));
// Assign response variable with get method.
// e.g. Operation response = future.get();
bodyStatements.add(
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(createVariableDeclExpr(RESPONSE, method.outputType()))
.setValueExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(apiFutureVarExpr)
.setMethodName("get")
.setReturnType(method.outputType())
.build())
.build()));
}

return TryCatchStatement.builder()
.setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientType))
.setTryBody(
Arrays.asList(
createLineCommentStatement(
"Note: Not implement yet, placeholder for Lro Rpc callable methods' sample code.")))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build();
}
Expand Down Expand Up @@ -899,6 +1007,10 @@ private static String getPagedCallableName(String methodName) {
return JavaStyle.toLowerCamelCase(String.format(PAGED_CALLABLE_NAME_PATTERN, methodName));
}

private static String getOperationCallableName(String methodName) {
return JavaStyle.toLowerCamelCase(String.format(OPERATION_CALLABLE_NAME_PATTERN, methodName));
}

private static CommentStatement createLineCommentStatement(String content) {
return CommentStatement.withComment(LineComment.withComment(content));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,12 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not implement yet, placeholder for Lro Rpc callable methods' sample code.
* Duration ttl = Duration.newBuilder().build();
* WaitRequest request = WaitRequest.newBuilder().setTtl(ttl).build();
* OperationFuture<WaitRequest, Operation> future =
* echoClient.waitOperationCallable().futureCall(request);
* // Do something.
* WaitResponse response = future.get();
* }
* }</pre>
*/
Expand All @@ -556,7 +561,11 @@ public class EchoClient implements BackgroundResource {
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* // Note: Not implement yet, placeholder for Lro Rpc callable methods' sample code.
* Duration ttl = Duration.newBuilder().build();
* WaitRequest request = WaitRequest.newBuilder().setTtl(ttl).build();
* ApiFuture<Operation> future = echoClient.waitCallable().futureCall(request);
* // Do something.
* Operation response = future.get();
* }
* }</pre>
*/
Expand Down
11 changes: 9 additions & 2 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ public final OperationFuture<ExportAssetsResponse, ExportAssetsRequest> exportAs
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not implement yet, placeholder for Lro Rpc callable methods' sample code.
* ExportAssetsRequest request = ExportAssetsRequest.newBuilder().build();
* OperationFuture<ExportAssetsRequest, Operation> future =
* assetServiceClient.exportAssetsOperationCallable().futureCall(request);
* // Do something.
* ExportAssetsResponse response = future.get();
* }
* }</pre>
*/
Expand All @@ -222,7 +226,10 @@ public final OperationFuture<ExportAssetsResponse, ExportAssetsRequest> exportAs
*
* <pre>{@code
* try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
* // Note: Not implement yet, placeholder for Lro Rpc callable methods' sample code.
* ExportAssetsRequest request = ExportAssetsRequest.newBuilder().build();
* ApiFuture<Operation> future = assetServiceClient.exportAssetsCallable().futureCall(request);
* // Do something.
* Operation response = future.get();
* }
* }</pre>
*/
Expand Down
Loading