Skip to content

Commit 3156f37

Browse files
authored
Merge pull request #377 from watson-developer-cloud/error-stream
Returns a node stream if callback is null
2 parents d3e268e + 2001136 commit 3156f37

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

lib/requestwrapper.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var request = require('request');
2121
var pkg = require('../package.json');
2222
var helper = require('./helper');
2323
var parseString = require('string');
24-
24+
var readableStream = require('stream').PassThrough;
2525
var isBrowser = typeof window === "object";
2626

2727
function parsePath(path, params) {
@@ -102,7 +102,7 @@ function formatErrorIfExists(cb) {
102102
* @private
103103
* @return {ReadableStream|undefined}
104104
*/
105-
function createRequest(parameters, callback) {
105+
function createRequest(parameters, _callback) {
106106
var missingParams = null,
107107
options = extend(true, {}, parameters.defaultOptions, parameters.options),
108108
path = options.path,
@@ -112,7 +112,7 @@ function createRequest(parameters, callback) {
112112
qs = options.qs; // Query parameters
113113

114114
// Provide a default callback if it doesn't exists
115-
callback = typeof callback === 'function' ? callback : function() { /* no op */};
115+
var callback = typeof _callback === 'function' ? _callback : function() { /* no op */};
116116

117117
// Missing parameters
118118
if (parameters.options.requiredParams) {
@@ -124,8 +124,15 @@ function createRequest(parameters, callback) {
124124
parameters.requiredParams);
125125

126126
if (missingParams) {
127-
callback(missingParams);
128-
return;
127+
if (typeof _callback === 'function') {
128+
return callback(missingParams);
129+
} else {
130+
var errorStream = readableStream();
131+
setTimeout(function() {
132+
errorStream.emit('error', missingParams);
133+
}, 0);
134+
return errorStream;
135+
}
129136
}
130137

131138
// Path params

speech-to-text/v1.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,12 @@ SpeechToTextV1.prototype.observeResult = function(params, callback) {
237237
* @deprecated use createRecognizeStream instead
238238
*/
239239
SpeechToTextV1.prototype.getRecognizeStatus = function(params, callback) {
240-
var missingParams = helper.getMissingParams(params, ['session_id']);
241-
if (missingParams) {
242-
callback(missingParams);
243-
return;
244-
}
245-
246-
var path = params || {};
247240
var parameters = {
241+
requiredParams: ['session_id'],
248242
options: {
249243
method: 'GET',
250-
url: '/v1/sessions/' + path.session_id + '/recognize',
251-
path: path,
244+
url: '/v1/sessions/{session_id}/recognize',
245+
path: pick(params, ['session_id']),
252246
json: true
253247
},
254248
defaultOptions: this._options
@@ -285,16 +279,14 @@ SpeechToTextV1.prototype.getModels = function(params, callback) {
285279
* @returns {ReadableStream|undefined}
286280
*/
287281
SpeechToTextV1.prototype.getModel = function(params, callback) {
288-
var path = params || {};
289-
290282
var parameters = {
283+
requiredParams: ['model_id'],
291284
options: {
292285
method: 'GET',
293-
url: '/v1/models/' + path.model_id,
294-
path: path,
286+
url: '/v1/models/{model_id}',
287+
path: pick(params, ['model_id']),
295288
json: true
296289
},
297-
requiredParams: ['model_id'],
298290
defaultOptions: this._options
299291
};
300292
return requestFactory(parameters, callback);
@@ -343,17 +335,13 @@ SpeechToTextV1.prototype.createSession = function(params, callback) {
343335
* @param {String} params.session_id - Session id.
344336
*/
345337
SpeechToTextV1.prototype.deleteSession = function(params, callback) {
346-
var missingParams = helper.getMissingParams(params, ['session_id']);
347-
if (missingParams) {
348-
callback(missingParams);
349-
return;
350-
}
351-
352338
var parameters = {
339+
requiredParams: ['session_id'],
353340
options: {
354341
method: 'DELETE',
355-
url: '/v1/sessions/' + params.session_id,
356-
json: true
342+
url: '/v1/sessions/{session_id}',
343+
json: true,
344+
path: pick(params, ['session_id']),
357345
},
358346
defaultOptions: this._options
359347
};
@@ -411,6 +399,7 @@ SpeechToTextV1.prototype.createRecognizeStream = function(params) {
411399
*/
412400
SpeechToTextV1.prototype.createCustomization = function(params, callback) {
413401
var parameters = {
402+
requiredParams: ['base_model_name', 'name'],
414403
options: {
415404
method: 'POST',
416405
url: '/v1/customizations',

test/unit/test.wrapper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var assert = require('assert');
44
var watson = require('../../index');
55
var helper = require('../../lib/helper');
6+
var fs = require('fs');
67

78
describe('wrapper', function() {
89

@@ -87,6 +88,20 @@ describe('wrapper', function() {
8788
version: 'v1'
8889
}));
8990
});
91+
it('should return an stream if callback is null and there is an error', function(done) {
92+
var textToSpeech = watson.text_to_speech({
93+
username: 'a',
94+
password: 'b',
95+
version: 'v1',
96+
});
97+
98+
textToSpeech.synthesize({ voice: '', accept: '' })
99+
.on('error', function(error) {
100+
assert.equal('Error: Missing required parameters: text', error);
101+
done();
102+
})
103+
.pipe(fs.createWriteStream('../resources/tts-output.ogg'));
104+
});
90105

91106
describe('env', function() {
92107
var env;

text-to-speech/v1.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ TextToSpeechV1.URL = 'https://stream.watsonplatform.net/text-to-speech/api';
4949
*/
5050
TextToSpeechV1.prototype.synthesize = function(params, callback) {
5151
params = extend({accept:'audio/ogg; codecs=opus'}, params);
52-
if (!params.text){
53-
callback(new Error('Missing required parameters: text'));
54-
return;
55-
}
5652

5753
var parameters = {
54+
requiredParams: ['text'],
5855
options: {
5956
method: 'POST',
6057
url: '/v1/synthesize',
6158
body: JSON.stringify(pick(params, ['text'])),
6259
qs: pick(params, ['accept', 'voice', 'customization_id']),
60+
path: pick(params, ['text']),
6361
headers: extend({
6462
'content-type': 'application/json'
6563
}, pick(params, ['X-Watson-Learning-Opt-Out'])),

0 commit comments

Comments
 (0)