• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.speech.v1p1beta1.stub;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.api.gax.core.BackgroundResource;
21 import com.google.api.gax.core.BackgroundResourceAggregation;
22 import com.google.api.gax.grpc.GrpcCallSettings;
23 import com.google.api.gax.grpc.GrpcStubCallableFactory;
24 import com.google.api.gax.rpc.BidiStreamingCallable;
25 import com.google.api.gax.rpc.ClientContext;
26 import com.google.api.gax.rpc.OperationCallable;
27 import com.google.api.gax.rpc.UnaryCallable;
28 import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata;
29 import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest;
30 import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse;
31 import com.google.cloud.speech.v1p1beta1.RecognizeRequest;
32 import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
33 import com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest;
34 import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse;
35 import com.google.longrunning.Operation;
36 import com.google.longrunning.stub.GrpcOperationsStub;
37 import io.grpc.MethodDescriptor;
38 import io.grpc.protobuf.ProtoUtils;
39 import java.io.IOException;
40 import java.util.concurrent.TimeUnit;
41 import javax.annotation.Generated;
42 
43 // AUTO-GENERATED DOCUMENTATION AND CLASS.
44 /**
45  * gRPC stub implementation for the Speech service API.
46  *
47  * <p>This class is for advanced usage and reflects the underlying API directly.
48  */
49 @BetaApi
50 @Generated("by gapic-generator-java")
51 public class GrpcSpeechStub extends SpeechStub {
52   private static final MethodDescriptor<RecognizeRequest, RecognizeResponse>
53       recognizeMethodDescriptor =
54           MethodDescriptor.<RecognizeRequest, RecognizeResponse>newBuilder()
55               .setType(MethodDescriptor.MethodType.UNARY)
56               .setFullMethodName("google.cloud.speech.v1p1beta1.Speech/Recognize")
57               .setRequestMarshaller(ProtoUtils.marshaller(RecognizeRequest.getDefaultInstance()))
58               .setResponseMarshaller(ProtoUtils.marshaller(RecognizeResponse.getDefaultInstance()))
59               .build();
60 
61   private static final MethodDescriptor<LongRunningRecognizeRequest, Operation>
62       longRunningRecognizeMethodDescriptor =
63           MethodDescriptor.<LongRunningRecognizeRequest, Operation>newBuilder()
64               .setType(MethodDescriptor.MethodType.UNARY)
65               .setFullMethodName("google.cloud.speech.v1p1beta1.Speech/LongRunningRecognize")
66               .setRequestMarshaller(
67                   ProtoUtils.marshaller(LongRunningRecognizeRequest.getDefaultInstance()))
68               .setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance()))
69               .build();
70 
71   private static final MethodDescriptor<StreamingRecognizeRequest, StreamingRecognizeResponse>
72       streamingRecognizeMethodDescriptor =
73           MethodDescriptor.<StreamingRecognizeRequest, StreamingRecognizeResponse>newBuilder()
74               .setType(MethodDescriptor.MethodType.BIDI_STREAMING)
75               .setFullMethodName("google.cloud.speech.v1p1beta1.Speech/StreamingRecognize")
76               .setRequestMarshaller(
77                   ProtoUtils.marshaller(StreamingRecognizeRequest.getDefaultInstance()))
78               .setResponseMarshaller(
79                   ProtoUtils.marshaller(StreamingRecognizeResponse.getDefaultInstance()))
80               .build();
81 
82   private final UnaryCallable<RecognizeRequest, RecognizeResponse> recognizeCallable;
83   private final UnaryCallable<LongRunningRecognizeRequest, Operation> longRunningRecognizeCallable;
84   private final OperationCallable<
85           LongRunningRecognizeRequest, LongRunningRecognizeResponse, LongRunningRecognizeMetadata>
86       longRunningRecognizeOperationCallable;
87   private final BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse>
88       streamingRecognizeCallable;
89 
90   private final BackgroundResource backgroundResources;
91   private final GrpcOperationsStub operationsStub;
92   private final GrpcStubCallableFactory callableFactory;
93 
create(SpeechStubSettings settings)94   public static final GrpcSpeechStub create(SpeechStubSettings settings) throws IOException {
95     return new GrpcSpeechStub(settings, ClientContext.create(settings));
96   }
97 
create(ClientContext clientContext)98   public static final GrpcSpeechStub create(ClientContext clientContext) throws IOException {
99     return new GrpcSpeechStub(SpeechStubSettings.newBuilder().build(), clientContext);
100   }
101 
create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)102   public static final GrpcSpeechStub create(
103       ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
104     return new GrpcSpeechStub(
105         SpeechStubSettings.newBuilder().build(), clientContext, callableFactory);
106   }
107 
108   /**
109    * Constructs an instance of GrpcSpeechStub, using the given settings. This is protected so that
110    * it is easy to make a subclass, but otherwise, the static factory methods should be preferred.
111    */
GrpcSpeechStub(SpeechStubSettings settings, ClientContext clientContext)112   protected GrpcSpeechStub(SpeechStubSettings settings, ClientContext clientContext)
113       throws IOException {
114     this(settings, clientContext, new GrpcSpeechCallableFactory());
115   }
116 
117   /**
118    * Constructs an instance of GrpcSpeechStub, using the given settings. This is protected so that
119    * it is easy to make a subclass, but otherwise, the static factory methods should be preferred.
120    */
GrpcSpeechStub( SpeechStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)121   protected GrpcSpeechStub(
122       SpeechStubSettings settings,
123       ClientContext clientContext,
124       GrpcStubCallableFactory callableFactory)
125       throws IOException {
126     this.callableFactory = callableFactory;
127     this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
128 
129     GrpcCallSettings<RecognizeRequest, RecognizeResponse> recognizeTransportSettings =
130         GrpcCallSettings.<RecognizeRequest, RecognizeResponse>newBuilder()
131             .setMethodDescriptor(recognizeMethodDescriptor)
132             .build();
133     GrpcCallSettings<LongRunningRecognizeRequest, Operation> longRunningRecognizeTransportSettings =
134         GrpcCallSettings.<LongRunningRecognizeRequest, Operation>newBuilder()
135             .setMethodDescriptor(longRunningRecognizeMethodDescriptor)
136             .build();
137     GrpcCallSettings<StreamingRecognizeRequest, StreamingRecognizeResponse>
138         streamingRecognizeTransportSettings =
139             GrpcCallSettings.<StreamingRecognizeRequest, StreamingRecognizeResponse>newBuilder()
140                 .setMethodDescriptor(streamingRecognizeMethodDescriptor)
141                 .build();
142 
143     this.recognizeCallable =
144         callableFactory.createUnaryCallable(
145             recognizeTransportSettings, settings.recognizeSettings(), clientContext);
146     this.longRunningRecognizeCallable =
147         callableFactory.createUnaryCallable(
148             longRunningRecognizeTransportSettings,
149             settings.longRunningRecognizeSettings(),
150             clientContext);
151     this.longRunningRecognizeOperationCallable =
152         callableFactory.createOperationCallable(
153             longRunningRecognizeTransportSettings,
154             settings.longRunningRecognizeOperationSettings(),
155             clientContext,
156             operationsStub);
157     this.streamingRecognizeCallable =
158         callableFactory.createBidiStreamingCallable(
159             streamingRecognizeTransportSettings,
160             settings.streamingRecognizeSettings(),
161             clientContext);
162 
163     this.backgroundResources =
164         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
165   }
166 
getOperationsStub()167   public GrpcOperationsStub getOperationsStub() {
168     return operationsStub;
169   }
170 
171   @Override
recognizeCallable()172   public UnaryCallable<RecognizeRequest, RecognizeResponse> recognizeCallable() {
173     return recognizeCallable;
174   }
175 
176   @Override
longRunningRecognizeCallable()177   public UnaryCallable<LongRunningRecognizeRequest, Operation> longRunningRecognizeCallable() {
178     return longRunningRecognizeCallable;
179   }
180 
181   @Override
182   public OperationCallable<
183           LongRunningRecognizeRequest, LongRunningRecognizeResponse, LongRunningRecognizeMetadata>
longRunningRecognizeOperationCallable()184       longRunningRecognizeOperationCallable() {
185     return longRunningRecognizeOperationCallable;
186   }
187 
188   @Override
189   public BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse>
streamingRecognizeCallable()190       streamingRecognizeCallable() {
191     return streamingRecognizeCallable;
192   }
193 
194   @Override
close()195   public final void close() {
196     try {
197       backgroundResources.close();
198     } catch (RuntimeException e) {
199       throw e;
200     } catch (Exception e) {
201       throw new IllegalStateException("Failed to close resource", e);
202     }
203   }
204 
205   @Override
shutdown()206   public void shutdown() {
207     backgroundResources.shutdown();
208   }
209 
210   @Override
isShutdown()211   public boolean isShutdown() {
212     return backgroundResources.isShutdown();
213   }
214 
215   @Override
isTerminated()216   public boolean isTerminated() {
217     return backgroundResources.isTerminated();
218   }
219 
220   @Override
shutdownNow()221   public void shutdownNow() {
222     backgroundResources.shutdownNow();
223   }
224 
225   @Override
awaitTermination(long duration, TimeUnit unit)226   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
227     return backgroundResources.awaitTermination(duration, unit);
228   }
229 }
230