Skip to content

Commit 2147d9a

Browse files
callmehiphopstephenplusplus
authored andcommitted
speech: add promise support (#1710)
1 parent e9f6b97 commit 2147d9a

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"mitm": "^1.1.0",
2121
"mkdirp": "^0.5.1",
2222
"mocha": "^2.5.3",
23+
"multiline": "^1.0.2",
2324
"package-json": "^2.4.0",
2425
"propprop": "^0.3.1",
2526
"proxyquire": "^1.7.10",

packages/speech/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ fs.createReadStream('./audio.raw')
5858
// ...
5959
// }
6060
});
61+
62+
// Promises are also supported by omitting callbacks.
63+
speech.recognize('./audio.raw', {
64+
encoding: 'LINEAR16',
65+
sampleRate: 16000
66+
}).then(function(data) {
67+
var transcript = data[0];
68+
});
69+
70+
// It's also possible to integrate with third-party Promise libraries.
71+
var speech = require('@google-cloud/speech')({
72+
promise: require('bluebird')
73+
});
6174
```
6275

6376

packages/speech/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"speech"
5555
],
5656
"dependencies": {
57-
"@google-cloud/common": "^0.6.0",
57+
"@google-cloud/common": "^0.7.0",
5858
"events-intercept": "^2.0.0",
5959
"extend": "^3.0.0",
6060
"google-gax": "^0.7.0",

packages/speech/src/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,14 @@ Speech.prototype.operation = function(name) {
632632
* // }
633633
* // ]
634634
* });
635+
*
636+
* //-
637+
* // If the callback is omitted, we'll return a Promise.
638+
* //-
639+
* speech.recognize('./bridge.raw', config).then(function(data) {
640+
* var results = data[0];
641+
* var apiResponse = data[1];
642+
* });
635643
*/
636644
Speech.prototype.recognize = function(file, config, callback) {
637645
var self = this;
@@ -764,6 +772,14 @@ Speech.prototype.recognize = function(file, config, callback) {
764772
* // ]
765773
* });
766774
* });
775+
*
776+
* //-
777+
* // If the callback is omitted, we'll return a Promise.
778+
* //-
779+
* speech.startRecognition('./bridge.raw', config).then(function(data) {
780+
* var operation = data[0];
781+
* var apiResponse = data[1];
782+
* });
767783
*/
768784
Speech.prototype.startRecognition = function(file, config, callback) {
769785
var self = this;
@@ -811,5 +827,14 @@ Speech.prototype.startRecognition = function(file, config, callback) {
811827
});
812828
};
813829

830+
/*! Developer Documentation
831+
*
832+
* All async methods (except for streams) will return a Promise in the event
833+
* that a callback is omitted.
834+
*/
835+
common.util.promisifyAll(Speech, {
836+
exclude: ['operation']
837+
});
838+
814839
module.exports = Speech;
815840
module.exports.v1beta1 = v1beta1;

packages/speech/test/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ var tmp = require('tmp');
2727

2828
var util = require('@google-cloud/common').util;
2929

30-
var fakeUtil = extend({}, util);
30+
var promisified = false;
31+
var fakeUtil = extend({}, util, {
32+
promisifyAll: function(Class, options) {
33+
if (Class.name !== 'Speech') {
34+
return;
35+
}
36+
37+
promisified = true;
38+
assert.deepEqual(options.exclude, ['operation']);
39+
}
40+
});
3141

3242
function FakeGrpcOperation() {
3343
this.calledWith_ = arguments;
@@ -90,6 +100,10 @@ describe('Speech', function() {
90100
});
91101

92102
describe('instantiation', function() {
103+
it('should promisify all the things', function() {
104+
assert(promisified);
105+
});
106+
93107
it('should normalize the arguments', function() {
94108
var normalizeArguments = fakeUtil.normalizeArguments;
95109
var normalizeArgumentsCalled = false;

test/docs.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var format = require('string-format-obj');
2121
var fs = require('fs');
2222
var glob = require('glob');
2323
var mitm = require('mitm');
24+
var multiline = require('multiline');
2425
var overviews = require('../scripts/docs/config').OVERVIEW;
2526
var path = require('path');
2627
var prop = require('propprop');
@@ -175,7 +176,18 @@ function getDocs(mod) {
175176
function createInstantiationCode(mod) {
176177
var config = overviews[mod] || {};
177178

178-
return format('var {instanceName} = require(\'{path}\')({config});', {
179+
return format(multiline.stripIndent(function() {/*
180+
var {instanceName} = require('{path}')({config});
181+
182+
var api = {instanceName}.api;
183+
if (api) {
184+
Object.keys(api).forEach(function(apiName) {
185+
Object.keys(api[apiName]).forEach(function(method) {
186+
api[apiName][method] = function() {};
187+
});
188+
});
189+
}
190+
*/}), {
179191
instanceName: config.instanceName || mod,
180192
path: '../packages/' + mod,
181193
config: JSON.stringify({

0 commit comments

Comments
 (0)