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