Skip to content

Commit 3655db6

Browse files
committed
Merge pull request #50 from arlemi/master
Handling of the credentials so it can be used outside of Bluemix
2 parents abb5f7a + 9855ea9 commit 3655db6

2 files changed

Lines changed: 126 additions & 131 deletions

File tree

services/language_translation/v2.html

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<i class="fa fa-question-circle"></i><b> Please wait: </b> Checking for bound service credentials...
2121
</div>
2222
</div>
23+
<div id="credentials-not-found" class="form-row">
24+
<div class="form-tips">
25+
<i class="fa fa-question-circle"></i><b> Could not bind to service. </b> Try other credentials?
26+
</div>
27+
</div>
2328
<div class="form-row">
2429
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
2530
<input type="text" id="node-input-name" placeholder="Name">
@@ -118,6 +123,7 @@
118123
var base_model_selected = $('#node-input-basemodel').val();
119124

120125
(function () {
126+
121127
// sorting functions
122128
function onlyUnique(value, index, self) {
123129
return self.indexOf(value) === index;
@@ -310,7 +316,6 @@
310316
available_destlang.forEach(function (val) {
311317
$('#node-input-destlang option[value=' + val.split('-')[1] + ']').removeAttr('disabled')
312318
});
313-
314319
checkPairLang();
315320
}
316321

@@ -321,35 +326,41 @@
321326
}
322327

323328
function getModels() {
324-
$.getJSON('watson-translate/models/').done(function (data) {
329+
var username = $('input#node-input-username').val();
330+
var password = $('input#node-input-password').val();
331+
$.getJSON('watson-translate/models/', {un: username,pwd: password}).done(function (data) {
325332
models = data.models;
326-
handlersUI();
327333
selectAction();
328334
}).fail(function (err) {
329335
console.log(err);
330-
}).always(function (osef) {});
336+
$('#credentials-not-found').show();
337+
}).always(function () {});
331338
}
332339

333340
function oneditprepare() {
341+
console.log("zfsqdfqsg");
342+
console.log(models);
334343
var node = this;
335-
344+
handlersUI();
336345
$.getJSON('watson-translate/vcap/')
337346
.done(function (sids) {
338347
$('.credentials').toggle(!sids);
348+
if (first_launch || !models) {
349+
$('#credentials-not-found').hide();
350+
getModels();
351+
first_launch = false;
352+
} else if (models) {
353+
$('#credentials-not-found').hide();
354+
selectAction();
355+
} else {
356+
$('#credentials-not-found').show();
357+
}
339358
})
340359
.fail(function () {
341360
$('.credentials').show();
342361
}).always(function () {
343362
$('#credentials-check').hide();
344363
})
345-
346-
if (first_launch || !models) {
347-
getModels();
348-
first_launch = false;
349-
} else {
350-
handlersUI();
351-
selectAction();
352-
}
353364
}
354365

355366
RED.nodes.registerType('watson-translate', {

services/language_translation/v2.js

Lines changed: 102 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ module.exports = function (RED) {
3737
});
3838

3939
RED.httpAdmin.get('/watson-translate/models', function (req, res) {
40-
language_translation = watson.language_translation({
41-
username: username,
42-
password: password,
43-
version: 'v2'
44-
});
40+
if(!username && !password) {
41+
language_translation = watson.language_translation({
42+
username: req.query.un,
43+
password: req.query.pwd,
44+
version: 'v2'
45+
});
46+
} else {
47+
language_translation = watson.language_translation({
48+
username: username,
49+
password: password,
50+
version: 'v2'
51+
});
52+
}
4553
language_translation.getModels({}, function (err, models) {
4654
if (err) {
4755
res.json(err);
@@ -72,13 +80,101 @@ module.exports = function (RED) {
7280
RED.nodes.createNode(this, config);
7381
var node = this;
7482

75-
this.doTranslate = function (msg, model_id) {
83+
this.on('input', function (msg) {
84+
var message = '';
85+
86+
if (!msg.payload) {
87+
message = 'Missing property: msg.payload';
88+
node.error(message, msg)
89+
return;
90+
}
91+
92+
var srclang = msg.srclang || config.srclang;
93+
94+
if (!srclang) {
95+
node.warn('Missing source language, message not translated');
96+
node.send(msg);
97+
return;
98+
}
99+
100+
var destlang = msg.destlang || config.destlang;
101+
102+
if (!destlang) {
103+
node.warn('Missing target language, message not translated');
104+
node.send(msg);
105+
return;
106+
}
107+
108+
var domain = msg.domain || config.domain;
109+
110+
if (!domain) {
111+
node.warn('Missing translation domain, message not translated');
112+
node.send(msg);
113+
return;
114+
}
115+
116+
var action = msg.action || config.action;
117+
118+
if (!action) {
119+
node.warn('Missing action, please select one');
120+
node.send(msg);
121+
return;
122+
}
123+
124+
var filetype = msg.filetype || config.filetype;
125+
126+
if (!filetype) {
127+
node.warn('Missing file type, please select one');
128+
node.send(msg);
129+
return;
130+
}
131+
132+
var trainid = msg.trainid || config.trainid;
133+
var basemodel = msg.basemodel || config.basemodel;
134+
135+
username = username || this.credentials.username;
136+
password = password || this.credentials.password;
137+
138+
if (!username || !password) {
139+
this.status({
140+
fill: 'red',
141+
shape: 'ring',
142+
text: 'missing credentials'
143+
});
144+
message = 'Missing Language Translation service credentials';
145+
node.error(message, msg)
146+
return;
147+
}
148+
76149
language_translation = watson.language_translation({
77150
username: username,
78151
password: password,
79152
version: 'v2'
80153
});
81154

155+
var model_id = "";
156+
if(domain === "news") {
157+
model_id = srclang + '-' + destlang;
158+
} else {
159+
model_id = srclang + '-' + destlang + '-' + domain;
160+
}
161+
switch (action) {
162+
case 'translate':
163+
this.doTranslate(msg, model_id);
164+
break;
165+
case 'train':
166+
this.doTrain(msg, basemodel, filetype);
167+
break;
168+
case 'getstatus':
169+
this.doGetStatus(msg, trainid);
170+
break;
171+
case 'delete':
172+
this.doDelete(msg, trainid);
173+
break;
174+
}
175+
});
176+
177+
this.doTranslate = function (msg, model_id) {
82178
node.status({
83179
fill: 'blue',
84180
shape: 'dot',
@@ -100,12 +196,6 @@ module.exports = function (RED) {
100196
};
101197

102198
this.doTrain = function (msg, basemodel, filetype) {
103-
language_translation = watson.language_translation({
104-
username: username,
105-
password: password,
106-
version: 'v2'
107-
});
108-
109199
node.status({
110200
fill: 'blue',
111201
shape: 'dot',
@@ -168,12 +258,6 @@ module.exports = function (RED) {
168258
}
169259

170260
this.doGetStatus = function(msg, trainid) {
171-
language_translation = watson.language_translation({
172-
username: username,
173-
password: password,
174-
version: 'v2'
175-
});
176-
177261
node.status({
178262
fill: 'blue',
179263
shape: 'dot',
@@ -200,12 +284,6 @@ module.exports = function (RED) {
200284
}
201285

202286
this.doDelete = function(msg, trainid) {
203-
language_translation = watson.language_translation({
204-
username: username,
205-
password: password,
206-
version: 'v2'
207-
});
208-
209287
node.status({
210288
fill: 'blue',
211289
shape: 'dot',
@@ -230,100 +308,6 @@ module.exports = function (RED) {
230308
}
231309
);
232310
}
233-
234-
this.on('input', function (msg) {
235-
var message = '';
236-
237-
if (!msg.payload) {
238-
message = 'Missing property: msg.payload';
239-
node.error(message, msg)
240-
return;
241-
}
242-
243-
var srclang = msg.srclang || config.srclang;
244-
245-
if (!srclang) {
246-
node.warn('Missing source language, message not translated');
247-
node.send(msg);
248-
return;
249-
}
250-
251-
var destlang = msg.destlang || config.destlang;
252-
253-
if (!destlang) {
254-
node.warn('Missing target language, message not translated');
255-
node.send(msg);
256-
return;
257-
}
258-
259-
var domain = msg.domain || config.domain;
260-
261-
if (!domain) {
262-
node.warn('Missing translation domain, message not translated');
263-
node.send(msg);
264-
return;
265-
}
266-
267-
var action = msg.action || config.action;
268-
269-
if (!action) {
270-
node.warn('Missing action, please select one');
271-
node.send(msg);
272-
return;
273-
}
274-
275-
var filetype = msg.filetype || config.filetype;
276-
277-
if (!filetype) {
278-
node.warn('Missing file type, please select one');
279-
node.send(msg);
280-
return;
281-
}
282-
283-
var trainid = msg.trainid || config.trainid;
284-
var basemodel = msg.basemodel || config.basemodel;
285-
286-
username = username || this.credentials.username;
287-
password = password || this.credentials.password;
288-
289-
if (!username || !password) {
290-
this.status({
291-
fill: 'red',
292-
shape: 'ring',
293-
text: 'missing credentials'
294-
});
295-
message = 'Missing Language Translation service credentials';
296-
node.error(message, msg)
297-
return;
298-
}
299-
300-
language_translation = watson.language_translation({
301-
username: username,
302-
password: password,
303-
version: 'v2'
304-
});
305-
306-
var model_id = "";
307-
if(domain === "news") {
308-
model_id = srclang + '-' + destlang;
309-
} else {
310-
model_id = srclang + '-' + destlang + '-' + domain;
311-
}
312-
switch (action) {
313-
case 'translate':
314-
this.doTranslate(msg, model_id);
315-
break;
316-
case 'train':
317-
this.doTrain(msg, basemodel, filetype);
318-
break;
319-
case 'getstatus':
320-
this.doGetStatus(msg, trainid);
321-
break;
322-
case 'delete':
323-
this.doDelete(msg, trainid);
324-
break;
325-
}
326-
});
327311
}
328312

329313
RED.nodes.registerType('watson-translate', SMTNode, {

0 commit comments

Comments
 (0)