1616package com .google .cloud .speech .spi .v1beta1 ;
1717
1818import com .google .api .gax .grpc .ChannelAndExecutor ;
19+ import com .google .api .gax .grpc .FixedChannelProvider ;
20+ import com .google .api .gax .grpc .FixedExecutorProvider ;
21+ import com .google .api .gax .grpc .OperationCallable ;
22+ import com .google .api .gax .grpc .OperationFuture ;
1923import com .google .api .gax .grpc .StreamingCallable ;
2024import com .google .api .gax .grpc .UnaryCallable ;
2125import com .google .cloud .speech .v1beta1 .AsyncRecognizeRequest ;
26+ import com .google .cloud .speech .v1beta1 .AsyncRecognizeResponse ;
2227import com .google .cloud .speech .v1beta1 .RecognitionAudio ;
2328import com .google .cloud .speech .v1beta1 .RecognitionConfig ;
2429import com .google .cloud .speech .v1beta1 .StreamingRecognizeRequest ;
2530import com .google .cloud .speech .v1beta1 .StreamingRecognizeResponse ;
2631import com .google .cloud .speech .v1beta1 .SyncRecognizeRequest ;
2732import com .google .cloud .speech .v1beta1 .SyncRecognizeResponse ;
2833import com .google .longrunning .Operation ;
34+ import com .google .longrunning .OperationsClient ;
35+ import com .google .longrunning .OperationsSettings ;
2936import com .google .protobuf .ExperimentalApi ;
3037import io .grpc .ManagedChannel ;
3138import java .io .Closeable ;
@@ -97,10 +104,13 @@ public class SpeechClient implements AutoCloseable {
97104 private final SpeechSettings settings ;
98105 private final ScheduledExecutorService executor ;
99106 private final ManagedChannel channel ;
107+ private final OperationsClient operationsClient ;
100108 private final List <AutoCloseable > closeables = new ArrayList <>();
101109
102110 private final UnaryCallable <SyncRecognizeRequest , SyncRecognizeResponse > syncRecognizeCallable ;
103111 private final UnaryCallable <AsyncRecognizeRequest , Operation > asyncRecognizeCallable ;
112+ private final OperationCallable <AsyncRecognizeRequest , AsyncRecognizeResponse >
113+ asyncRecognizeOperationCallable ;
104114 private final StreamingCallable <StreamingRecognizeRequest , StreamingRecognizeResponse >
105115 streamingRecognizeCallable ;
106116
@@ -127,10 +137,25 @@ protected SpeechClient(SpeechSettings settings) throws IOException {
127137 this .executor = channelAndExecutor .getExecutor ();
128138 this .channel = channelAndExecutor .getChannel ();
129139
140+ FixedExecutorProvider executorProvider = FixedExecutorProvider .create (this .executor );
141+ FixedChannelProvider channelProvider = FixedChannelProvider .create (this .channel );
142+ OperationsSettings operationsSettings =
143+ OperationsSettings .defaultBuilder ()
144+ .setExecutorProvider (executorProvider )
145+ .setChannelProvider (channelProvider )
146+ .build ();
147+ this .operationsClient = OperationsClient .create (operationsSettings );
148+
130149 this .syncRecognizeCallable =
131150 UnaryCallable .create (settings .syncRecognizeSettings (), this .channel , this .executor );
132151 this .asyncRecognizeCallable =
133- UnaryCallable .create (settings .asyncRecognizeSettings (), this .channel , this .executor );
152+ UnaryCallable .create (
153+ settings .asyncRecognizeSettings ().getInitialCallSettings (),
154+ this .channel ,
155+ this .executor );
156+ this .asyncRecognizeOperationCallable =
157+ OperationCallable .create (
158+ settings .asyncRecognizeSettings (), this .channel , this .executor , this .operationsClient );
134159 this .streamingRecognizeCallable =
135160 StreamingCallable .create (settings .streamingRecognizeSettings (), this .channel );
136161
@@ -158,6 +183,10 @@ public final SpeechSettings getSettings() {
158183 return settings ;
159184 }
160185
186+ public final OperationsClient getOperationsClient () {
187+ return operationsClient ;
188+ }
189+
161190 // AUTO-GENERATED DOCUMENTATION AND METHOD
162191 /**
163192 * Perform synchronous speech-recognition: receive results after all audio has been sent and
@@ -249,7 +278,7 @@ public final UnaryCallable<SyncRecognizeRequest, SyncRecognizeResponse> syncReco
249278 * try (SpeechClient speechClient = SpeechClient.create()) {
250279 * RecognitionConfig config = RecognitionConfig.newBuilder().build();
251280 * RecognitionAudio audio = RecognitionAudio.newBuilder().build();
252- * Operation response = speechClient.asyncRecognize (config, audio);
281+ * AsyncRecognizeResponse response = speechClient.asyncRecognizeAsync (config, audio).get( );
253282 * }
254283 * </code></pre>
255284 *
@@ -258,11 +287,12 @@ public final UnaryCallable<SyncRecognizeRequest, SyncRecognizeResponse> syncReco
258287 * @param audio [Required] The audio data to be recognized.
259288 * @throws com.google.api.gax.grpc.ApiException if the remote call fails
260289 */
261- public final Operation asyncRecognize (RecognitionConfig config , RecognitionAudio audio ) {
290+ public final OperationFuture <AsyncRecognizeResponse > asyncRecognizeAsync (
291+ RecognitionConfig config , RecognitionAudio audio ) {
262292
263293 AsyncRecognizeRequest request =
264294 AsyncRecognizeRequest .newBuilder ().setConfig (config ).setAudio (audio ).build ();
265- return asyncRecognize (request );
295+ return asyncRecognizeAsync (request );
266296 }
267297
268298 // AUTO-GENERATED DOCUMENTATION AND METHOD
@@ -281,15 +311,43 @@ public final Operation asyncRecognize(RecognitionConfig config, RecognitionAudio
281311 * .setConfig(config)
282312 * .setAudio(audio)
283313 * .build();
284- * Operation response = speechClient.asyncRecognize (request);
314+ * AsyncRecognizeResponse response = speechClient.asyncRecognizeAsync (request).get( );
285315 * }
286316 * </code></pre>
287317 *
288318 * @param request The request object containing all of the parameters for the API call.
289319 * @throws com.google.api.gax.grpc.ApiException if the remote call fails
290320 */
291- public final Operation asyncRecognize (AsyncRecognizeRequest request ) {
292- return asyncRecognizeCallable ().call (request );
321+ public final OperationFuture <AsyncRecognizeResponse > asyncRecognizeAsync (
322+ AsyncRecognizeRequest request ) {
323+ return asyncRecognizeOperationCallable ().futureCall (request );
324+ }
325+
326+ // AUTO-GENERATED DOCUMENTATION AND METHOD
327+ /**
328+ * Perform asynchronous speech-recognition: receive results via the google.longrunning.Operations
329+ * interface. Returns either an `Operation.error` or an `Operation.response` which contains an
330+ * `AsyncRecognizeResponse` message.
331+ *
332+ * <p>Sample code:
333+ *
334+ * <pre><code>
335+ * try (SpeechClient speechClient = SpeechClient.create()) {
336+ * RecognitionConfig config = RecognitionConfig.newBuilder().build();
337+ * RecognitionAudio audio = RecognitionAudio.newBuilder().build();
338+ * AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
339+ * .setConfig(config)
340+ * .setAudio(audio)
341+ * .build();
342+ * OperationFuture<Operation> future = speechClient.asyncRecognizeOperationCallable().futureCall(request);
343+ * // Do something
344+ * AsyncRecognizeResponse response = future.get();
345+ * }
346+ * </code></pre>
347+ */
348+ public final OperationCallable <AsyncRecognizeRequest , AsyncRecognizeResponse >
349+ asyncRecognizeOperationCallable () {
350+ return asyncRecognizeOperationCallable ;
293351 }
294352
295353 // AUTO-GENERATED DOCUMENTATION AND METHOD
0 commit comments