Skip to content

Regenerate speech API. Now it contains streamingRecognize method. #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@google-cloud/common": "^0.7.0",
"events-intercept": "^2.0.0",
"extend": "^3.0.0",
"google-gax": "^0.8.1",
"google-gax": "^0.9.1",
"google-proto-files": "^0.8.0",
"is": "^3.1.0",
"modelo": "^4.2.0",
Expand Down
7 changes: 1 addition & 6 deletions packages/speech/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,13 @@ Speech.formatResults_ = function(resultSets, verboseMode) {
Speech.prototype.createRecognizeStream = function(config) {
var self = this;

var protoOpts = {
service: 'Speech',
method: 'streamingRecognize'
};

var verboseMode = config.verbose === true;
delete config.verbose;

var recognizeStream = streamEvents(pumpify.obj());

recognizeStream.once('writing', function() {
var requestStream = self.requestWritableStream(protoOpts);
var requestStream = self.api.Speech.streamingRecognize();

requestStream.on('response', function(response) {
recognizeStream.emit('response', response);
Expand Down
40 changes: 36 additions & 4 deletions packages/speech/src/v1beta1/speech_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var DEFAULT_SERVICE_PORT = 443;

var CODE_GEN_NAME_VERSION = 'gapic/0.1.0';

var STREAM_DESCRIPTORS = {
streamingRecognize: new gax.StreamDescriptor(gax.StreamType.BIDI_STREAMING)
};

/**
* The scopes needed to make gRPC calls to all of the methods defined in
Expand Down Expand Up @@ -80,8 +83,6 @@ function SpeechApi(gaxGrpc, grpcClients, opts) {
'google.cloud.speech.v1beta1.Speech',
configData,
clientConfig,
null,
null,
{'x-goog-api-client': googleApiClient});

var speechStub = gaxGrpc.createStub(
Expand All @@ -91,14 +92,16 @@ function SpeechApi(gaxGrpc, grpcClients, opts) {
{sslCreds: sslCreds});
var speechStubMethods = [
'syncRecognize',
'asyncRecognize'
'asyncRecognize',
'streamingRecognize'
];
speechStubMethods.forEach(function(methodName) {
this['_' + methodName] = gax.createApiCall(
speechStub.then(function(speechStub) {
return speechStub[methodName].bind(speechStub);
}),
defaults[methodName]);
defaults[methodName],
STREAM_DESCRIPTORS[methodName]);
}.bind(this));
}

Expand Down Expand Up @@ -208,6 +211,35 @@ SpeechApi.prototype.asyncRecognize = function(request, options, callback) {
return this._asyncRecognize(request, options, callback);
};

/**
* Perform bidirectional streaming speech-recognition: receive results while
* sending audio. This method is only available via the gRPC API (not REST).
*
* @param {Object=} options
* Optional parameters. You can override the default settings for this call, e.g, timeout,
* retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details.
* @returns {Stream}
* An object stream which is both readable and writable. It accepts objects
* representing [StreamingRecognizeRequest]{@link StreamingRecognizeRequest} for write() method, and
* will emit objects representing [StreamingRecognizeResponse]{@link StreamingRecognizeResponse} on 'data' event asynchronously.
*
* @example
*
* var api = speechV1beta1.speechApi();
* var stream = api.streamingRecognize().on('data', function(response) {
* // doThingsWith(response);
* });
* var request = {};
* // Write request objects.
* stream.write(request);
*/
SpeechApi.prototype.streamingRecognize = function(options) {
if (options === undefined) {
options = {};
}
return this._streamingRecognize(options);
};

function SpeechApiBuilder(gaxGrpc) {
if (!(this instanceof SpeechApiBuilder)) {
return new SpeechApiBuilder(gaxGrpc);
Expand Down
72 changes: 38 additions & 34 deletions packages/speech/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,20 @@ describe('Speech', function() {

stream.setPipeline = util.noop;

speech.requestWritableStream = function() {
requestStream = through.obj();
return requestStream;
speech.api.Speech = {
streamingRecognize: function() {
requestStream = through.obj();
return requestStream;
}
};
});

it('should make the correct request once writing started', function(done) {
speech.requestWritableStream = function(protoOpts) {
assert.deepEqual(protoOpts, {
service: 'Speech',
method: 'streamingRecognize'
});

setImmediate(done);

return through.obj();
speech.api.Speech = {
streamingRecognize: function() {
setImmediate(done);
return through.obj();
}
};

stream.emit('writing');
Expand All @@ -477,31 +475,35 @@ describe('Speech', function() {
done();
});

speech.requestWritableStream = function() {
var requestStream = through.obj();
speech.api.Speech = {
streamingRecognize: function() {
var requestStream = through.obj();

setImmediate(function() {
requestStream.emit('response', response);
});
setImmediate(function() {
requestStream.emit('response', response);
});

return requestStream;
return requestStream;
}
};

stream.emit('writing');
});

it('should send the initial write to the request stream', function(done) {
speech.requestWritableStream = function() {
var requestStream = through.obj();
speech.api.Speech = {
streamingRecognize: function() {
var requestStream = through.obj();

requestStream.once('data', function(data) {
assert.deepEqual(data, {
streamingConfig: CONFIG
requestStream.once('data', function(data) {
assert.deepEqual(data, {
streamingConfig: CONFIG
});
done();
});
done();
});

return requestStream;
return requestStream;
}
};

stream.emit('writing');
Expand Down Expand Up @@ -582,17 +584,19 @@ describe('Speech', function() {
verbose: true
});

speech.requestWritableStream = function() {
var stream = through.obj();
speech.api.Speech = {
streamingRecognize: function() {
var stream = through.obj();

stream.on('data', function(data) {
assert.deepEqual(data, {
streamingConfig: {} // No `verbose` property.
stream.on('data', function(data) {
assert.deepEqual(data, {
streamingConfig: {} // No `verbose` property.
});
done();
});
done();
});

return stream;
return stream;
}
};

stream.emit('writing');
Expand Down