Skip to content

Commit

Permalink
feat: add server streaming support for REST transport (#902)
Browse files Browse the repository at this point in the history
This PR depends on googleapis/gax-java#1599
  • Loading branch information
vam-google committed Jan 22, 2022
1 parent 128039b commit 3b2dec6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 23 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jvm_maven_import_external(
# which in its turn, prioritizes actual generated clients runtime dependencies
# over the generator dependencies.

_gax_java_version = "2.7.1"
_gax_java_version = "2.10.0"

http_archive(
name = "com_google_api_gax_java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ protected MethodDefinition createBatchingCallableMethod(Service service, TypeSto
protected abstract MethodDefinition createOperationCallableMethod(
Service service, TypeStore typeStore);

protected MethodDefinition createServerStreamingCallableMethod(
Service service, TypeStore typeStore) {
String methodVariantName = "ServerStreaming";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
return createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ methodVariantName,
/*returnCallableTemplateNames=*/ methodTemplateNames,
/*methodVariantName=*/ methodVariantName,
/*grpcCallSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()));
}

protected MethodDefinition createGenericCallableMethod(
Service service,
TypeStore typeStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,6 @@ private MethodDefinition createBidiStreamingCallableMethod(Service service, Type
.collect(Collectors.toList()));
}

private MethodDefinition createServerStreamingCallableMethod(
Service service, TypeStore typeStore) {
String methodVariantName = "ServerStreaming";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName);
return createGenericCallableMethod(
service,
typeStore,
/*methodTemplateNames=*/ methodTemplateNames,
/*returnCallableKindName=*/ methodVariantName,
/*returnCallableTemplateNames=*/ methodTemplateNames,
/*methodVariantName=*/ methodVariantName,
/*grpcCallSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()),
/*callSettingsVariantName=*/ methodVariantName,
/*callSettingsTemplateObjects=*/ methodTemplateNames.stream()
.map(n -> (Object) n)
.collect(Collectors.toList()));
}

private MethodDefinition createClientStreamingCallableMethod(
Service service, TypeStore typeStore) {
String methodVariantName = "ClientStreaming";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ protected List<TypeNode> createClassImplements(Service service, TypeStore typeSt
Arrays.asList(operationType.reference(), operationsStubType.reference()))));
}

@Override
protected List<MethodDefinition> createClassMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> classMethods =
new ArrayList<>(super.createClassMethods(service, typeStore));
classMethods.addAll(Arrays.asList(createServerStreamingCallableMethod(service, typeStore)));
return classMethods;
}

@Override
protected MethodDefinition createOperationCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Operation";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.api.client.http.HttpMethods;
import com.google.api.core.InternalApi;
import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.ApiMethodDescriptor.MethodType;
import com.google.api.gax.httpjson.FieldsExtractor;
import com.google.api.gax.httpjson.HttpJsonCallSettings;
import com.google.api.gax.httpjson.HttpJsonLongRunningClient;
Expand Down Expand Up @@ -143,6 +144,7 @@ protected Statement createMethodDescriptorVariableDecl(
.apply(expr);

expr = methodMaker.apply("setHttpMethod", getHttpMethodTypeExpr(protoMethod)).apply(expr);
expr = methodMaker.apply("setType", getMethodTypeExpr(protoMethod)).apply(expr);
expr =
methodMaker.apply("setRequestFormatter", getRequestFormatterExpr(protoMethod)).apply(expr);
expr = methodMaker.apply("setResponseParser", setResponseParserExpr(protoMethod)).apply(expr);
Expand Down Expand Up @@ -977,6 +979,36 @@ private List<Expr> getHttpMethodTypeExpr(Method protoMethod) {
return Collections.singletonList(expr);
}

private List<Expr> getMethodTypeExpr(Method protoMethod) {
MethodType methodType;
switch (protoMethod.stream()) {
case NONE:
methodType = MethodType.UNARY;
break;
case SERVER:
methodType = MethodType.SERVER_STREAMING;
break;
case CLIENT:
// Not feasible to suppor in REST
case BIDI:
// Not feasible to suppor in REST
default:
throw new UnsupportedOperationException(
String.format(
"Methods of type %s are not supported by REST transport", protoMethod.stream()));
}
EnumRefExpr expr =
EnumRefExpr.builder()
.setName(methodType.toString())
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(ApiMethodDescriptor.MethodType.class)
.build()))
.build();
return Collections.singletonList(expr);
}

