Skip to content

Commit

Permalink
Merge pull request #378 from chughts/nlc-germany
Browse files Browse the repository at this point in the history
Endpoint switching for NLC Node
  • Loading branch information
chughts authored Feb 18, 2018
2 parents e40cc4d + 91de7b0 commit f3b5439
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Node-RED Watson Nodes for IBM Cloud

<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>

### New in version 0.6.5
- Endpoint can now be specified in Natural Language Classifier Node

### New in version 0.6.4
- Speech to Text node now reports all errors, including disconnects when running in stream mode.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.6.4",
"version": "0.6.5",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"async": "^1.5.2",
Expand All @@ -10,7 +10,7 @@
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^3.0.5",
"watson-developer-cloud": "^3.0.6",
"kuromoji": "^0.1.1",
"word-count": "^0.2.2",
"is-docx": "^0.0.3",
Expand Down
82 changes: 65 additions & 17 deletions services/natural_language_classifier/v1.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-input-password" placeholder="Password">
</div>

<div class="form-row credentials">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
</div>
<div class="form-row">
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/natural-language-classifier/api">
</div>

<div class="form-row">
<label for="node-input-mode"><i class="fa fa-question"></i> Mode</label>
<select type="text" id="node-input-mode" style="display: inline-block; width: 70%;" >
Expand Down Expand Up @@ -88,14 +99,66 @@
</script>

<script type="text/javascript">
// Need to simulate a namespace, so that some of the variables don't leak across nodes
function NaturalLanguageClassifierV1 () {}

// This is the namespace for this version of this Node.
var nlcV1 = new NaturalLanguageClassifierV1();

nlcV1.CreateIListener = function(listen, action, opp) {
opp = typeof opp === 'undefined' ? false : opp;
listen.change(function(val){
var isSet = listen.prop('checked');
if (opp) {
isSet = !isSet;
}
nluV1.setVisibility(action, isSet);
});
}

nlcV1.UIListeners = function () {
$('#node-input-mode').change(function () {
var mode = $('#node-input-mode').val();
$('.form-row.mode.' + mode).show();
$('.form-row.mode:not(.' + mode + ')').hide();
});

nlcV1.CreateIListener($('#node-input-default-endpoint'),
$('#node-input-service-endpoint'), true);
};

nlcV1.checkForPrepare = function () {
nlcV1.UIListeners();
};

// This is the on edit prepare function, which will be
//invoked everytime the dialog is shown.
function nlconeditprepare() {


nlcV1.checkForPrepare();
$.getJSON('watson-natural-language-classifier/vcap/')
.done(function (service) {
$('.credentials').toggle(!service);
})
.fail(function () {
$('.credentials').show();
}).always(function () {
$('#credentials-check').hide();
})
}


(function() {
RED.nodes.registerType('watson-natural-language-classifier', {
category: 'IBM Watson',
defaults: {
name: {value: ""},
mode: {value: "classify"},
language: {value: "en"},
classifier: {value: ""}
classifier: {value: ""},
'default-endpoint' :{value: true},
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/natural-language-classifier/api'}
},
credentials: {
username: {type:"text"},
Expand All @@ -112,22 +175,7 @@
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
$('#node-input-mode').change(function () {
var mode = $('#node-input-mode').val();
$('.form-row.mode.' + mode).show();
$('.form-row.mode:not(.' + mode + ')').hide();
})
$.getJSON('watson-natural-language-classifier/vcap/')
.done(function (service) {
$('.credentials').toggle(!service);
})
.fail(function () {
$('.credentials').show();
}).always(function () {
$('#credentials-check').hide();
})
}
oneditprepare: nlconeditprepare
});
})();
</script>
35 changes: 26 additions & 9 deletions services/natural_language_classifier/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ module.exports = function(RED) {
username = null,
password = null,
sUsername = null,
sPassword = null;
sPassword = null,
endpoint = '',
sEndpoint = 'https://gateway.watsonplatform.net/natural-language-classifier/api';

if (service) {
sUsername = service.username;
sPassword = service.password;
sEndpoint = service.url;
}

temp.track();
Expand All @@ -54,6 +57,12 @@ module.exports = function(RED) {
node.verifyCredentials = function(msg) {
username = sUsername || node.credentials.username;
password = sPassword || node.credentials.password;

endpoint = sEndpoint;
if ((!config['default-endpoint']) && config['service-endpoint']) {
endpoint = config['service-endpoint'];
}

if (!username || !password) {
return Promise.reject('Missing Natural Language Classifier credentials');
} else {
Expand Down Expand Up @@ -145,14 +154,22 @@ module.exports = function(RED) {

node.performOperation = function(msg, config, params) {
var p = new Promise(function resolver(resolve, reject) {
const natural_language_classifier = new NaturalLanguageClassifierV1({
username: username,
password: password,
version: 'v1',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
});
var natural_language_classifier = null,
serviceSettings = {
username: username,
password: password,
version: 'v1',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
};

if (endpoint) {
serviceSettings.url = endpoint;
}

natural_language_classifier = new NaturalLanguageClassifierV1(serviceSettings);

natural_language_classifier[config.mode](params, function(err, response) {
if (err) {
reject(err);
Expand Down
9 changes: 8 additions & 1 deletion services/speech_to_text/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ module.exports = function (RED) {

ws.on('error', (err) => {
socketListening = false;
payloadutils.reportError(node,newMsg,err);
// console.log('Error Detected');
if (initialConnect) {
//reject(err);
Expand Down Expand Up @@ -468,7 +469,13 @@ module.exports = function (RED) {
if (audioStack && audioStack.length) {
audioStack.forEach((a) => {
if (a && a.action && 'data' === a.action) {
websocket.send(a.data);
//websocket.send(a.data);
websocket.send(a.data, (error) => {
if (error) {
payloadutils.reportError(node,{},error);
} else {
}
});
}
});
audioStack = [];
Expand Down

0 comments on commit f3b5439

Please sign in to comment.