Skip to content

Commit 5a0bb20

Browse files
authored
Merge pull request #139 from watson-developer-cloud/master
Rebasing
2 parents 6863ae2 + f3b5439 commit 5a0bb20

File tree

5 files changed

+104
-29
lines changed

5 files changed

+104
-29
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Node-RED Watson Nodes for IBM Cloud
77

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

10+
### New in version 0.6.5
11+
- Endpoint can now be specified in Natural Language Classifier Node
12+
1013
### New in version 0.6.4
1114
- Speech to Text node now reports all errors, including disconnects when running in stream mode.
1215

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"async": "^1.5.2",
@@ -10,7 +10,7 @@
1010
"temp": "^0.8.3",
1111
"qs": "6.x",
1212
"image-type": "^2.0.2",
13-
"watson-developer-cloud": "^3.0.5",
13+
"watson-developer-cloud": "^3.0.6",
1414
"kuromoji": "^0.1.1",
1515
"word-count": "^0.2.2",
1616
"is-docx": "^0.0.3",

services/natural_language_classifier/v1.html

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
3232
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
3333
<input type="password" id="node-input-password" placeholder="Password">
3434
</div>
35+
36+
<div class="form-row credentials">
37+
<label>&nbsp;</label>
38+
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
39+
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
40+
</div>
41+
<div class="form-row">
42+
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
43+
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/natural-language-classifier/api">
44+
</div>
45+
3546
<div class="form-row">
3647
<label for="node-input-mode"><i class="fa fa-question"></i> Mode</label>
3748
<select type="text" id="node-input-mode" style="display: inline-block; width: 70%;" >
@@ -88,14 +99,66 @@
8899
</script>
89100

90101
<script type="text/javascript">
102+
// Need to simulate a namespace, so that some of the variables don't leak across nodes
103+
function NaturalLanguageClassifierV1 () {}
104+
105+
// This is the namespace for this version of this Node.
106+
var nlcV1 = new NaturalLanguageClassifierV1();
107+
108+
nlcV1.CreateIListener = function(listen, action, opp) {
109+
opp = typeof opp === 'undefined' ? false : opp;
110+
listen.change(function(val){
111+
var isSet = listen.prop('checked');
112+
if (opp) {
113+
isSet = !isSet;
114+
}
115+
nluV1.setVisibility(action, isSet);
116+
});
117+
}
118+
119+
nlcV1.UIListeners = function () {
120+
$('#node-input-mode').change(function () {
121+
var mode = $('#node-input-mode').val();
122+
$('.form-row.mode.' + mode).show();
123+
$('.form-row.mode:not(.' + mode + ')').hide();
124+
});
125+
126+
nlcV1.CreateIListener($('#node-input-default-endpoint'),
127+
$('#node-input-service-endpoint'), true);
128+
};
129+
130+
nlcV1.checkForPrepare = function () {
131+
nlcV1.UIListeners();
132+
};
133+
134+
// This is the on edit prepare function, which will be
135+
//invoked everytime the dialog is shown.
136+
function nlconeditprepare() {
137+
138+
139+
nlcV1.checkForPrepare();
140+
$.getJSON('watson-natural-language-classifier/vcap/')
141+
.done(function (service) {
142+
$('.credentials').toggle(!service);
143+
})
144+
.fail(function () {
145+
$('.credentials').show();
146+
}).always(function () {
147+
$('#credentials-check').hide();
148+
})
149+
}
150+
151+
91152
(function() {
92153
RED.nodes.registerType('watson-natural-language-classifier', {
93154
category: 'IBM Watson',
94155
defaults: {
95156
name: {value: ""},
96157
mode: {value: "classify"},
97158
language: {value: "en"},
98-
classifier: {value: ""}
159+
classifier: {value: ""},
160+
'default-endpoint' :{value: true},
161+
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/natural-language-classifier/api'}
99162
},
100163
credentials: {
101164
username: {type:"text"},
@@ -112,22 +175,7 @@
112175
labelStyle: function() {
113176
return this.name ? "node_label_italic" : "";
114177
},
115-
oneditprepare: function() {
116-
$('#node-input-mode').change(function () {
117-
var mode = $('#node-input-mode').val();
118-
$('.form-row.mode.' + mode).show();
119-
$('.form-row.mode:not(.' + mode + ')').hide();
120-
})
121-
$.getJSON('watson-natural-language-classifier/vcap/')
122-
.done(function (service) {
123-
$('.credentials').toggle(!service);
124-
})
125-
.fail(function () {
126-
$('.credentials').show();
127-
}).always(function () {
128-
$('#credentials-check').hide();
129-
})
130-
}
178+
oneditprepare: nlconeditprepare
131179
});
132180
})();
133181
</script>

services/natural_language_classifier/v1.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ module.exports = function(RED) {
2828
username = null,
2929
password = null,
3030
sUsername = null,
31-
sPassword = null;
31+
sPassword = null,
32+
endpoint = '',
33+
sEndpoint = 'https://gateway.watsonplatform.net/natural-language-classifier/api';
3234

3335
if (service) {
3436
sUsername = service.username;
3537
sPassword = service.password;
38+
sEndpoint = service.url;
3639
}
3740

3841
temp.track();
@@ -54,6 +57,12 @@ module.exports = function(RED) {
5457
node.verifyCredentials = function(msg) {
5558
username = sUsername || node.credentials.username;
5659
password = sPassword || node.credentials.password;
60+
61+
endpoint = sEndpoint;
62+
if ((!config['default-endpoint']) && config['service-endpoint']) {
63+
endpoint = config['service-endpoint'];
64+
}
65+
5766
if (!username || !password) {
5867
return Promise.reject('Missing Natural Language Classifier credentials');
5968
} else {
@@ -145,14 +154,22 @@ module.exports = function(RED) {
145154

146155
node.performOperation = function(msg, config, params) {
147156
var p = new Promise(function resolver(resolve, reject) {
148-
const natural_language_classifier = new NaturalLanguageClassifierV1({
149-
username: username,
150-
password: password,
151-
version: 'v1',
152-
headers: {
153-
'User-Agent': pkg.name + '-' + pkg.version
154-
}
155-
});
157+
var natural_language_classifier = null,
158+
serviceSettings = {
159+
username: username,
160+
password: password,
161+
version: 'v1',
162+
headers: {
163+
'User-Agent': pkg.name + '-' + pkg.version
164+
}
165+
};
166+
167+
if (endpoint) {
168+
serviceSettings.url = endpoint;
169+
}
170+
171+
natural_language_classifier = new NaturalLanguageClassifierV1(serviceSettings);
172+
156173
natural_language_classifier[config.mode](params, function(err, response) {
157174
if (err) {
158175
reject(err);

services/speech_to_text/v1.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ module.exports = function (RED) {
423423

424424
ws.on('error', (err) => {
425425
socketListening = false;
426+
payloadutils.reportError(node,newMsg,err);
426427
// console.log('Error Detected');
427428
if (initialConnect) {
428429
//reject(err);
@@ -468,7 +469,13 @@ module.exports = function (RED) {
468469
if (audioStack && audioStack.length) {
469470
audioStack.forEach((a) => {
470471
if (a && a.action && 'data' === a.action) {
471-
websocket.send(a.data);
472+
//websocket.send(a.data);
473+
websocket.send(a.data, (error) => {
474+
if (error) {
475+
payloadutils.reportError(node,{},error);
476+
} else {
477+
}
478+
});
472479
}
473480
});
474481
audioStack = [];

0 commit comments

Comments
 (0)