@Override
protected List<Expr> createOperationsStubInitExpr(
Service service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.longrunning.Operation;
Expand Down Expand Up @@ -74,4 +76,14 @@ public class HttpJsonComplianceCallableFactory
return HttpJsonCallableFactory.createOperationCallable(
callSettings, clientContext, operationsStub.longRunningClient(), initialCallable);
}

@Override
public <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
HttpJsonCallSettings<RequestT, ResponseT> httpJsonCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return HttpJsonCallableFactory.createServerStreamingCallable(
httpJsonCallSettings, callSettings, clientContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Compliance/RepeatDataBody")
.setHttpMethod(HttpMethods.POST)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -74,6 +75,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Compliance/RepeatDataBodyInfo")
.setHttpMethod(HttpMethods.POST)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -109,6 +111,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Compliance/RepeatDataQuery")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -144,6 +147,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Compliance/RepeatDataSimplePath")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -191,6 +195,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
ApiMethodDescriptor.<RepeatRequest, RepeatResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Compliance/RepeatDataPathResource")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -237,6 +242,7 @@ public class HttpJsonComplianceStub extends ComplianceStub {
.setFullMethodName(
"google.showcase.v1beta1.Compliance/RepeatDataPathTrailingResource")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<RepeatRequest>newBuilder()
.setPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.cloud.compute.v1.Operation;
Expand Down Expand Up @@ -89,4 +91,14 @@ OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
return HttpJsonCallableFactory.createOperationCallable(
callSettings, clientContext, operationsStub.longRunningClient(), initialCallable);
}

@Override
public <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
HttpJsonCallSettings<RequestT, ResponseT> httpJsonCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return HttpJsonCallableFactory.createServerStreamingCallable(
httpJsonCallSettings, callSettings, clientContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class HttpJsonAddressesStub extends AddressesStub {
ApiMethodDescriptor.<AggregatedListAddressesRequest, AddressAggregatedList>newBuilder()
.setFullMethodName("google.cloud.compute.v1.Addresses/AggregatedList")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<AggregatedListAddressesRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -117,6 +118,7 @@ public class HttpJsonAddressesStub extends AddressesStub {
ApiMethodDescriptor.<DeleteAddressRequest, Operation>newBuilder()
.setFullMethodName("google.cloud.compute.v1.Addresses/Delete")
.setHttpMethod(HttpMethods.DELETE)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<DeleteAddressRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -166,6 +168,7 @@ public class HttpJsonAddressesStub extends AddressesStub {
ApiMethodDescriptor.<InsertAddressRequest, Operation>newBuilder()
.setFullMethodName("google.cloud.compute.v1.Addresses/Insert")
.setHttpMethod(HttpMethods.POST)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<InsertAddressRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -217,6 +220,7 @@ public class HttpJsonAddressesStub extends AddressesStub {
ApiMethodDescriptor.<ListAddressesRequest, AddressList>newBuilder()
.setFullMethodName("google.cloud.compute.v1.Addresses/List")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<ListAddressesRequest>newBuilder()
.setPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.longrunning.Operation;
Expand Down Expand Up @@ -90,4 +92,14 @@ OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
return HttpJsonCallableFactory.createOperationCallable(
callSettings, clientContext, operationsStub.longRunningClient(), initialCallable);
}

@Override
public <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
HttpJsonCallSettings<RequestT, ResponseT> httpJsonCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return HttpJsonCallableFactory.createServerStreamingCallable(
httpJsonCallSettings, callSettings, clientContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class HttpJsonRegionOperationsStub extends RegionOperationsStub {
ApiMethodDescriptor.<GetRegionOperationRequest, Operation>newBuilder()
.setFullMethodName("google.cloud.compute.v1.RegionOperations/Get")
.setHttpMethod(HttpMethods.GET)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<GetRegionOperationRequest>newBuilder()
.setPath(
Expand Down Expand Up @@ -117,6 +118,7 @@ public class HttpJsonRegionOperationsStub extends RegionOperationsStub {
ApiMethodDescriptor.<WaitRegionOperationRequest, Operation>newBuilder()
.setFullMethodName("google.cloud.compute.v1.RegionOperations/Wait")
.setHttpMethod(HttpMethods.POST)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<WaitRegionOperationRequest>newBuilder()
.setPath(
Expand Down

0 comments on commit 3b2dec6

Please sign in to comment.