From efe1d7fb090a26776d55d6494888a33d06e5f210 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Mon, 17 Oct 2016 20:05:45 -0400 Subject: [PATCH] speech: add promise support (#1710) --- packages/google-cloud-node/README.md | 13 ++++++++++++ packages/google-cloud-node/package.json | 2 +- packages/google-cloud-node/src/index.js | 25 ++++++++++++++++++++++++ packages/google-cloud-node/test/index.js | 16 ++++++++++++++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/google-cloud-node/README.md b/packages/google-cloud-node/README.md index 016936023223..8f47b7efec7a 100644 --- a/packages/google-cloud-node/README.md +++ b/packages/google-cloud-node/README.md @@ -58,6 +58,19 @@ fs.createReadStream('./audio.raw') // ... // } }); + +// Promises are also supported by omitting callbacks. +speech.recognize('./audio.raw', { + encoding: 'LINEAR16', + sampleRate: 16000 +}).then(function(data) { + var transcript = data[0]; +}); + +// It's also possible to integrate with third-party Promise libraries. +var speech = require('@google-cloud/speech')({ + promise: require('bluebird') +}); ``` diff --git a/packages/google-cloud-node/package.json b/packages/google-cloud-node/package.json index 70c5427274fb..6f5771f43096 100644 --- a/packages/google-cloud-node/package.json +++ b/packages/google-cloud-node/package.json @@ -54,7 +54,7 @@ "speech" ], "dependencies": { - "@google-cloud/common": "^0.6.0", + "@google-cloud/common": "^0.7.0", "events-intercept": "^2.0.0", "extend": "^3.0.0", "google-gax": "^0.7.0", diff --git a/packages/google-cloud-node/src/index.js b/packages/google-cloud-node/src/index.js index a312aa9feb7d..ec6f9c7a38eb 100644 --- a/packages/google-cloud-node/src/index.js +++ b/packages/google-cloud-node/src/index.js @@ -632,6 +632,14 @@ Speech.prototype.operation = function(name) { * // } * // ] * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * speech.recognize('./bridge.raw', config).then(function(data) { + * var results = data[0]; + * var apiResponse = data[1]; + * }); */ Speech.prototype.recognize = function(file, config, callback) { var self = this; @@ -764,6 +772,14 @@ Speech.prototype.recognize = function(file, config, callback) { * // ] * }); * }); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * speech.startRecognition('./bridge.raw', config).then(function(data) { + * var operation = data[0]; + * var apiResponse = data[1]; + * }); */ Speech.prototype.startRecognition = function(file, config, callback) { var self = this; @@ -811,5 +827,14 @@ Speech.prototype.startRecognition = function(file, config, callback) { }); }; +/*! Developer Documentation + * + * All async methods (except for streams) will return a Promise in the event + * that a callback is omitted. + */ +common.util.promisifyAll(Speech, { + exclude: ['operation'] +}); + module.exports = Speech; module.exports.v1beta1 = v1beta1; diff --git a/packages/google-cloud-node/test/index.js b/packages/google-cloud-node/test/index.js index 1ebc4d1e1df7..e1b414f1a772 100644 --- a/packages/google-cloud-node/test/index.js +++ b/packages/google-cloud-node/test/index.js @@ -27,7 +27,17 @@ var tmp = require('tmp'); var util = require('@google-cloud/common').util; -var fakeUtil = extend({}, util); +var promisified = false; +var fakeUtil = extend({}, util, { + promisifyAll: function(Class, options) { + if (Class.name !== 'Speech') { + return; + } + + promisified = true; + assert.deepEqual(options.exclude, ['operation']); + } +}); function FakeGrpcOperation() { this.calledWith_ = arguments; @@ -90,6 +100,10 @@ describe('Speech', function() { }); describe('instantiation', function() { + it('should promisify all the things', function() { + assert(promisified); + }); + it('should normalize the arguments', function() { var normalizeArguments = fakeUtil.normalizeArguments; var normalizeArgumentsCalled = false;