From 32f660e676632fb4c07533368773ce0245aa2d9d Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Thu, 22 Sep 2016 21:20:13 -0700 Subject: [PATCH 1/5] Introduce auto-generated Gapic client for speech API --- packages/speech/package.json | 2 + packages/speech/src/index.js | 1 + packages/speech/src/v1beta1/index.js | 32 +++ packages/speech/src/v1beta1/speech_api.js | 254 ++++++++++++++++++ .../src/v1beta1/speech_client_config.json | 43 +++ 5 files changed, 332 insertions(+) create mode 100644 packages/speech/src/v1beta1/index.js create mode 100644 packages/speech/src/v1beta1/speech_api.js create mode 100644 packages/speech/src/v1beta1/speech_client_config.json diff --git a/packages/speech/package.json b/packages/speech/package.json index c6b29efe1dd..0ec84195198 100644 --- a/packages/speech/package.json +++ b/packages/speech/package.json @@ -55,8 +55,10 @@ ], "dependencies": { "@google-cloud/common": "^0.5.0", + "arguejs": "^0.2.3", "events-intercept": "^2.0.0", "extend": "^3.0.0", + "google-gax": "^0.6.0", "google-proto-files": "^0.7.0", "is": "^3.1.0", "modelo": "^4.2.0", diff --git a/packages/speech/src/index.js b/packages/speech/src/index.js index 7697ea3d904..492529bbb79 100644 --- a/packages/speech/src/index.js +++ b/packages/speech/src/index.js @@ -827,3 +827,4 @@ Speech.prototype.startRecognition = function(file, config, callback) { }; module.exports = Speech; +module.exports.v1beta1 = require('./v1beta1'); diff --git a/packages/speech/src/v1beta1/index.js b/packages/speech/src/v1beta1/index.js new file mode 100644 index 00000000000..f3f4c52e7f3 --- /dev/null +++ b/packages/speech/src/v1beta1/index.js @@ -0,0 +1,32 @@ +/*! + * 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. + */ +'use strict'; + +var speechApi = require('./speech_api'); +var gax = require('google-gax'); +var extend = require('extend'); + +function v1beta1(options) { + options = extend({ + scopes: v1beta1.ALL_SCOPES + }, options); + var gaxGrpc = gax.grpc(options); + var result = {}; + return speechApi(gaxGrpc); +} +v1beta1.SERVICE_ADDRESS = speechApi.SERVICE_ADDRESS; +v1beta1.ALL_SCOPES = speechApi.ALL_SCOPES; +module.exports = v1beta1; diff --git a/packages/speech/src/v1beta1/speech_api.js b/packages/speech/src/v1beta1/speech_api.js new file mode 100644 index 00000000000..69d253571cb --- /dev/null +++ b/packages/speech/src/v1beta1/speech_api.js @@ -0,0 +1,254 @@ +/* + * 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. + * + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto, + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google + * engineers. + * + * The only allowed edits are to method and file documentation. A 3-way + * merge preserves those additions if the generated source changes. + */ +/* TODO: introduce line-wrapping so that it never exceeds the limit. */ +/* jscs: disable maximumLineLength */ +'use strict'; + +var arguejs = require('arguejs'); +var configData = require('./speech_client_config'); +var extend = require('extend'); +var gax = require('google-gax'); + +var SERVICE_ADDRESS = 'speech.googleapis.com'; + +var DEFAULT_SERVICE_PORT = 443; + +var CODE_GEN_NAME_VERSION = 'gapic/0.1.0'; + +var DEFAULT_TIMEOUT = 30; + +/** + * The scopes needed to make gRPC calls to all of the methods defined in + * this service. + */ +var ALL_SCOPES = [ + 'https://www.googleapis.com/auth/cloud-platform' +]; + +/** + * Service that implements Google Cloud Speech API. + * + * This will be created through a builder function which can be obtained by the module. + * See the following example of how to initialize the module and how to access to the builder. + * @see {@link speechApi} + * + * @example + * var speechV1beta1 = require('@google-cloud/speech').v1beta1({ + * // optional auth parameters. + * }); + * var api = speechV1beta1.speechApi(); + * + * @class + */ +function SpeechApi(gaxGrpc, grpcClients, opts) { + opts = opts || {}; + var servicePath = opts.servicePath || SERVICE_ADDRESS; + var port = opts.port || DEFAULT_SERVICE_PORT; + var sslCreds = opts.sslCreds || null; + var clientConfig = opts.clientConfig || {}; + var timeout = opts.timeout || DEFAULT_TIMEOUT; + var appName = opts.appName || 'gax'; + var appVersion = opts.appVersion || gax.version; + + var googleApiClient = [ + appName + '/' + appVersion, + CODE_GEN_NAME_VERSION, + 'gax/' + gax.version, + 'nodejs/' + process.version].join(' '); + + var defaults = gaxGrpc.constructSettings( + 'google.cloud.speech.v1beta1.Speech', + configData, + clientConfig, + timeout, + null, + null, + {'x-goog-api-client': googleApiClient}); + + var speechStub = gaxGrpc.createStub( + servicePath, + port, + grpcClients.speechClient.google.cloud.speech.v1beta1.Speech, + {sslCreds: sslCreds}); + var speechStubMethods = [ + 'syncRecognize', + 'asyncRecognize' + ]; + speechStubMethods.forEach(function(methodName) { + this['_' + methodName] = gax.createApiCall( + speechStub.then(function(speechStub) { + return speechStub[methodName].bind(speechStub); + }), + defaults[methodName]); + }.bind(this)); +} + +// Service calls + +/** + * Perform synchronous speech-recognition: receive results after all audio + * has been sent and processed. + * + * @param {Object} config + * [Required] The `config` message provides information to the recognizer + * that specifies how to process the request. + * + * This object should have the same structure as [RecognitionConfig]{@link RecognitionConfig} + * @param {Object} audio + * [Required] The audio data to be recognized. + * + * This object should have the same structure as [RecognitionAudio]{@link RecognitionAudio} + * @param {gax.CallOptions=} options + * Overrides the default settings for this call, e.g, timeout, + * retries, etc. + * @param {function(?Error, ?Object)=} callback + * The function which will be called with the result of the API call. + * + * The second parameter to the callback is an object representing [SyncRecognizeResponse]{@link SyncRecognizeResponse} + * @returns {gax.EventEmitter} - the event emitter to handle the call + * status. + * + * @example + * + * var api = speechV1beta1.speechApi(); + * var config = {}; + * var audio = {}; + * api.syncRecognize(config, audio, function(err, response) { + * if (err) { + * console.error(err); + * return; + * } + * // doThingsWith(response) + * }); + */ +SpeechApi.prototype.syncRecognize = function syncRecognize() { + var args = arguejs({ + config: Object, + audio: Object, + options: [gax.CallOptions], + callback: [Function] + }, arguments); + var req = { + config: args.config, + audio: args.audio + }; + return this._syncRecognize(req, args.options, args.callback); +}; + +/** + * Perform asynchronous speech-recognition: receive results via the + * google.longrunning.Operations interface. `Operation.response` returns + * `AsyncRecognizeResponse`. + * + * @param {Object} config + * [Required] The `config` message provides information to the recognizer + * that specifies how to process the request. + * + * This object should have the same structure as [RecognitionConfig]{@link RecognitionConfig} + * @param {Object} audio + * [Required] The audio data to be recognized. + * + * This object should have the same structure as [RecognitionAudio]{@link RecognitionAudio} + * @param {gax.CallOptions=} options + * Overrides the default settings for this call, e.g, timeout, + * retries, etc. + * @param {function(?Error, ?Object)=} callback + * The function which will be called with the result of the API call. + * + * The second parameter to the callback is an object representing [google.longrunning.Operation]{@link external:"google.longrunning.Operation"} + * @returns {gax.EventEmitter} - the event emitter to handle the call + * status. + * + * @example + * + * var api = speechV1beta1.speechApi(); + * var config = {}; + * var audio = {}; + * api.asyncRecognize(config, audio, function(err, response) { + * if (err) { + * console.error(err); + * return; + * } + * // doThingsWith(response) + * }); + */ +SpeechApi.prototype.asyncRecognize = function asyncRecognize() { + var args = arguejs({ + config: Object, + audio: Object, + options: [gax.CallOptions], + callback: [Function] + }, arguments); + var req = { + config: args.config, + audio: args.audio + }; + return this._asyncRecognize(req, args.options, args.callback); +}; + +function SpeechApiBuilder(gaxGrpc) { + if (!(this instanceof SpeechApiBuilder)) { + return new SpeechApiBuilder(gaxGrpc); + } + + var speechClient = gaxGrpc.load([{ + root: require('google-proto-files')('..'), + file: 'google/cloud/speech/v1beta1/cloud_speech.proto' + }]); + extend(this, speechClient.google.cloud.speech.v1beta1); + + var grpcClients = { + speechClient: speechClient + }; + + /** + * Build a new instance of {@link SpeechApi}. + * + * @param {Object=} opts - The optional parameters. + * @param {String=} opts.servicePath + * The domain name of the API remote host. + * @param {number=} opts.port + * The port on which to connect to the remote host. + * @param {grpc.ClientCredentials=} opts.sslCreds + * A ClientCredentials for use with an SSL-enabled channel. + * @param {Object=} opts.clientConfig + * The customized config to build the call settings. See + * {@link gax.constructSettings} for the format. + * @param {number=} opts.timeout + * The default timeout, in seconds, for calls made through this client. + * @param {number=} opts.appName + * The codename of the calling service. + * @param {String=} opts.appVersion + * The version of the calling service. + */ + this.speechApi = function(opts) { + return new SpeechApi(gaxGrpc, grpcClients, opts); + }; + extend(this.speechApi, SpeechApi); +} +module.exports = SpeechApiBuilder; +module.exports.SERVICE_ADDRESS = SERVICE_ADDRESS; +module.exports.ALL_SCOPES = ALL_SCOPES; \ No newline at end of file diff --git a/packages/speech/src/v1beta1/speech_client_config.json b/packages/speech/src/v1beta1/speech_client_config.json new file mode 100644 index 00000000000..4d499650afb --- /dev/null +++ b/packages/speech/src/v1beta1/speech_client_config.json @@ -0,0 +1,43 @@ +{ + "interfaces": { + "google.cloud.speech.v1beta1.Speech": { + "retry_codes": { + "retry_codes_def": { + "idempotent": [ + "DEADLINE_EXCEEDED", + "UNAVAILABLE" + ], + "non_idempotent": [] + } + }, + "retry_params": { + "default": { + "initial_retry_delay_millis": 100, + "retry_delay_multiplier": 1.3, + "max_retry_delay_millis": 60000, + "initial_rpc_timeout_millis": 60000, + "rpc_timeout_multiplier": 1.0, + "max_rpc_timeout_millis": 60000, + "total_timeout_millis": 600000 + } + }, + "methods": { + "SyncRecognize": { + "timeout_millis": 60000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + }, + "AsyncRecognize": { + "timeout_millis": 60000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + }, + "StreamingRecognize": { + "timeout_millis": 60000, + "retry_codes_name": "non_idempotent", + "retry_params_name": "default" + } + } + } + } +} From 5d196e17746d6b246b68f987052067dbcfd1049e Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Fri, 23 Sep 2016 11:19:05 -0700 Subject: [PATCH 2/5] Fix a typo --- packages/speech/src/v1beta1/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/speech/src/v1beta1/index.js b/packages/speech/src/v1beta1/index.js index f3f4c52e7f3..b54f6f561b9 100644 --- a/packages/speech/src/v1beta1/index.js +++ b/packages/speech/src/v1beta1/index.js @@ -24,7 +24,6 @@ function v1beta1(options) { scopes: v1beta1.ALL_SCOPES }, options); var gaxGrpc = gax.grpc(options); - var result = {}; return speechApi(gaxGrpc); } v1beta1.SERVICE_ADDRESS = speechApi.SERVICE_ADDRESS; From 191474d76f1400735f9c9a07288401c286e9852c Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Fri, 23 Sep 2016 14:57:45 -0700 Subject: [PATCH 3/5] Coverage ignorance for a line. --- packages/speech/src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/speech/src/index.js b/packages/speech/src/index.js index 492529bbb79..b8be69ef2ca 100644 --- a/packages/speech/src/index.js +++ b/packages/speech/src/index.js @@ -137,6 +137,7 @@ Speech.prototype.endpointerTypes = { */ Speech.detectEncoding_ = function(filename) { if (!is.string(filename)) { + // istanbul ignore next return; } From d602ee1e0cb2e015d75dde0c66958441a185a850 Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Fri, 23 Sep 2016 15:59:59 -0700 Subject: [PATCH 4/5] Add test case for detectEncoding_ for better coverage. --- packages/speech/src/index.js | 1 - packages/speech/test/index.js | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/speech/src/index.js b/packages/speech/src/index.js index b8be69ef2ca..492529bbb79 100644 --- a/packages/speech/src/index.js +++ b/packages/speech/src/index.js @@ -137,7 +137,6 @@ Speech.prototype.endpointerTypes = { */ Speech.detectEncoding_ = function(filename) { if (!is.string(filename)) { - // istanbul ignore next return; } diff --git a/packages/speech/test/index.js b/packages/speech/test/index.js index 137b75c5ef8..8ef35616577 100644 --- a/packages/speech/test/index.js +++ b/packages/speech/test/index.js @@ -158,6 +158,10 @@ describe('Speech', function() { Speech.detectEncoding_('blah.mp3'); }, /Encoding could not be determined for file: blah\.mp3/); }); + + it('should return nothing if the argument is not a string', function() { + assert.equal(Speech.detectEncoding_({}), undefined); + }); }); describe('findFile_', function() { From 34f69fff1bfad42c5f0625ae5fdd0ad7e42e7c81 Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Mon, 26 Sep 2016 14:15:12 -0700 Subject: [PATCH 5/5] Fix assertion line to make sure returning undefined exactly. --- packages/speech/test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/speech/test/index.js b/packages/speech/test/index.js index 8ef35616577..00bb64adbfa 100644 --- a/packages/speech/test/index.js +++ b/packages/speech/test/index.js @@ -160,7 +160,7 @@ describe('Speech', function() { }); it('should return nothing if the argument is not a string', function() { - assert.equal(Speech.detectEncoding_({}), undefined); + assert.strictEqual(Speech.detectEncoding_({}), undefined); }); });