Skip to content

Commit cb1b534

Browse files
committed
feat: Add REST AIP-151 LRO suport
Depends on googleapis/gax-java#1484
1 parent 8ae8e6f commit cb1b534

10 files changed

Lines changed: 386 additions & 181 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/common/AbstractTransportServiceStubClassComposer.java

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.api.core.BetaApi;
1818
import com.google.api.gax.core.BackgroundResource;
1919
import com.google.api.gax.core.BackgroundResourceAggregation;
20+
import com.google.api.gax.longrunning.OperationSnapshot;
2021
import com.google.api.gax.rpc.BidiStreamingCallable;
2122
import com.google.api.gax.rpc.ClientContext;
2223
import com.google.api.gax.rpc.ClientStreamingCallable;
@@ -119,6 +120,7 @@ private static TypeStore createStaticTypes() {
119120
IOException.class,
120121
Operation.class,
121122
OperationCallable.class,
123+
OperationSnapshot.class,
122124
RequestParamsExtractor.class,
123125
ServerStreamingCallable.class,
124126
TimeUnit.class,
@@ -161,7 +163,7 @@ public GapicClass generate(GapicContext context, Service service) {
161163
}
162164

163165
boolean operationPollingMethod = checkOperationPollingMethod(service);
164-
if(operationPollingMethod) {
166+
if (operationPollingMethod) {
165167
VariableExpr longRunningVarExpr = declareLongRunningClient();
166168
if (longRunningVarExpr != null) {
167169
classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
@@ -212,7 +214,10 @@ public GapicClass generate(GapicContext context, Service service) {
212214
}
213215

214216
protected abstract Statement createMethodDescriptorVariableDecl(
215-
Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr, Map<String, Message> messageTypes);
217+
Service service,
218+
Method protoMethod,
219+
VariableExpr methodDescriptorVarExpr,
220+
Map<String, Message> messageTypes);
216221

217222
protected boolean generateOperationsStubLogic(Service service) {
218223
return true;
@@ -227,7 +232,8 @@ protected List<MethodDefinition> createOperationsStubGetterMethod(
227232
String methodName =
228233
String.format(
229234
"get%s",
230-
JavaStyle.toUpperCamelCase(getTransportContext().transportOperationsStubNames().get(0)));
235+
JavaStyle.toUpperCamelCase(
236+
getTransportContext().transportOperationsStubNames().get(0)));
231237

232238
return Arrays.asList(
233239
MethodDefinition.builder()
@@ -248,15 +254,26 @@ protected List<MethodDefinition> createGetMethodDescriptorsMethod(
248254
return Arrays.asList();
249255
}
250256

257+
protected List<Statement> createTypeRegistry(Service service) {
258+
return Arrays.asList();
259+
}
260+
251261
protected List<Statement> createClassStatements(
252262
Service service,
253263
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs,
254264
Map<String, VariableExpr> callableClassMemberVarExprs,
255265
Map<String, VariableExpr> classMemberVarExprs,
256266
Map<String, Message> messageTypes) {
257267
List<Statement> classStatements = new ArrayList<>();
268+
269+
classStatements.addAll(createTypeRegistry(service));
270+
if (!classStatements.isEmpty()) {
271+
classStatements.add(EMPTY_LINE_STATEMENT);
272+
}
273+
258274
for (Statement statement :
259-
createMethodDescriptorVariableDecls(service, protoMethodNameToDescriptorVarExprs, messageTypes)) {
275+
createMethodDescriptorVariableDecls(
276+
service, protoMethodNameToDescriptorVarExprs, messageTypes)) {
260277
classStatements.add(statement);
261278
classStatements.add(EMPTY_LINE_STATEMENT);
262279
}
@@ -265,11 +282,15 @@ protected List<Statement> createClassStatements(
265282
classStatements.add(EMPTY_LINE_STATEMENT);
266283

267284
classStatements.addAll(createClassMemberFieldDeclarations(classMemberVarExprs));
285+
classStatements.add(EMPTY_LINE_STATEMENT);
286+
268287
return classStatements;
269288
}
270289

271290
protected List<Statement> createMethodDescriptorVariableDecls(
272-
Service service, Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs, Map<String, Message> messageTypes) {
291+
Service service,
292+
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs,
293+
Map<String, Message> messageTypes) {
273294
return service.methods().stream()
274295
.map(
275296
m ->
@@ -418,14 +439,17 @@ protected List<MethodDefinition> createClassMethods(
418439
createGetMethodDescriptorsMethod(service, typeStore, protoMethodNameToDescriptorVarExprs));
419440
javaMethods.addAll(
420441
createOperationsStubGetterMethod(
421-
service, classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0))));
442+
service,
443+
classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0))));
422444
javaMethods.addAll(createCallableGetterMethods(callableClassMemberVarExprs));
423445
javaMethods.addAll(
424-
createStubOverrideMethods(classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME), service));
446+
createStubOverrideMethods(
447+
classMemberVarExprs.get(BACKGROUND_RESOURCES_MEMBER_NAME), service));
425448
return javaMethods;
426449
}
427450

428-
protected List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore, String newBuilderMethod) {
451+
protected List<MethodDefinition> createStaticCreatorMethods(
452+
Service service, TypeStore typeStore, String newBuilderMethod) {
429453
TypeNode creatorMethodReturnType =
430454
typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
431455
Function<List<VariableExpr>, MethodDefinition.Builder> creatorMethodStarterFn =
@@ -584,22 +608,16 @@ protected List<MethodDefinition> createConstructorMethods(
584608
.build())
585609
.setValueExpr(callableFactoryVarExpr)
586610
.build());
587-
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0));
588-
//TODO: refactor this
611+
VariableExpr operationsStubClassVarExpr =
612+
classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0));
613+
// TODO: refactor this
589614
if (generateOperationsStubLogic(service)) {
590-
TypeNode opeationsStubType = getTransportOperationsStubType(service);
591-
secondCtorExprs.add(
592-
AssignmentExpr.builder()
593-
.setVariableExpr(
594-
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
595-
.setValueExpr(
596-
MethodInvocationExpr.builder()
597-
.setStaticReferenceType(opeationsStubType)
598-
.setMethodName("create")
599-
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
600-
.setReturnType(operationsStubClassVarExpr.type())
601-
.build())
602-
.build());
615+
secondCtorExprs.addAll(createOperationsStubInitExpr(
616+
service,
617+
thisExpr,
618+
operationsStubClassVarExpr,
619+
clientContextVarExpr,
620+
callableFactoryVarExpr));
603621
}
604622
secondCtorStatements.addAll(
605623
secondCtorExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
@@ -665,7 +683,6 @@ protected List<MethodDefinition> createConstructorMethods(
665683
secondCtorExprs.clear();
666684
secondCtorStatements.add(EMPTY_LINE_STATEMENT);
667685

668-
669686
secondCtorStatements.addAll(createLongRunningClient(service, typeStore));
670687

671688
// Instantiate backgroundResources.
@@ -699,6 +716,27 @@ protected List<MethodDefinition> createConstructorMethods(
699716
return Arrays.asList(firstCtor, secondCtor);
700717
}
701718

719+
protected List<Expr> createOperationsStubInitExpr(
720+
Service service,
721+
Expr thisExpr,
722+
VariableExpr operationsStubClassVarExpr,
723+
VariableExpr clientContextVarExpr,
724+
VariableExpr callableFactoryVarExpr) {
725+
TypeNode opeationsStubType = getTransportOperationsStubType(service);
726+
return Collections.singletonList(
727+
AssignmentExpr.builder()
728+
.setVariableExpr(
729+
operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
730+
.setValueExpr(
731+
MethodInvocationExpr.builder()
732+
.setStaticReferenceType(opeationsStubType)
733+
.setMethodName("create")
734+
.setArguments(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr))
735+
.setReturnType(operationsStubClassVarExpr.type())
736+
.build())
737+
.build());
738+
}
739+
702740
protected List<Statement> createLongRunningClient(Service service, TypeStore typeStore) {
703741
return ImmutableList.of();
704742
}
@@ -963,8 +1001,8 @@ private List<MethodDefinition> createStubOverrideMethods(
9631001
}
9641002

9651003
private boolean checkOperationPollingMethod(Service service) {
966-
for(Method method : service.methods()) {
967-
if(method.isOperationPollingMethod()) {
1004+
for (Method method : service.methods()) {
1005+
if (method.isOperationPollingMethod()) {
9681006
return true;
9691007
}
9701008
}
@@ -1063,15 +1101,15 @@ protected TypeNode getTransportOperationsStubType(Service service) {
10631101
TypeNode transportOpeationsStubType = service.operationServiceStubType();
10641102
if (transportOpeationsStubType == null) {
10651103
transportOpeationsStubType = getTransportContext().transportOperationsStubTypes().get(0);
1066-
}
1067-
else {
1068-
transportOpeationsStubType = TypeNode.withReference(
1069-
VaporReference.builder()
1070-
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
1071-
.setPakkage(transportOpeationsStubType.reference().pakkage())
1072-
.build());
1104+
} else {
1105+
transportOpeationsStubType =
1106+
TypeNode.withReference(
1107+
VaporReference.builder()
1108+
.setName("HttpJson" + transportOpeationsStubType.reference().simpleName())
1109+
.setPakkage(transportOpeationsStubType.reference().pakkage())
1110+
.build());
10731111
}
10741112

10751113
return transportOpeationsStubType;
10761114
}
1077-
}
1115+
}

src/main/java/com/google/api/generator/gapic/composer/grpcrest/GrpcRestContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.api.generator.gapic.composer.grpcrest;
1616

17-
import com.google.api.gax.core.BackgroundResource;
1817
import com.google.api.gax.grpc.GrpcTransportChannel;
1918
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2019
import com.google.api.gax.grpc.ProtoOperationTransformers;
@@ -72,7 +71,8 @@ public abstract class GrpcRestContext extends TransportContext {
7271
.setTransportCallableFactoryType(null)
7372
.setOperationsStubTypes(
7473
ImmutableList.of(
75-
classToType(OperationsStub.class), classToType(BackgroundResource.class)))
74+
classToType(OperationsStub.class),
75+
classToType(com.google.api.gax.httpjson.longrunning.stub.OperationsStub.class)))
7676
.setTransportCallSettingsName(null)
7777
// For RetrySettingsComposer
7878
// TODO: fix when LRO for REST RE FIXED

src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceCallableFactoryClassComposer.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727
import com.google.api.generator.engine.ast.NewObjectExpr;
2828
import com.google.api.generator.engine.ast.Statement;
2929
import com.google.api.generator.engine.ast.TypeNode;
30-
import com.google.api.generator.engine.ast.ValueExpr;
3130
import com.google.api.generator.engine.ast.VaporReference;
3231
import com.google.api.generator.engine.ast.Variable;
3332
import com.google.api.generator.engine.ast.VariableExpr;
3433
import com.google.api.generator.gapic.composer.common.AbstractServiceCallableFactoryClassComposer;
3534
import com.google.api.generator.gapic.composer.store.TypeStore;
3635
import com.google.api.generator.gapic.model.Service;
37-
import com.google.common.collect.ImmutableList;
36+
import com.google.longrunning.Operation;
3837
import java.util.ArrayList;
3938
import java.util.Arrays;
4039
import java.util.List;
@@ -46,7 +45,7 @@ public class HttpJsonServiceCallableFactoryClassComposer
4645
new HttpJsonServiceCallableFactoryClassComposer();
4746

4847
private static final TypeNode DEFAULT_OPERATION_TYPE =
49-
TypeNode.withReference(ConcreteReference.withClazz(Object.class));
48+
TypeNode.withReference(ConcreteReference.withClazz(Operation.class));
5049

5150
private HttpJsonServiceCallableFactoryClassComposer() {
5251
super(RestContext.instance());
@@ -129,15 +128,6 @@ protected MethodDefinition createOperationCallableMethod(Service service, TypeSt
129128
Arrays.asList(betaAnnotation));
130129

131130
List<Statement> createOperationCallableBody = new ArrayList<>();
132-
if (service.operationServiceStubType() == null) {
133-
// It is an Operation polling service, it cannot contain LRO methods
134-
return method
135-
.toBuilder()
136-
.setBody(ImmutableList.of())
137-
.setReturnExpr(ValueExpr.createNullExpr())
138-
.build();
139-
}
140-
141131
List<VariableExpr> arguments = new ArrayList<>(method.arguments());
142132

143133
Variable httpJsonCallSettingsVar = arguments.get(0).variable();

0 commit comments

Comments
 (0)