Skip to content

Commit 5f98762

Browse files
committed
migrate to ibm-watson dependancy
1 parent a4e5893 commit 5f98762

File tree

4 files changed

+99
-45
lines changed

4 files changed

+99
-45
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ Node-RED Watson Nodes for IBM Cloud
88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

1010
### New in version 0.8.1
11-
- Node-RED & IBM-Watson & Use of promises on API invokation & IAM URL construct migration & Removal of default endpoint of
11+
- Node-RED & IBM-Watson & Use of promises on API invocation & IAM URL construct migration & Removal of default endpoint of
12+
- Document Translator node
13+
14+
15+
### New in version 0.8.1
16+
- Node-RED & IBM-Watson & Use of promises on API invocation & IAM URL construct migration & Removal of default endpoint of
1217
- Speech to Text node
1318
- Speech to Text Corpus Builder node
1419
- Natural Language Understanding node
@@ -33,7 +38,7 @@ During the migration there will be a dependancy on both modules.
3338
- Bump dependancy on node to >=10.0.0
3439
- Bump dependancy on cfenv, request, file-type
3540
- Bump dependancy on ibm-cloud-sdk-core to 0.3.7 (need to stay on 0.x, for STT Streaming to work)
36-
- Node-RED & IBM-Watson & Use of promises on API invokation & IAM URL construct migration & Removal of default endpoint of
41+
- Node-RED & IBM-Watson & Use of promises on API invocation & IAM URL construct migration & Removal of default endpoint of
3742
- Tone Analyzer node.
3843
- Personality Insights node.
3944
- Visual Recognition V3 node

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"async": "^1.5.2",

services/language_translator/v3-doc.html

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
</div>
4848

4949
<div class="form-row credentials">
50-
<label>&nbsp;</label>
51-
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
52-
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
53-
</div>
54-
<div class="form-row">
5550
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
5651
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/language-translator/api">
5752
</div>
@@ -335,15 +330,6 @@
335330
doctor.checkActionSelected();
336331
});
337332

