|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package com.google.cloud.speech.spi.v1; |
| 15 | + |
| 16 | +import com.google.api.gax.grpc.ApiCallable; |
| 17 | +import com.google.api.gax.protobuf.PathTemplate; |
| 18 | +import com.google.cloud.speech.v1.AudioRequest; |
| 19 | +import com.google.cloud.speech.v1.InitialRecognizeRequest; |
| 20 | +import com.google.cloud.speech.v1.NonStreamingRecognizeResponse; |
| 21 | +import com.google.cloud.speech.v1.RecognizeRequest; |
| 22 | +import io.grpc.ManagedChannel; |
| 23 | +import java.io.Closeable; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.ScheduledExecutorService; |
| 28 | + |
| 29 | +// AUTO-GENERATED DOCUMENTATION AND SERVICE |
| 30 | +/** |
| 31 | + * Service Description: Service that implements Google Cloud Speech API. |
| 32 | + * |
| 33 | + * <p>This class provides the ability to make remote calls to the backing service through method |
| 34 | + * calls that map to API methods. Sample code to get started: |
| 35 | + * |
| 36 | + * <pre> |
| 37 | + * <code> |
| 38 | + * try (SpeechApi speechApi = SpeechApi.create()) { |
| 39 | + * InitialRecognizeRequest initialRequest = InitialRecognizeRequest.newBuilder().build(); |
| 40 | + * AudioRequest audioRequest = AudioRequest.newBuilder().build(); |
| 41 | + * NonStreamingRecognizeResponse response = speechApi.nonStreamingRecognize(initialRequest, audioRequest); |
| 42 | + * } |
| 43 | + * </code> |
| 44 | + * </pre> |
| 45 | + * |
| 46 | + * <p>Note: close() needs to be called on the speechApi object to clean up resources such |
| 47 | + * as threads. In the example above, try-with-resources is used, which automatically calls |
| 48 | + * close(). |
| 49 | + * |
| 50 | + * <p>The surface of this class includes several types of Java methods for each of the API's methods: |
| 51 | + * |
| 52 | + * <ol> |
| 53 | + * <li> A "flattened" method. With this type of method, the fields of the request type have been |
| 54 | + * converted into function parameters. It may be the case that not all fields are available |
| 55 | + * as parameters, and not every API method will have a flattened method entry point. |
| 56 | + * <li> A "request object" method. This type of method only takes one parameter, a request |
| 57 | + * object, which must be constructed before the call. Not every API method will have a request |
| 58 | + * object method. |
| 59 | + * <li> A "callable" method. This type of method takes no parameters and returns an immutable |
| 60 | + * ApiCallable object, which can be used to initiate calls to the service. |
| 61 | + * </ol> |
| 62 | + * |
| 63 | + * <p>See the individual methods for example code. |
| 64 | + * |
| 65 | + * <p>Many parameters require resource names to be formatted in a particular way. To assist |
| 66 | + * with these names, this class includes a format method for each type of name, and additionally |
| 67 | + * a parse method to extract the individual identifiers contained within names that are |
| 68 | + * returned. |
| 69 | + * |
| 70 | + * <p>This class can be customized by passing in a custom instance of SpeechSettings to |
| 71 | + * create(). For example: |
| 72 | + * |
| 73 | + * <pre> |
| 74 | + * <code> |
| 75 | + * SpeechSettings speechSettings = SpeechSettings.defaultBuilder() |
| 76 | + * .provideChannelWith(myCredentials) |
| 77 | + * .build(); |
| 78 | + * SpeechApi speechApi = SpeechApi.create(speechSettings); |
| 79 | + * </code> |
| 80 | + * </pre> |
| 81 | + */ |
| 82 | +@javax.annotation.Generated("by GAPIC") |
| 83 | +public class SpeechApi implements AutoCloseable { |
| 84 | + private final SpeechSettings settings; |
| 85 | + private final ManagedChannel channel; |
| 86 | + private final ScheduledExecutorService executor; |
| 87 | + private final List<AutoCloseable> closeables = new ArrayList<>(); |
| 88 | + |
| 89 | + private final ApiCallable<RecognizeRequest, NonStreamingRecognizeResponse> |
| 90 | + nonStreamingRecognizeCallable; |
| 91 | + |
| 92 | + public final SpeechSettings getSettings() { |
| 93 | + return settings; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Constructs an instance of SpeechApi with default settings. |
| 98 | + */ |
| 99 | + public static final SpeechApi create() throws IOException { |
| 100 | + return create(SpeechSettings.defaultBuilder().build()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Constructs an instance of SpeechApi, using the given settings. |
| 105 | + * The channels are created based on the settings passed in, or defaults for any |
| 106 | + * settings that are not set. |
| 107 | + */ |
| 108 | + public static final SpeechApi create(SpeechSettings settings) throws IOException { |
| 109 | + return new SpeechApi(settings); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Constructs an instance of SpeechApi, using the given settings. |
| 114 | + * This is protected so that it easy to make a subclass, but otherwise, the static |
| 115 | + * factory methods should be preferred. |
| 116 | + */ |
| 117 | + protected SpeechApi(SpeechSettings settings) throws IOException { |
| 118 | + this.settings = settings; |
| 119 | + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); |
| 120 | + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); |
| 121 | + |
| 122 | + this.nonStreamingRecognizeCallable = |
| 123 | + ApiCallable.create(settings.nonStreamingRecognizeSettings(), this.channel, this.executor); |
| 124 | + |
| 125 | + if (settings.getChannelProvider().shouldAutoClose()) { |
| 126 | + closeables.add( |
| 127 | + new Closeable() { |
| 128 | + @Override |
| 129 | + public void close() throws IOException { |
| 130 | + channel.shutdown(); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + if (settings.getExecutorProvider().shouldAutoClose()) { |
| 135 | + closeables.add( |
| 136 | + new Closeable() { |
| 137 | + @Override |
| 138 | + public void close() throws IOException { |
| 139 | + executor.shutdown(); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // AUTO-GENERATED DOCUMENTATION AND METHOD |
| 146 | + /** |
| 147 | + * Perform non-streaming speech-recognition: receive results after all audio |
| 148 | + * has been sent and processed. |
| 149 | + * |
| 150 | + * Sample code: |
| 151 | + * <pre><code> |
| 152 | + * try (SpeechApi speechApi = SpeechApi.create()) { |
| 153 | + * InitialRecognizeRequest initialRequest = InitialRecognizeRequest.newBuilder().build(); |
| 154 | + * AudioRequest audioRequest = AudioRequest.newBuilder().build(); |
| 155 | + * NonStreamingRecognizeResponse response = speechApi.nonStreamingRecognize(initialRequest, audioRequest); |
| 156 | + * } |
| 157 | + * </code></pre> |
| 158 | + * |
| 159 | + * @param initialRequest The `initial_request` message provides information to the recognizer |
| 160 | + * that specifies how to process the request. |
| 161 | + * |
| 162 | + * The first `RecognizeRequest` message must contain an `initial_request`. |
| 163 | + * Any subsequent `RecognizeRequest` messages must not contain an |
| 164 | + * `initial_request`. |
| 165 | + * @param audioRequest The audio data to be recognized. For REST or `NonStreamingRecognize`, all |
| 166 | + * audio data must be contained in the first (and only) `RecognizeRequest` |
| 167 | + * message. For gRPC streaming `Recognize`, sequential chunks of audio data |
| 168 | + * are sent in sequential `RecognizeRequest` messages. |
| 169 | + * @throws com.google.api.gax.grpc.ApiException if the remote call fails |
| 170 | + */ |
| 171 | + public final NonStreamingRecognizeResponse nonStreamingRecognize( |
| 172 | + InitialRecognizeRequest initialRequest, AudioRequest audioRequest) { |
| 173 | + RecognizeRequest request = |
| 174 | + RecognizeRequest.newBuilder() |
| 175 | + .setInitialRequest(initialRequest) |
| 176 | + .setAudioRequest(audioRequest) |
| 177 | + .build(); |
| 178 | + return nonStreamingRecognize(request); |
| 179 | + } |
| 180 | + |
| 181 | + // AUTO-GENERATED DOCUMENTATION AND METHOD |
| 182 | + /** |
| 183 | + * Perform non-streaming speech-recognition: receive results after all audio |
| 184 | + * has been sent and processed. |
| 185 | + * |
| 186 | + * Sample code: |
| 187 | + * <pre><code> |
| 188 | + * try (SpeechApi speechApi = SpeechApi.create()) { |
| 189 | + * InitialRecognizeRequest initialRequest = InitialRecognizeRequest.newBuilder().build(); |
| 190 | + * AudioRequest audioRequest = AudioRequest.newBuilder().build(); |
| 191 | + * RecognizeRequest request = RecognizeRequest.newBuilder() |
| 192 | + * .setInitialRequest(initialRequest) |
| 193 | + * .setAudioRequest(audioRequest) |
| 194 | + * .build(); |
| 195 | + * NonStreamingRecognizeResponse response = speechApi.nonStreamingRecognize(request); |
| 196 | + * } |
| 197 | + * </code></pre> |
| 198 | + * |
| 199 | + * @param request The request object containing all of the parameters for the API call. |
| 200 | + * @throws com.google.api.gax.grpc.ApiException if the remote call fails |
| 201 | + */ |
| 202 | + public final NonStreamingRecognizeResponse nonStreamingRecognize(RecognizeRequest request) { |
| 203 | + return nonStreamingRecognizeCallable().call(request); |
| 204 | + } |
| 205 | + |
| 206 | + // AUTO-GENERATED DOCUMENTATION AND METHOD |
| 207 | + /** |
| 208 | + * Perform non-streaming speech-recognition: receive results after all audio |
| 209 | + * has been sent and processed. |
| 210 | + * |
| 211 | + * Sample code: |
| 212 | + * <pre><code> |
| 213 | + * try (SpeechApi speechApi = SpeechApi.create()) { |
| 214 | + * InitialRecognizeRequest initialRequest = InitialRecognizeRequest.newBuilder().build(); |
| 215 | + * AudioRequest audioRequest = AudioRequest.newBuilder().build(); |
| 216 | + * RecognizeRequest request = RecognizeRequest.newBuilder() |
| 217 | + * .setInitialRequest(initialRequest) |
| 218 | + * .setAudioRequest(audioRequest) |
| 219 | + * .build(); |
| 220 | + * ListenableFuture<NonStreamingRecognizeResponse> future = speechApi.nonStreamingRecognizeCallable().futureCall(request); |
| 221 | + * // Do something |
| 222 | + * NonStreamingRecognizeResponse response = future.get(); |
| 223 | + * } |
| 224 | + * </code></pre> |
| 225 | + */ |
| 226 | + public final ApiCallable<RecognizeRequest, NonStreamingRecognizeResponse> |
| 227 | + nonStreamingRecognizeCallable() { |
| 228 | + return nonStreamingRecognizeCallable; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately |
| 233 | + * cancelled. |
| 234 | + */ |
| 235 | + @Override |
| 236 | + public final void close() throws Exception { |
| 237 | + for (AutoCloseable closeable : closeables) { |
| 238 | + closeable.close(); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments