diff --git a/packages/speech/src/index.js b/packages/speech/src/index.js index 492529bbb79..2c69f8e8c93 100644 --- a/packages/speech/src/index.js +++ b/packages/speech/src/index.js @@ -34,6 +34,7 @@ var request = require('request'); var streamEvents = require('stream-events'); var through = require('through2'); var util = require('util'); +var v1beta1 = require('./v1beta1'); /** * The [Cloud Speech API](https://cloud.google.com/speech/docs) enables easy @@ -87,6 +88,10 @@ function Speech(options) { }; common.GrpcService.call(this, config, options); + + this.api = { + Speech: v1beta1(options).speechApi(options) + }; } util.inherits(Speech, common.GrpcService); @@ -631,11 +636,6 @@ Speech.prototype.operation = function(name) { Speech.prototype.recognize = function(file, config, callback) { var self = this; - var protoOpts = { - service: 'Speech', - method: 'syncRecognize' - }; - config = extend({}, config); if (!config.encoding) { @@ -651,21 +651,16 @@ Speech.prototype.recognize = function(file, config, callback) { return; } - var reqOpts = { - audio: foundFile, - config: config - }; - - self.request(protoOpts, reqOpts, function(err, apiResponse) { + self.api.Speech.syncRecognize(config, foundFile, function(err, resp) { if (err) { - callback(err, null, apiResponse); + callback(err, null, resp); return; } - var response = new self.protos.Speech.SyncRecognizeResponse(apiResponse); + var response = new self.protos.Speech.SyncRecognizeResponse(resp); var results = Speech.formatResults_(response.results, verboseMode); - callback(null, results, apiResponse); + callback(null, results, resp); }); }); }; @@ -773,11 +768,6 @@ Speech.prototype.recognize = function(file, config, callback) { Speech.prototype.startRecognition = function(file, config, callback) { var self = this; - var protoOpts = { - service: 'Speech', - method: 'asyncRecognize' - }; - config = extend({}, config); if (!config.encoding) { @@ -793,19 +783,14 @@ Speech.prototype.startRecognition = function(file, config, callback) { return; } - var reqOpts = { - audio: foundFile, - config: config - }; - - self.request(protoOpts, reqOpts, function(err, apiResponse) { + self.api.Speech.asyncRecognize(config, foundFile, function(err, resp) { if (err) { - callback(err, null, apiResponse); + callback(err, null, resp); return; } - var operation = self.operation(apiResponse.name); - operation.metadata = apiResponse; + var operation = self.operation(resp.name); + operation.metadata = resp; // Intercept the "complete" event to decode and format the results of the // operation for the user. @@ -821,10 +806,10 @@ Speech.prototype.startRecognition = function(file, config, callback) { callback(null, Speech.formatResults_(response.results, verboseMode)); }); - callback(null, operation, apiResponse); + callback(null, operation, resp); }); }); }; module.exports = Speech; -module.exports.v1beta1 = require('./v1beta1'); +module.exports.v1beta1 = v1beta1; diff --git a/packages/speech/test/index.js b/packages/speech/test/index.js index 00bb64adbfa..5bef48918b0 100644 --- a/packages/speech/test/index.js +++ b/packages/speech/test/index.js @@ -582,6 +582,8 @@ describe('Speech', function() { Speech.findFile_ = function(files, callback) { callback(null, FOUND_FILE); }; + + speech.api.Speech.syncRecognize = util.noop; }); it('should find the files', function(done) { @@ -594,16 +596,13 @@ describe('Speech', function() { }); it('should make the correct request', function(done) { - speech.request = function(protoOpts, reqOpts) { - assert.deepEqual(protoOpts, { - service: 'Speech', - method: 'syncRecognize' + speech.api.Speech.syncRecognize = function(config, file) { + var expectedConfig = extend({}, CONFIG, { + encoding: DETECTED_ENCODING }); + assert.deepEqual(config, expectedConfig); - assert.deepEqual(reqOpts, { - config: extend({}, CONFIG, { encoding: DETECTED_ENCODING }), - audio: FOUND_FILE - }); + assert.strictEqual(file, FOUND_FILE); done(); }; @@ -620,8 +619,8 @@ describe('Speech', function() { done(); // Will cause test to fail. }; - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.encoding, config.encoding); + speech.api.Speech.syncRecognize = function(config_) { + assert.strictEqual(config_.encoding, config.encoding); done(); }; @@ -636,8 +635,8 @@ describe('Speech', function() { return expectedEncoding; }; - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.encoding, expectedEncoding); + speech.api.Speech.syncRecognize = function(config) { + assert.strictEqual(config.encoding, expectedEncoding); done(); }; @@ -662,7 +661,7 @@ describe('Speech', function() { var apiResponse = {}; beforeEach(function() { - speech.request = function(protoOpts, reqOpts, callback) { + speech.api.Speech.syncRecognize = function(config, file, callback) { callback(error, apiResponse); }; }); @@ -699,7 +698,7 @@ describe('Speech', function() { return formattedResults; }; - speech.request = function(protoOpts, reqOpts, callback) { + speech.api.Speech.syncRecognize = function(config, file, callback) { callback(null, apiResponse); }; }); @@ -755,8 +754,8 @@ describe('Speech', function() { }); it('should delete verbose option from request object', function(done) { - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.verbose, undefined); + speech.api.Speech.syncRecognize = function(config) { + assert.strictEqual(config.verbose, undefined); done(); }; @@ -783,6 +782,8 @@ describe('Speech', function() { Speech.findFile_ = function(files, callback) { callback(null, FOUND_FILE); }; + + speech.api.Speech.asyncRecognize = util.noop; }); it('should find the files', function(done) { @@ -795,16 +796,13 @@ describe('Speech', function() { }); it('should make the correct request', function(done) { - speech.request = function(protoOpts, reqOpts) { - assert.deepEqual(protoOpts, { - service: 'Speech', - method: 'asyncRecognize' + speech.api.Speech.asyncRecognize = function(config, file) { + var expectedConfig = extend({}, CONFIG, { + encoding: DETECTED_ENCODING }); + assert.deepEqual(config, expectedConfig); - assert.deepEqual(reqOpts, { - config: extend({}, CONFIG, { encoding: DETECTED_ENCODING }), - audio: FOUND_FILE - }); + assert.strictEqual(file, FOUND_FILE); done(); }; @@ -821,8 +819,8 @@ describe('Speech', function() { done(); // Will cause test to fail. }; - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.encoding, config.encoding); + speech.api.Speech.asyncRecognize = function(config_) { + assert.strictEqual(config_.encoding, config.encoding); done(); }; @@ -837,8 +835,8 @@ describe('Speech', function() { return expectedEncoding; }; - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.encoding, expectedEncoding); + speech.api.Speech.asyncRecognize = function(config) { + assert.strictEqual(config.encoding, expectedEncoding); done(); }; @@ -863,7 +861,7 @@ describe('Speech', function() { var apiResponse = {}; beforeEach(function() { - speech.request = function(protoOpts, reqOpts, callback) { + speech.api.Speech.asyncRecognize = function(config, file, callback) { callback(error, apiResponse); }; }); @@ -907,7 +905,7 @@ describe('Speech', function() { return through.obj(); }; - speech.request = function(protoOpts, reqOpts, callback) { + speech.api.Speech.asyncRecognize = function(config, file, callback) { callback(null, apiResponse); }; }); @@ -985,8 +983,8 @@ describe('Speech', function() { }); it('should delete verbose option from request object', function(done) { - speech.request = function(protoOpts, reqOpts) { - assert.strictEqual(reqOpts.config.verbose, undefined); + speech.api.Speech.asyncRecognize = function(config) { + assert.strictEqual(config.verbose, undefined); done(); };