338-
$('#node-input-default-endpoint').change(() => {
339-
var checked = $('#node-input-default-endpoint').prop('checked')
340-
if (checked) {
341-
$('#node-input-service-endpoint').parent().hide();
342-
} else {
343-
$('#node-input-service-endpoint').parent().show();
344-
}
345-
});
346-
347333
$('#node-input-srclang').change(function () {
348334
doctor.srclang_selected = $('#node-input-srclang').val();
349335
if (doctor.models) {
@@ -400,9 +386,7 @@
400386
var e = $('#node-input-service-endpoint').val();
401387
var creds = {un: u, pwd: p, key: k};
402388

403-
if (! $('#node-input-default-endpoint').prop('checked')) {
404-
creds.e = e;
405-
}
389+
creds.e = e;
406390

407391
$.getJSON('watson-doc-translator/models/', creds)
408392
.done(function (data) {
@@ -541,8 +525,7 @@
541525
destlanghidden: {value: ''},
542526
filename: {value: ''},
543527
'document-id': {value: ''},
544-
'default-endpoint' :{value: true},
545-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/language-translator/api'}
528+
'service-endpoint' :{value: ''}
546529
},
547530
credentials: {
548531
username: {type: 'text'}

services/language_translator/v3-doc.js

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
module.exports = function (RED) {
1818
const request = require('request'),
1919
SERVICE_IDENTIFIER = 'language-translator',
20-
SERVICE_VERSION = '2018-05-01';
20+
SERVICE_VERSION = '2018-05-01',
21+
LanguageTranslatorV3 = require('ibm-watson/language-translator/v3'),
22+
{ IamAuthenticator } = require('ibm-watson/auth');
2123

2224
var pkg = require('../../package.json'),
23-
LanguageTranslatorV3 = require('watson-developer-cloud/language-translator/v3'),
2425
fs = require('fs'),
2526
fileType = require('file-type'),
2627
temp = require('temp'),
@@ -54,6 +55,7 @@ module.exports = function (RED) {
5455
RED.httpAdmin.get('/watson-doc-translator/models', function (req, res) {
5556
endpoint = req.query.e ? req.query.e : sEndpoint;
5657
var lt = null,
58+
authSettings = {},
5759
serviceSettings = {
5860
version: SERVICE_VERSION,
5961
url: endpoint,
@@ -63,24 +65,28 @@ module.exports = function (RED) {
6365
};
6466

6567
if (sApikey || req.query.key) {
66-
serviceSettings.iam_apikey = sApikey ? sApikey : req.query.key;
68+
authSettings.apikey = sApikey ? sApikey : req.query.key;
6769
} else {
68-
serviceSettings.username = sUsername ? sUsername : req.query.un;
69-
serviceSettings.password = sPassword ? sPassword : req.query.pwd;
70+
authSettings.username = sUsername ? sUsername : req.query.un;
71+
authSettings.password = sPassword ? sPassword : req.query.pwd;
7072
}
73+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
7174

7275
lt = new LanguageTranslatorV3(serviceSettings);
7376

74-
lt.listModels({}, function (err, models) {
75-
if (err) {
76-
res.json(err);
77-
} else {
77+
lt.listModels({})
78+
.then((response) => {
79+
let models = [];
80+
if (response && response.result && response.result.models) {
81+
models = response.result;
82+
}
7883
res.json(models);
79-
}
80-
});
84+
})
85+
.catch((err) => {
86+
res.json(err);
87+
})
8188
});
8289

83-
8490
function Node (config) {
8591
var node = this;
8692
RED.nodes.createNode(this, config);
@@ -144,6 +150,30 @@ module.exports = function (RED) {
144150
return Promise.resolve();
145151
}
146152

153+
function getService() {
154+
let authSettings = {},
155+
serviceSettings = {
156+
version: '2018-05-01',
157+
headers: {
158+
'User-Agent': pkg.name + '-' + pkg.version
159+
}
160+
};
161+
162+
if (apikey) {
163+
authSettings.apikey = apikey;
164+
} else {
165+
authSettings.username = username;
166+
authSettings.password = password;
167+
}
168+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
169+
170+
if (endpoint) {
171+
serviceSettings.url = endpoint;
172+
}
173+
174+
return new LanguageTranslatorV3(serviceSettings);
175+
}
176+
147177

148178
function buildAuthSettings () {
149179
var authSettings = {};
@@ -186,7 +216,6 @@ module.exports = function (RED) {
186216
reject('Document not found ' + response.statusCode);
187217
break;
188218
default:
189-
console.log(body);
190219
reject('Error Invoking API ' + response.statusCode);
191220
break;
192221
}
@@ -324,10 +353,46 @@ module.exports = function (RED) {
324353
}
325354

326355
function executeGetDocument(msg) {
327-
var docid = docID(msg);
328-
let uriAddress = `${endpoint}/v3/documents/${docid}/translated_document?version=${SERVICE_VERSION}`;
356+
return new Promise(function resolver(resolve, reject){
357+
let lt = getService(),
358+
docid = docID(msg);
359+
360+
lt.getTranslatedDocument({documentId : docid})
361+
.then((response) => {
362+
msg.payload = response;
363+
if (response && response.result) {
364+
msg.payload = response.result;
365+
}
366+
return payloadutils.checkForStream(msg);
367+
})
368+
.then(() => {
369+
resolve(msg.payload);
370+
})
371+
.catch((err) => {
372+
reject(err);
373+
});
374+
});
375+
}
329376

330-
return executeGetRequest(uriAddress);
377+
function executeGetDocumentXX(msg) {
378+
return new Promise(function resolver(resolve, reject){
379+
var docid = docID(msg);
380+
let uriAddress = `${endpoint}/v3/documents/${docid}/translated_document?version=${SERVICE_VERSION}`;
381+
382+
executeGetRequest(uriAddress)
383+
.then((response) => {
384+
msg.payload = response;
385+
return payloadutils.checkForStream(msg);
386+
})
387+
.then(() => {
388+
resolve(msg.payload);
389+
})
390+
.catch((err) => {
391+
reject(err);
392+
});
393+
});
394+
395+
// return executeGetRequest(uriAddress);
331396
}
332397

333398
function executeDeleteDocument(msg) {
@@ -416,7 +481,7 @@ module.exports = function (RED) {
416481
return Promise.resolve();
417482
}
418483

419-
function doit(msg) {
484+
function doit(msg, send) {
420485
let action = msg.action || config.action;
421486

422487
translatorutils.credentialCheck(username, password, apikey)
@@ -440,16 +505,16 @@ module.exports = function (RED) {
440505
.then(function(){
441506
temp.cleanup();
442507
node.status({});
443-
node.send(msg);
508+
send(msg);
444509
})
445510
.catch(function(err){
446511
temp.cleanup();
447512
payloadutils.reportError(node, msg, err);
448-
node.send(msg);
513+
send(msg);
449514
});
450515
}
451516

452-
this.on('input', function (msg) {
517+
this.on('input', function(msg, send, done) {
453518
// The dynamic nature of this node has caused problems with the password field. it is
454519
// hidden but not a credential. If it is treated as a credential, it gets lost when there
455520
// is a request to refresh the model list.
@@ -459,7 +524,7 @@ module.exports = function (RED) {
459524
apikey = sApikey || this.credentials.apikey || config.apikey;
460525

461526
endpoint = sEndpoint;
462-
if ((!config['default-endpoint']) && config['service-endpoint']) {
527+
if (config['service-endpoint']) {
463528
endpoint = config['service-endpoint'];
464529
}
465530

@@ -476,12 +541,13 @@ module.exports = function (RED) {
476541
pos = i+1;
477542
node.status({ fill: 'blue', shape: 'dot', text: `Processing document ${pos} of ${len}` });
478543
msgClone.payload = e;
479-
doit(msgClone);
544+
doit(msgClone, send);
480545
});
481546

482547
} else {
483-
doit(msg);
548+
doit(msg, send);
484549
}
550+
done();
485551
});
486552
}
487553

0 commit comments

Comments
 (0)