Skip to content

Commit 36baf36

Browse files
committed
migrate to ibm-watson dependancy
1 parent f489574 commit 36baf36

8 files changed

Lines changed: 62 additions & 52 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ During the migration there will be a dependancy on both modules.
2020
- Node-RED & IBM-Watson & Use of promises on API invokation & IAM URL construct migration & Removal of default endpoint of
2121
- Tone Analyzer node.
2222
- Personality Insights node.
23-
- Visual Recognition V3
23+
- Visual Recognition V3 node
24+
- Text to Speech node
25+
- Text to Speech Corpus Builder node.
2426
- New Visual Recognition V4 node.
2527
- Drop faces detect option from Visual Recognition V3 node.
2628
- Fix to URL parsing for bound services.

services/personality_insights/v3.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
outputlang: {value: "en"},
125125
rawscores: {value: false},
126126
consumption: {value: false},
127-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/personality-insights/api'}
127+
'service-endpoint' :{value: ""}
128128
},
129129
credentials: {
130130
username: {type:"text"},

services/text_to_speech/tts-utils.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515
**/
1616

1717
const pkg = require('../../package.json'),
18-
TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
18+
TextToSpeechV1 = require('ibm-watson/text-to-speech/v1'),
19+
{ IamAuthenticator } = require('ibm-watson/auth');
1920

2021
class TTSUtils {
2122
constructor() {
2223
}
2324

2425
static buildStdSettings (apikey, username, password, endpoint) {
26+
let authSettings = {};
2527
let serviceSettings = {
2628
headers: {
2729
'User-Agent': pkg.name + '-' + pkg.version
2830
}
2931
};
3032

3133
if (apikey) {
32-
serviceSettings.iam_apikey = apikey;
34+
authSettings.apikey = apikey;
3335
} else {
34-
serviceSettings.username = username;
35-
serviceSettings.password = password;
36+
authSettings.username = username;
37+
authSettings.password = password;
3638
}
3739

40+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
41+
3842
if (endpoint) {
3943
serviceSettings.url = endpoint;
4044
}
@@ -45,23 +49,25 @@ class TTSUtils {
4549
static initTTSService(req, sApikey, sUsername, sPassword, sEndpoint) {
4650
const endpoint = req.query.e ? req.query.e : sEndpoint;
4751

52+
let authSettings = {};
4853
let serviceSettings = {
4954
url: endpoint,
5055
headers: {
5156
'User-Agent': pkg.name + '-' + pkg.version
5257
}};
5358

5459
if (sApikey || req.query.key) {
55-
serviceSettings.iam_apikey = sApikey ? sApikey : req.query.key;
60+
authSettings.apikey = sApikey ? sApikey : req.query.key;
5661
} else {
57-
serviceSettings.username = sUsername ? sUsername : req.query.un;
58-
serviceSettings.password = sPassword ? sPassword : req.query.pwd;
62+
authSettings.username = sUsername ? sUsername : req.query.un;
63+
authSettings.password = sPassword ? sPassword : req.query.pwd;
5964
}
6065

66+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
67+
6168
return new TextToSpeechV1(serviceSettings);
6269
}
6370

64-
6571
}
6672

6773
module.exports = TTSUtils;

services/text_to_speech/v1.html

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
</div>
4545

4646
<div class="form-row credentials">
47-
<label>&nbsp;</label>
48-
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
49-
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
50-
</div>
51-
<div class="form-row">
5247
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
5348
<input type="text" id="node-input-service-endpoint" placeholder="https://stream.watsonplatform.net/text-to-speech/api">
5449
</div>
@@ -247,9 +242,8 @@
247242
var e = $('#node-input-service-endpoint').val();
248243
var creds = {un: u, pwd: p, key: k};
249244

250-
if (! $('#node-input-default-endpoint').prop('checked')) {
251-
creds.e = e;
252-
}
245+
creds.e = e;
246+
253247
$.getJSON('watson-text-to-speech/customs', creds)
254248
.done(function (data) {
255249
$('label#node-label-message').parent().hide();
@@ -293,9 +287,7 @@
293287
var e = $('#node-input-service-endpoint').val();
294288
var creds = {un: u, pwd: p, key: k};
295289

296-
if (! $('#node-input-default-endpoint').prop('checked')) {
297-
creds.e = e;
298-
}
290+
creds.e = e;
299291

300292
$.getJSON('watson-text-to-speech/voices/', creds)
301293
.done(function (data) {
@@ -435,14 +427,6 @@
435427
$('#node-input-langcustom').change(function (val) {
436428
tts.custom_selected = $('#node-input-langcustom').val();
437429
});
438-
$('#node-input-default-endpoint').change(function () {
439-
var checked = $('#node-input-default-endpoint').prop('checked')
440-
if (checked) {
441-
$('#node-input-service-endpoint').parent().hide();
442-
} else {
443-
$('#node-input-service-endpoint').parent().show();
444-
}
445-
});
446430
}
447431

448432
// Function to be used at the start, as don't want to expose any fields, unless the models are
@@ -499,8 +483,7 @@
499483
password: {value: ''},
500484
apikey: {value: ''},
501485
'payload-response' :{value: false},
502-
'default-endpoint' :{value: true},
503-
'service-endpoint' :{value: 'https://stream.watsonplatform.net/text-to-speech/api'}
486+
'service-endpoint' :{value: ""}
504487
},
505488
credentials: {
506489
username: {type:"text"},

services/text_to_speech/v1.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
module.exports = function(RED) {
1818
const SERVICE_IDENTIFIER = 'text-to-speech';
19+
1920
var pkg = require('../../package.json'),
20-
TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1'),
2121
serviceutils = require('../../utilities/service-utils'),
2222
payloadutils = require('../../utilities/payload-utils'),
2323
ttsutils = require('./tts-utils'),
@@ -113,35 +113,52 @@ module.exports = function(RED) {
113113

114114
function performTTS(msg, params) {
115115
var p = new Promise(function resolver(resolve, reject) {
116-
var tts = ttsutils.buildStdSettings(apikey, username, password, endpoint);
116+
let tts = ttsutils.buildStdSettings(apikey, username, password, endpoint);
117117

118-
tts.synthesize(params, function (err, body, response) {
119-
if (err) {
120-
reject(err);
121-
} else {
118+
tts.synthesize(params)
119+
.then((body) => {
122120
resolve(body);
123-
}
124-
});
121+
})
122+
.catch((err) => {
123+
reject(err);
124+
});
125+
125126
});
126127
return p;
127128
}
128129

129-
function processResponse(msg, body) {
130-
msg.speech = body;
131-
if (config['payload-response']) {
130+
function processResponse(msg, data) {
131+
return new Promise(function resolver(resolve, reject) {
132+
let body = data
133+
if (data && data.result) {
134+
body = data.result;
135+
}
136+
137+
let tmpHolder = msg.payload;
132138
msg.payload = body;
133-
}
134-
return Promise.resolve();
139+
140+
payloadutils.checkForStream(msg)
141+
.then(() => {
142+
if (! config['payload-response']) {
143+
msg.speech = msg.payload;
144+
msg.payload = tmpHolder;
145+
}
146+
resolve();
147+
})
148+
.catch((err) => {
149+
reject(err);
150+
});
151+
});
135152
}
136153

137-
this.on('input', function(msg) {
154+
this.on('input', function(msg, send, done) {
138155

139156
username = sUsername || this.credentials.username;
140157
password = sPassword || this.credentials.password || config.password;
141158
apikey = sApikey || this.credentials.apikey || config.apikey;
142159

143160
endpoint = sEndpoint;
144-
if ((!config['default-endpoint']) && config['service-endpoint']) {
161+
if (config['service-endpoint']) {
145162
endpoint = config['service-endpoint'];
146163
}
147164

@@ -163,10 +180,12 @@ module.exports = function(RED) {
163180
})
164181
.then(function(){
165182
node.status({});
166-
node.send(msg);
183+
send(msg);
184+
done();
167185
})
168186
.catch(function(err){
169187
payloadutils.reportError(node,msg,err);
188+
done(err);
170189
});
171190
})
172191
}

services/tone_analyzer/v3.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
"interface-version": {value:"generalTone"},
222222
name: {value: ""},
223223
inputlang: {value: "en"},
224-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/tone-analyzer/api'}
224+
'service-endpoint' :{value: ""}
225225
},
226226
credentials: {
227227
username: {type:'text'},

services/visual_recognition/v3.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
category: 'IBM Watson',
144144
defaults: {
145145
name: {value: ""},
146-
'vr-service-endpoint' :{value: 'https://gateway.watsonplatform.net/visual-recognition/api'},
146+
'vr-service-endpoint' :{value: ""},
147147
"image-feature": {value: ""},
148148
"lang": {value: ""},
149149
},
@@ -211,7 +211,7 @@
211211
category: 'IBM Watson',
212212
defaults: {
213213
name: {value: ""},
214-
'vr-service-endpoint' :{value: 'https://gateway.watsonplatform.net/visual-recognition/api'},
214+
'vr-service-endpoint' :{value: ""},
215215
"image-feature": {value: ""}
216216
},
217217
credentials: {

services/visual_recognition/v4-collection-utils.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
category: 'IBM Watson',
8282
defaults: {
8383
name: {value: ""},
84-
'vr-service-endpoint' :{value: 'https://gateway.watsonplatform.net/visual-recognition/api'},
84+
'vr-service-endpoint' :{value: ""},
8585
"image-feature": {value: ""}
8686
},
8787
credentials: {
@@ -161,7 +161,7 @@
161161
<ul>
162162
<li><code>msg.payload</code> : Either a zip of image files or an array of URLs of image files (Required)</li>
163163
<li><code>msg.params["collectionId"]</code> : The identifier of the collection (Required)</li>
164-
<li><code>msg.params["threshold"]</code> : Minimum score for matches (Optional)</li>
164+
<li><code>msg.params["threshold"]</code> : Minimum score for matches (Optional)</li>
165165
</ul>
166166
<p>More information on this
167167
<a href="https://cloud.ibm.com/apidocs/visual-recognition/visual-recognition-v4?code=node#add-images">

0 commit comments

Comments
 (0)