Skip to content
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

Make Speech client use autogen layer #1631

Merged
merged 3 commits into from
Sep 28, 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
45 changes: 15 additions & 30 deletions packages/speech/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +88,10 @@ function Speech(options) {
};

common.GrpcService.call(this, config, options);

this.api = {

This comment was marked as spam.

This comment was marked as spam.

Speech: v1beta1(options).speechApi(options)
};
}

util.inherits(Speech, common.GrpcService);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});
});
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand All @@ -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;
62 changes: 30 additions & 32 deletions packages/speech/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
};
Expand All @@ -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();
};

Expand All @@ -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();
};

Expand All @@ -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);
};
});
Expand Down Expand Up @@ -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);
};
});
Expand Down Expand Up @@ -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();
};

Expand All @@ -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) {
Expand All @@ -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();
};
Expand All @@ -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();
};

Expand All @@ -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();
};

Expand All @@ -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);
};
});
Expand Down Expand Up @@ -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);
};
});
Expand Down Expand Up @@ -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();
};

Expand Down