diff --git a/gcloud-java-speech/pom.xml b/gcloud-java-speech/pom.xml new file mode 100644 index 000000000000..9d72c7cc09c0 --- /dev/null +++ b/gcloud-java-speech/pom.xml @@ -0,0 +1,126 @@ + + + 4.0.0 + gcloud-java-speech + jar + GCloud Java Speech + https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-speech + + Java idiomatic client for Google Cloud Speech. + + + com.google.cloud + gcloud-java-pom + 0.2.9-SNAPSHOT + + + gcloud-java-speech + + + + io.netty + netty-tcnative-boringssl-static + 1.1.33.Fork17 + + + ${project.groupId} + gcloud-java-core + ${project.version} + + + com.google.api.grpc + grpc-google-cloud-speech-v1beta1 + 0.0.7 + + + io.grpc + grpc-all + 0.15.0 + + + com.google.auto.value + auto-value + 1.1 + + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.4 + test + + + + + doclint-java8-disable + + [1.8,) + + + + -Xdoclint:none + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.9.1 + + + generate-sources + add-source + + + generated/src/main/java + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + jar + + + ${javadoc.opts} + + + + + + maven-compiler-plugin + + + + 3.1 + + 1.7 + 1.7 + UTF-8 + -Xlint:unchecked + + + + + diff --git a/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java new file mode 100644 index 000000000000..a77b0d40a1c8 --- /dev/null +++ b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java @@ -0,0 +1,311 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.speech.spi.v1beta1; + +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.RecognitionAudio; +import com.google.cloud.speech.v1beta1.RecognitionConfig; +import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; +import com.google.longrunning.Operation; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: Service that implements Google Cloud Speech API. + * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (SpeechApi speechApi = SpeechApi.create()) {
+ *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+ *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+ *   SyncRecognizeResponse response = speechApi.syncRecognize(config, audio);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the speechApi object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of SpeechSettings to + * create(). For example: + * + *

+ * 
+ * SpeechSettings speechSettings = SpeechSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * SpeechApi speechApi = SpeechApi.create(speechSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class SpeechApi implements AutoCloseable { + private final SpeechSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable syncRecognizeCallable; + private final ApiCallable asyncRecognizeCallable; + + public final SpeechSettings getSettings() { + return settings; + } + + /** + * Constructs an instance of SpeechApi with default settings. + */ + public static final SpeechApi create() throws IOException { + return create(SpeechSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of SpeechApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final SpeechApi create(SpeechSettings settings) throws IOException { + return new SpeechApi(settings); + } + + /** + * Constructs an instance of SpeechApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected SpeechApi(SpeechSettings settings) throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.syncRecognizeCallable = + ApiCallable.create(settings.syncRecognizeSettings(), this.channel, this.executor); + this.asyncRecognizeCallable = + ApiCallable.create(settings.asyncRecognizeSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform synchronous speech-recognition: receive results after all audio + * has been sent and processed. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   SyncRecognizeResponse response = speechApi.syncRecognize(config, audio);
+   * }
+   * 
+ * + * @param config [Required] The `config` message provides information to the recognizer + * that specifies how to process the request. + * @param audio [Required] The audio data to be recognized. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final SyncRecognizeResponse syncRecognize( + RecognitionConfig config, RecognitionAudio audio) { + SyncRecognizeRequest request = + SyncRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build(); + return syncRecognize(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform synchronous speech-recognition: receive results after all audio + * has been sent and processed. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   SyncRecognizeRequest request = SyncRecognizeRequest.newBuilder()
+   *     .setConfig(config)
+   *     .setAudio(audio)
+   *     .build();
+   *   SyncRecognizeResponse response = speechApi.syncRecognize(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final SyncRecognizeResponse syncRecognize(SyncRecognizeRequest request) { + return syncRecognizeCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform synchronous speech-recognition: receive results after all audio + * has been sent and processed. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   SyncRecognizeRequest request = SyncRecognizeRequest.newBuilder()
+   *     .setConfig(config)
+   *     .setAudio(audio)
+   *     .build();
+   *   ListenableFuture<SyncRecognizeResponse> future = speechApi.syncRecognizeCallable().futureCall(request);
+   *   // Do something
+   *   SyncRecognizeResponse response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable syncRecognizeCallable() { + return syncRecognizeCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform asynchronous speech-recognition: receive results via the + * google.longrunning.Operations interface. `Operation.response` returns + * `AsyncRecognizeResponse`. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   Operation response = speechApi.asyncRecognize(config, audio);
+   * }
+   * 
+ * + * @param config [Required] The `config` message provides information to the recognizer + * that specifies how to process the request. + * @param audio [Required] The audio data to be recognized. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Operation asyncRecognize(RecognitionConfig config, RecognitionAudio audio) { + AsyncRecognizeRequest request = + AsyncRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build(); + return asyncRecognize(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform asynchronous speech-recognition: receive results via the + * google.longrunning.Operations interface. `Operation.response` returns + * `AsyncRecognizeResponse`. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
+   *     .setConfig(config)
+   *     .setAudio(audio)
+   *     .build();
+   *   Operation response = speechApi.asyncRecognize(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Operation asyncRecognize(AsyncRecognizeRequest request) { + return asyncRecognizeCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Perform asynchronous speech-recognition: receive results via the + * google.longrunning.Operations interface. `Operation.response` returns + * `AsyncRecognizeResponse`. + * + * Sample code: + *

+   * try (SpeechApi speechApi = SpeechApi.create()) {
+   *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+   *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+   *   AsyncRecognizeRequest request = AsyncRecognizeRequest.newBuilder()
+   *     .setConfig(config)
+   *     .setAudio(audio)
+   *     .build();
+   *   ListenableFuture<Operation> future = speechApi.asyncRecognizeCallable().futureCall(request);
+   *   // Do something
+   *   Operation response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable asyncRecognizeCallable() { + return asyncRecognizeCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java new file mode 100644 index 000000000000..1b3c3774be9c --- /dev/null +++ b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java @@ -0,0 +1,323 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.speech.spi.v1beta1; + +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.PageStreamingDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; +import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SpeechGrpc; +import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.longrunning.Operation; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import org.joda.time.Duration; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link SpeechApi}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (speech.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of syncRecognize to 30 seconds: + * + *

+ * 
+ * SpeechSettings.Builder speechSettingsBuilder =
+ *     SpeechSettings.defaultBuilder();
+ * speechSettingsBuilder.syncRecognizeSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * SpeechSettings speechSettings = speechSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class SpeechSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "speech.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().add("https://www.googleapis.com/auth/cloud-platform").build(); + + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + + private final SimpleCallSettings + syncRecognizeSettings; + private final SimpleCallSettings asyncRecognizeSettings; + + /** + * Returns the object with the settings used for calls to syncRecognize. + */ + public SimpleCallSettings syncRecognizeSettings() { + return syncRecognizeSettings; + } + + /** + * Returns the object with the settings used for calls to asyncRecognize. + */ + public SimpleCallSettings asyncRecognizeSettings() { + return asyncRecognizeSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); + } + + /** + * Returns a new builder for this class. + */ + public static Builder newBuilder() { + return new Builder(); + } + + /** + * Returns a builder containing all the values of this settings class. + */ + public Builder toBuilder() { + return new Builder(this); + } + + private SpeechSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + syncRecognizeSettings = settingsBuilder.syncRecognizeSettings().build(); + asyncRecognizeSettings = settingsBuilder.asyncRecognizeSettings().build(); + } + + /** + * Builder for SpeechSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder + syncRecognizeSettings; + private SimpleCallSettings.Builder asyncRecognizeSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.3) + .setMaxRetryDelay(Duration.millis(60000L)) + .setInitialRpcTimeout(Duration.millis(60000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(60000L)) + .setTotalTimeout(Duration.millis(600000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + syncRecognizeSettings = SimpleCallSettings.newBuilder(SpeechGrpc.METHOD_SYNC_RECOGNIZE); + + asyncRecognizeSettings = SimpleCallSettings.newBuilder(SpeechGrpc.METHOD_ASYNC_RECOGNIZE); + + methodSettingsBuilders = + ImmutableList.of(syncRecognizeSettings, asyncRecognizeSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .syncRecognizeSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .asyncRecognizeSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(SpeechSettings settings) { + super(settings); + + syncRecognizeSettings = settings.syncRecognizeSettings.toBuilder(); + asyncRecognizeSettings = settings.asyncRecognizeSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of(syncRecognizeSettings, asyncRecognizeSettings); + } + + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + /** + * Returns the builder for the settings used for calls to syncRecognize. + */ + public SimpleCallSettings.Builder + syncRecognizeSettings() { + return syncRecognizeSettings; + } + + /** + * Returns the builder for the settings used for calls to asyncRecognize. + */ + public SimpleCallSettings.Builder asyncRecognizeSettings() { + return asyncRecognizeSettings; + } + + @Override + public SpeechSettings build() throws IOException { + return new SpeechSettings(this); + } + } +} diff --git a/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/package-info.java b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/package-info.java new file mode 100644 index 000000000000..8ec07dd8e306 --- /dev/null +++ b/gcloud-java-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/package-info.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/** + * A client to Google Cloud Speech API. + * + * The interfaces provided are listed below, along with a usage sample + * + * ========= + * SpeechApi + * ========= + * + * Service Description: Service that implements Google Cloud Speech API. + * + * Sample for SpeechApi: + *
+ * 
+ * try (SpeechApi speechApi = SpeechApi.create()) {
+ *   RecognitionConfig config = RecognitionConfig.newBuilder().build();
+ *   RecognitionAudio audio = RecognitionAudio.newBuilder().build();
+ *   SyncRecognizeResponse response = speechApi.syncRecognize(config, audio);
+ * }
+ * 
+ * 
+ * + */ +package com.google.cloud.speech.spi.v1beta1; diff --git a/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeech.java b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeech.java new file mode 100644 index 000000000000..35e556f53659 --- /dev/null +++ b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeech.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.speech.spi.v1beta1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.cloud.speech.v1beta1.SpeechGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockSpeech implements MockGrpcService { + private MockSpeechImpl serviceImpl; + + public MockSpeech() { + serviceImpl = new MockSpeechImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return SpeechGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeechImpl.java b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeechImpl.java new file mode 100644 index 000000000000..f625817f868d --- /dev/null +++ b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/MockSpeechImpl.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.speech.spi.v1beta1; + +import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SpeechGrpc.Speech; +import com.google.cloud.speech.v1beta1.StreamingRecognizeRequest; +import com.google.cloud.speech.v1beta1.StreamingRecognizeResponse; +import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; +import com.google.common.collect.Lists; +import com.google.longrunning.Operation; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockSpeechImpl implements Speech { + private ArrayList requests; + private Queue responses; + + public MockSpeechImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void syncRecognize( + SyncRecognizeRequest request, StreamObserver responseObserver) { + SyncRecognizeResponse response = (SyncRecognizeResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void asyncRecognize( + AsyncRecognizeRequest request, StreamObserver responseObserver) { + Operation response = (Operation) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public StreamObserver streamingRecognize( + StreamObserver responseObserver) { + System.err.println("Streaming method is not supported."); + return null; + } +} diff --git a/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/SpeechTest.java b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/SpeechTest.java new file mode 100644 index 000000000000..15a8b5229ec2 --- /dev/null +++ b/gcloud-java-speech/src/test/java/com/google/cloud/speech/spi/v1beta1/SpeechTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.speech.spi.v1beta1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.RecognitionAudio; +import com.google.cloud.speech.v1beta1.RecognitionConfig; +import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; +import com.google.common.collect.Lists; +import com.google.longrunning.Operation; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class SpeechTest { + private static MockSpeech mockSpeech; + private static MockServiceHelper serviceHelper; + private SpeechApi api; + + @BeforeClass + public static void startStaticServer() { + mockSpeech = new MockSpeech(); + serviceHelper = + new MockServiceHelper("in-process-1", Arrays.asList(mockSpeech)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + SpeechSettings settings = + SpeechSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = SpeechApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void syncRecognizeTest() { + SyncRecognizeResponse expectedResponse = SyncRecognizeResponse.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSpeech.setResponses(expectedResponses); + + RecognitionConfig config = RecognitionConfig.newBuilder().build(); + RecognitionAudio audio = RecognitionAudio.newBuilder().build(); + + SyncRecognizeResponse actualResponse = api.syncRecognize(config, audio); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockSpeech.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + SyncRecognizeRequest actualRequest = (SyncRecognizeRequest) actualRequests.get(0); + + Assert.assertEquals(config, actualRequest.getConfig()); + Assert.assertEquals(audio, actualRequest.getAudio()); + } + + @Test + @SuppressWarnings("all") + public void asyncRecognizeTest() { + String name = "name3373707"; + boolean done = true; + Operation expectedResponse = Operation.newBuilder().setName(name).setDone(done).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSpeech.setResponses(expectedResponses); + + RecognitionConfig config = RecognitionConfig.newBuilder().build(); + RecognitionAudio audio = RecognitionAudio.newBuilder().build(); + + Operation actualResponse = api.asyncRecognize(config, audio); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockSpeech.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AsyncRecognizeRequest actualRequest = (AsyncRecognizeRequest) actualRequests.get(0); + + Assert.assertEquals(config, actualRequest.getConfig()); + Assert.assertEquals(audio, actualRequest.getAudio()); + } +} diff --git a/pom.xml b/pom.xml index ee02d866c951..5012d360ab45 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,7 @@ gcloud-java-logging gcloud-java-pubsub gcloud-java-resourcemanager + gcloud-java-speech gcloud-java-storage gcloud-java-translate gcloud-java-vision