Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit c9e181a

Browse files
Add support for Callable factory (#483)
Adds support for the Callable factory feature in this toolkit change: googleapis/gapic-generator#1855
1 parent 1e11861 commit c9e181a

File tree

5 files changed

+409
-7
lines changed

5 files changed

+409
-7
lines changed

gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallableFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@
5555
public class GrpcCallableFactory {
5656
private GrpcCallableFactory() {}
5757

58-
private static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(
58+
/**
59+
* Create a Unary callable object with minimal grpc-specific functionality.
60+
*
61+
* @param grpcCallSettings the gRPC call settings
62+
* @param callSettings the Unary call settings
63+
* @param clientContext {@link ClientContext} to use to connect to the service.
64+
*/
65+
public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(
5966
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
6067
UnaryCallSettings<?, ?> callSettings,
6168
ClientContext clientContext) {
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
package com.google.api.gax.grpc;
31+
32+
import com.google.api.core.BetaApi;
33+
import com.google.api.gax.rpc.BatchingCallSettings;
34+
import com.google.api.gax.rpc.BidiStreamingCallable;
35+
import com.google.api.gax.rpc.ClientContext;
36+
import com.google.api.gax.rpc.ClientStreamingCallable;
37+
import com.google.api.gax.rpc.OperationCallSettings;
38+
import com.google.api.gax.rpc.OperationCallable;
39+
import com.google.api.gax.rpc.PagedCallSettings;
40+
import com.google.api.gax.rpc.ServerStreamingCallSettings;
41+
import com.google.api.gax.rpc.ServerStreamingCallable;
42+
import com.google.api.gax.rpc.StreamingCallSettings;
43+
import com.google.api.gax.rpc.UnaryCallSettings;
44+
import com.google.api.gax.rpc.UnaryCallable;
45+
import com.google.longrunning.Operation;
46+
import com.google.longrunning.stub.OperationsStub;
47+
48+
@BetaApi("The surface for use by generated code is not stable yet and may change in the future.")
49+
public interface GrpcStubCallableFactory {
50+
51+
/**
52+
* Create a callable object with grpc-specific functionality. Designed for use by generated code.
53+
*
54+
* @param grpcCallSettings the gRPC call settings
55+
*/
56+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
57+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
58+
UnaryCallSettings<RequestT, ResponseT> callSettings,
59+
ClientContext clientContext);
60+
61+
/**
62+
* Create a paged callable object that represents a paged API method. Designed for use by
63+
* generated code.
64+
*
65+
* @param grpcCallSettings the gRPC call settings
66+
* @param pagedCallSettings {@link PagedCallSettings} to configure the paged settings with.
67+
* @param clientContext {@link ClientContext} to use to connect to the service.
68+
* @return {@link UnaryCallable} callable object.
69+
*/
70+
public <RequestT, ResponseT, PagedListResponseT>
71+
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
72+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
73+
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> pagedCallSettings,
74+
ClientContext clientContext);
75+
76+
/**
77+
* Create a callable object that represents a batching API method. Designed for use by generated
78+
* code.
79+
*
80+
* @param grpcCallSettings the gRPC call settings
81+
* @param batchingCallSettings {@link BatchingCallSettings} to configure the batching related
82+
* settings with.
83+
* @param clientContext {@link ClientContext} to use to connect to the service.
84+
* @return {@link UnaryCallable} callable object.
85+
*/
86+
@BetaApi("The surface for batching is not stable yet and may change in the future.")
87+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
88+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
89+
BatchingCallSettings<RequestT, ResponseT> batchingCallSettings,
90+
ClientContext clientContext);
91+
92+
/**
93+
* Creates a callable object that represents a long-running operation. Designed for use by
94+
* generated code.
95+
*
96+
* @param grpcCallSettings the gRPC call settings
97+
* @param operationCallSettings {@link OperationCallSettings} to configure the method-level
98+
* settings with.
99+
* @param clientContext {@link ClientContext} to use to connect to the service.
100+
* @param operationsStub {@link OperationsStub} to use to poll for updates on the Operation.
101+
* @return {@link OperationCallable} callable object.
102+
*/
103+
@BetaApi(
104+
"The surface for long-running operations is not stable yet and may change in the future.")
105+
public <RequestT, ResponseT, MetadataT>
106+
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
107+
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
108+
OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings,
109+
ClientContext clientContext,
110+
OperationsStub operationsStub);
111+
112+
/**
113+
* Create a bidirectional streaming callable object with grpc-specific functionality. Designed for
114+
* use by generated code.
115+
*
116+
* @param grpcCallSettings the gRPC call settings
117+
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
118+
* settings with.
119+
* @param clientContext {@link ClientContext} to use to connect to the service.
120+
* @return {@link BidiStreamingCallable} callable object.
121+
*/
122+
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
123+
public <RequestT, ResponseT>
124+
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
125+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
126+
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
127+
ClientContext clientContext);
128+
129+
/**
130+
* Create a server-streaming callable with grpc-specific functionality. Designed for use by
131+
* generated code.
132+
*
133+
* @param grpcCallSettings the gRPC call settings
134+
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
135+
* settings with.
136+
* @param clientContext {@link ClientContext} to use to connect to the service.
137+
*/
138+
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
139+
public <RequestT, ResponseT>
140+
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
141+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
142+
ServerStreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
143+
ClientContext clientContext);
144+
145+
/**
146+
* Create a client-streaming callable object with grpc-specific functionality. Designed for use by
147+
* generated code.
148+
*
149+
* @param grpcCallSettings the gRPC call settings
150+
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
151+
* settings with.
152+
* @param clientContext {@link ClientContext} to use to connect to the service.
153+
* @return {@link ClientStreamingCallable} callable object.
154+
*/
155+
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
156+
public <RequestT, ResponseT>
157+
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
158+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
159+
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
160+
ClientContext clientContext);
161+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
package com.google.longrunning.stub;
31+
32+
import com.google.api.core.BetaApi;
33+
import com.google.api.gax.grpc.GrpcCallSettings;
34+
import com.google.api.gax.grpc.GrpcCallableFactory;
35+
import com.google.api.gax.grpc.GrpcStubCallableFactory;
36+
import com.google.api.gax.rpc.BatchingCallSettings;
37+
import com.google.api.gax.rpc.BidiStreamingCallable;
38+
import com.google.api.gax.rpc.ClientContext;
39+
import com.google.api.gax.rpc.ClientStreamingCallable;
40+
import com.google.api.gax.rpc.OperationCallSettings;
41+
import com.google.api.gax.rpc.OperationCallable;
42+
import com.google.api.gax.rpc.PagedCallSettings;
43+
import com.google.api.gax.rpc.ServerStreamingCallSettings;
44+
import com.google.api.gax.rpc.ServerStreamingCallable;
45+
import com.google.api.gax.rpc.StreamingCallSettings;
46+
import com.google.api.gax.rpc.UnaryCallSettings;
47+
import com.google.api.gax.rpc.UnaryCallable;
48+
import com.google.longrunning.Operation;
49+
import javax.annotation.Generated;
50+
51+
// AUTO-GENERATED DOCUMENTATION AND CLASS
52+
/**
53+
* gRPC callable factory implementation for Google Long Running Operations API.
54+
*
55+
* <p>This class is for advanced usage.
56+
*/
57+
@Generated("by GAPIC v0.0.5")
58+
@BetaApi("The surface for use by generated code is not stable yet and may change in the future.")
59+
public class GrpcOperationsCallableFactory implements GrpcStubCallableFactory {
60+
@Override
61+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
62+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
63+
UnaryCallSettings<RequestT, ResponseT> callSettings,
64+
ClientContext clientContext) {
65+
return GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext);
66+
}
67+
68+
@Override
69+
public <RequestT, ResponseT, PagedListResponseT>
70+
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
71+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
72+
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> pagedCallSettings,
73+
ClientContext clientContext) {
74+
return GrpcCallableFactory.createPagedCallable(
75+
grpcCallSettings, pagedCallSettings, clientContext);
76+
}
77+
78+
@Override
79+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
80+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
81+
BatchingCallSettings<RequestT, ResponseT> batchingCallSettings,
82+
ClientContext clientContext) {
83+
return GrpcCallableFactory.createBatchingCallable(
84+
grpcCallSettings, batchingCallSettings, clientContext);
85+
}
86+
87+
@Override
88+
public <RequestT, ResponseT, MetadataT>
89+
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
90+
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
91+
OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings,
92+
ClientContext clientContext,
93+
OperationsStub operationsStub) {
94+
return GrpcCallableFactory.createOperationCallable(
95+
grpcCallSettings, operationCallSettings, clientContext, operationsStub);
96+
}
97+
98+
@Override
99+
public <RequestT, ResponseT>
100+
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
101+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
102+
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
103+
ClientContext clientContext) {
104+
return GrpcCallableFactory.createBidiStreamingCallable(
105+
grpcCallSettings, streamingCallSettings, clientContext);
106+
}
107+
108+
@Override
109+
public <RequestT, ResponseT>
110+
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
111+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
112+
ServerStreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
113+
ClientContext clientContext) {
114+
return GrpcCallableFactory.createServerStreamingCallable(
115+
grpcCallSettings, streamingCallSettings, clientContext);
116+
}
117+
118+
@Override
119+
public <RequestT, ResponseT>
120+
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
121+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
122+
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
123+
ClientContext clientContext) {
124+
return GrpcCallableFactory.createClientStreamingCallable(
125+
grpcCallSettings, streamingCallSettings, clientContext);
126+
}
127+
}

gax-grpc/src/main/java/com/google/longrunning/stub/GrpcOperationsStub.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.google.api.gax.core.BackgroundResource;
3636
import com.google.api.gax.core.BackgroundResourceAggregation;
3737
import com.google.api.gax.grpc.GrpcCallSettings;
38-
import com.google.api.gax.grpc.GrpcCallableFactory;
38+
import com.google.api.gax.grpc.GrpcStubCallableFactory;
3939
import com.google.api.gax.rpc.ClientContext;
4040
import com.google.api.gax.rpc.UnaryCallable;
4141
import com.google.longrunning.CancelOperationRequest;
@@ -107,6 +107,8 @@ public class GrpcOperationsStub extends OperationsStub {
107107
private final UnaryCallable<CancelOperationRequest, Empty> cancelOperationCallable;
108108
private final UnaryCallable<DeleteOperationRequest, Empty> deleteOperationCallable;
109109

110+
private final GrpcStubCallableFactory callableFactory;
111+
110112
public static final GrpcOperationsStub create(OperationsStubSettings settings)
111113
throws IOException {
112114
return new GrpcOperationsStub(settings, ClientContext.create(settings));
@@ -116,13 +118,33 @@ public static final GrpcOperationsStub create(ClientContext clientContext) throw
116118
return new GrpcOperationsStub(OperationsStubSettings.newBuilder().build(), clientContext);
117119
}
118120

121+
public static final GrpcOperationsStub create(
122+
ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
123+
return new GrpcOperationsStub(
124+
OperationsStubSettings.newBuilder().build(), clientContext, callableFactory);
125+
}
126+
119127
/**
120128
* Constructs an instance of GrpcOperationsStub, using the given settings. This is protected so
121129
* that it is easy to make a subclass, but otherwise, the static factory methods should be
122130
* preferred.
123131
*/
124132
protected GrpcOperationsStub(OperationsStubSettings settings, ClientContext clientContext)
125133
throws IOException {
134+
this(settings, clientContext, new GrpcOperationsCallableFactory());
135+
}
136+
137+
/**
138+
* Constructs an instance of GrpcOperationsStub, using the given settings. This is protected so
139+
* that it is easy to make a subclass, but otherwise, the static factory methods should be
140+
* preferred.
141+
*/
142+
protected GrpcOperationsStub(
143+
OperationsStubSettings settings,
144+
ClientContext clientContext,
145+
GrpcStubCallableFactory callableFactory)
146+
throws IOException {
147+
this.callableFactory = callableFactory;
126148

127149
GrpcCallSettings<GetOperationRequest, Operation> getOperationTransportSettings =
128150
GrpcCallSettings.<GetOperationRequest, Operation>newBuilder()
@@ -143,19 +165,19 @@ protected GrpcOperationsStub(OperationsStubSettings settings, ClientContext clie
143165
.build();
144166

145167
this.getOperationCallable =
146-
GrpcCallableFactory.createUnaryCallable(
168+
callableFactory.createUnaryCallable(
147169
getOperationTransportSettings, settings.getOperationSettings(), clientContext);
148170
this.listOperationsCallable =
149-
GrpcCallableFactory.createUnaryCallable(
171+
callableFactory.createUnaryCallable(
150172
listOperationsTransportSettings, settings.listOperationsSettings(), clientContext);
151173
this.listOperationsPagedCallable =
152-
GrpcCallableFactory.createPagedCallable(
174+
callableFactory.createPagedCallable(
153175
listOperationsTransportSettings, settings.listOperationsSettings(), clientContext);
154176
this.cancelOperationCallable =
155-
GrpcCallableFactory.createUnaryCallable(
177+
callableFactory.createUnaryCallable(
156178
cancelOperationTransportSettings, settings.cancelOperationSettings(), clientContext);
157179
this.deleteOperationCallable =
158-
GrpcCallableFactory.createUnaryCallable(
180+
callableFactory.createUnaryCallable(
159181
deleteOperationTransportSettings, settings.deleteOperationSettings(), clientContext);
160182

161183
backgroundResources = new BackgroundResourceAggregation(clientContext.getBackgroundResources());

0 commit comments

Comments
 (0)