Skip to content

Commit 0f8d832

Browse files
jmdobrystephenplusplus
authored andcommitted
Make Speech client use autogen layer (#1631)
1 parent 48ad9d2 commit 0f8d832

2 files changed

Lines changed: 45 additions & 62 deletions

File tree

packages/speech/src/index.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var request = require('request');
3434
var streamEvents = require('stream-events');
3535
var through = require('through2');
3636
var util = require('util');
37+
var v1beta1 = require('./v1beta1');
3738

3839
/**
3940
* The [Cloud Speech API](https://cloud.google.com/speech/docs) enables easy
@@ -87,6 +88,10 @@ function Speech(options) {
8788
};
8889

8990
common.GrpcService.call(this, config, options);
91+
92+
this.api = {
93+
Speech: v1beta1(options).speechApi(options)
94+
};
9095
}
9196

9297
util.inherits(Speech, common.GrpcService);
@@ -631,11 +636,6 @@ Speech.prototype.operation = function(name) {
631636
Speech.prototype.recognize = function(file, config, callback) {
632637
var self = this;
633638

634-
var protoOpts = {
635-
service: 'Speech',
636-
method: 'syncRecognize'
637-
};
638-
639639
config = extend({}, config);
640640

641641
if (!config.encoding) {
@@ -651,21 +651,16 @@ Speech.prototype.recognize = function(file, config, callback) {
651651
return;
652652
}
653653

654-
var reqOpts = {
655-
audio: foundFile,
656-
config: config
657-
};
658-
659-
self.request(protoOpts, reqOpts, function(err, apiResponse) {
654+
self.api.Speech.syncRecognize(config, foundFile, function(err, resp) {
660655
if (err) {
661-
callback(err, null, apiResponse);
656+
callback(err, null, resp);
662657
return;
663658
}
664659

665-
var response = new self.protos.Speech.SyncRecognizeResponse(apiResponse);
660+
var response = new self.protos.Speech.SyncRecognizeResponse(resp);
666661
var results = Speech.formatResults_(response.results, verboseMode);
667662

668-
callback(null, results, apiResponse);
663+
callback(null, results, resp);
669664
});
670665
});
671666
};
@@ -773,11 +768,6 @@ Speech.prototype.recognize = function(file, config, callback) {
773768
Speech.prototype.startRecognition = function(file, config, callback) {
774769
var self = this;
775770

776-
var protoOpts = {
777-
service: 'Speech',
778-
method: 'asyncRecognize'
779-
};
780-
781771
config = extend({}, config);
782772

783773
if (!config.encoding) {
@@ -793,19 +783,14 @@ Speech.prototype.startRecognition = function(file, config, callback) {
793783
return;
794784
}
795785

796-
var reqOpts = {
797-
audio: foundFile,
798-
config: config
799-
};
800-
801-
self.request(protoOpts, reqOpts, function(err, apiResponse) {
786+
self.api.Speech.asyncRecognize(config, foundFile, function(err, resp) {
802787
if (err) {
803-
callback(err, null, apiResponse);
788+
callback(err, null, resp);
804789
return;
805790
}
806791

807-
var operation = self.operation(apiResponse.name);
808-
operation.metadata = apiResponse;
792+
var operation = self.operation(resp.name);
793+
operation.metadata = resp;
809794

810795
// Intercept the "complete" event to decode and format the results of the
811796
// operation for the user.
@@ -821,10 +806,10 @@ Speech.prototype.startRecognition = function(file, config, callback) {
821806
callback(null, Speech.formatResults_(response.results, verboseMode));
822807
});
823808

824-
callback(null, operation, apiResponse);
809+
callback(null, operation, resp);
825810
});
826811
});
827812
};
828813

829814
module.exports = Speech;
830-
module.exports.v1beta1 = require('./v1beta1');
815+
module.exports.v1beta1 = v1beta1;

packages/speech/test/index.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ describe('Speech', function() {
582582
Speech.findFile_ = function(files, callback) {
583583
callback(null, FOUND_FILE);
584584
};
585+
586+
speech.api.Speech.syncRecognize = util.noop;
585587
});
586588

587589
it('should find the files', function(done) {
@@ -594,16 +596,13 @@ describe('Speech', function() {
594596
});
595597

596598
it('should make the correct request', function(done) {
597-
speech.request = function(protoOpts, reqOpts) {
598-
assert.deepEqual(protoOpts, {
599-
service: 'Speech',
600-
method: 'syncRecognize'
599+
speech.api.Speech.syncRecognize = function(config, file) {
600+
var expectedConfig = extend({}, CONFIG, {
601+
encoding: DETECTED_ENCODING
601602
});
603+
assert.deepEqual(config, expectedConfig);
602604

603-
assert.deepEqual(reqOpts, {
604-
config: extend({}, CONFIG, { encoding: DETECTED_ENCODING }),
605-
audio: FOUND_FILE
606-
});
605+
assert.strictEqual(file, FOUND_FILE);
607606

608607
done();
609608
};
@@ -620,8 +619,8 @@ describe('Speech', function() {
620619
done(); // Will cause test to fail.
621620
};
622621

623-
speech.request = function(protoOpts, reqOpts) {
624-
assert.strictEqual(reqOpts.config.encoding, config.encoding);
622+
speech.api.Speech.syncRecognize = function(config_) {
623+
assert.strictEqual(config_.encoding, config.encoding);
625624
done();
626625
};
627626

@@ -636,8 +635,8 @@ describe('Speech', function() {
636635
return expectedEncoding;
637636
};
638637

639-
speech.request = function(protoOpts, reqOpts) {
640-
assert.strictEqual(reqOpts.config.encoding, expectedEncoding);
638+
speech.api.Speech.syncRecognize = function(config) {
639+
assert.strictEqual(config.encoding, expectedEncoding);
641640
done();
642641
};
643642

@@ -662,7 +661,7 @@ describe('Speech', function() {
662661
var apiResponse = {};
663662

664663
beforeEach(function() {
665-
speech.request = function(protoOpts, reqOpts, callback) {
664+
speech.api.Speech.syncRecognize = function(config, file, callback) {
666665
callback(error, apiResponse);
667666
};
668667
});
@@ -699,7 +698,7 @@ describe('Speech', function() {
699698
return formattedResults;
700699
};
701700

702-
speech.request = function(protoOpts, reqOpts, callback) {
701+
speech.api.Speech.syncRecognize = function(config, file, callback) {
703702
callback(null, apiResponse);
704703
};
705704
});
@@ -755,8 +754,8 @@ describe('Speech', function() {
755754
});
756755

757756
it('should delete verbose option from request object', function(done) {
758-
speech.request = function(protoOpts, reqOpts) {
759-
assert.strictEqual(reqOpts.config.verbose, undefined);
757+
speech.api.Speech.syncRecognize = function(config) {
758+
assert.strictEqual(config.verbose, undefined);
760759
done();
761760
};
762761

@@ -783,6 +782,8 @@ describe('Speech', function() {
783782
Speech.findFile_ = function(files, callback) {
784783
callback(null, FOUND_FILE);
785784
};
785+
786+
speech.api.Speech.asyncRecognize = util.noop;
786787
});
787788

788789
it('should find the files', function(done) {
@@ -795,16 +796,13 @@ describe('Speech', function() {
795796
});
796797

797798
it('should make the correct request', function(done) {
798-
speech.request = function(protoOpts, reqOpts) {
799-
assert.deepEqual(protoOpts, {
800-
service: 'Speech',
801-
method: 'asyncRecognize'
799+
speech.api.Speech.asyncRecognize = function(config, file) {
800+
var expectedConfig = extend({}, CONFIG, {
801+
encoding: DETECTED_ENCODING
802802
});
803+
assert.deepEqual(config, expectedConfig);
803804

804-
assert.deepEqual(reqOpts, {
805-
config: extend({}, CONFIG, { encoding: DETECTED_ENCODING }),
806-
audio: FOUND_FILE
807-
});
805+
assert.strictEqual(file, FOUND_FILE);
808806

809807
done();
810808
};
@@ -821,8 +819,8 @@ describe('Speech', function() {
821819
done(); // Will cause test to fail.
822820
};
823821

824-
speech.request = function(protoOpts, reqOpts) {
825-
assert.strictEqual(reqOpts.config.encoding, config.encoding);
822+
speech.api.Speech.asyncRecognize = function(config_) {
823+
assert.strictEqual(config_.encoding, config.encoding);
826824
done();
827825
};
828826

@@ -837,8 +835,8 @@ describe('Speech', function() {
837835
return expectedEncoding;
838836
};
839837

840-
speech.request = function(protoOpts, reqOpts) {
841-
assert.strictEqual(reqOpts.config.encoding, expectedEncoding);
838+
speech.api.Speech.asyncRecognize = function(config) {
839+
assert.strictEqual(config.encoding, expectedEncoding);
842840
done();
843841
};
844842

@@ -863,7 +861,7 @@ describe('Speech', function() {
863861
var apiResponse = {};
864862

865863
beforeEach(function() {
866-
speech.request = function(protoOpts, reqOpts, callback) {
864+
speech.api.Speech.asyncRecognize = function(config, file, callback) {
867865
callback(error, apiResponse);
868866
};
869867
});
@@ -907,7 +905,7 @@ describe('Speech', function() {
907905
return through.obj();
908906
};
909907

910-
speech.request = function(protoOpts, reqOpts, callback) {
908+
speech.api.Speech.asyncRecognize = function(config, file, callback) {
911909
callback(null, apiResponse);
912910
};
913911
});
@@ -985,8 +983,8 @@ describe('Speech', function() {
985983
});
986984

987985
it('should delete verbose option from request object', function(done) {
988-
speech.request = function(protoOpts, reqOpts) {
989-
assert.strictEqual(reqOpts.config.verbose, undefined);
986+
speech.api.Speech.asyncRecognize = function(config) {
987+
assert.strictEqual(config.verbose, undefined);
990988
done();
991989
};
992990

0 commit comments

Comments
 (0)