Skip to content

Commit 640ffb9

Browse files
committed
Migrate to ibm-watson
1 parent 9633995 commit 640ffb9

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

services/discovery/v1-document-loader.html

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,17 @@
3838
</div>
3939

4040
<div class="form-row credentials">
41-
<label>&nbsp;</label>
42-
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
43-
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
44-
</div>
45-
<div class="form-row">
4641
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
4742
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/discovery/api">
4843
</div>
4944

5045
<div class="form-row">
51-
<label for="node-input-environment_id"><i class="fa fa-tag"></i> Environment ID</label>
52-
<input type="text" id="node-input-environment_id" placeholder="">
46+
<label for="node-input-environmentId"><i class="fa fa-tag"></i> Environment ID</label>
47+
<input type="text" id="node-input-environmentId" placeholder="">
5348
</div>
5449
<div class="form-row">
55-
<label for="node-input-collection_id"><i class="fa fa-tag"></i> Collection ID</label>
56-
<input type="text" id="node-input-collection_id" placeholder="">
50+
<label for="node-input-collectionId"><i class="fa fa-tag"></i> Collection ID</label>
51+
<input type="text" id="node-input-collectionId" placeholder="">
5752
</div>
5853
<div class="form-row">
5954
<label for="node-input-filename"><i class="fa fa-tag"></i> Filename</label>
@@ -69,8 +64,8 @@
6964
<p>If a filename is not provided then one is automatically generated</p>
7065
<p>The collection being written to can be overridden by specifying the
7166
search ids in
72-
<code>msg.discoveryparams.environment_id</code>
73-
, <code>msg.discoveryparams.collection_id</code>
67+
<code>msg.discoveryparams.environmentId</code>
68+
, <code>msg.discoveryparams.collectionId</code>
7469
and <code>msg.discoveryparams.filename</code>
7570
</p>
7671
<p>The document to be added should be passed in as a data buffer
@@ -91,14 +86,6 @@
9186
var disdocV1 = new DiscoveryDocLoaderV1();
9287

9388
disdocV1.UIListeners = function () {
94-
$('input#node-input-default-endpoint').change(function () {
95-
var checked = $('input#node-input-default-endpoint').prop('checked')
96-
if (checked) {
97-
$('#node-input-service-endpoint').parent().hide();
98-
} else {
99-
$('#node-input-service-endpoint').parent().show();
100-
}
101-
});
10289
}
10390

10491
disdocV1.checkForPrepare = function () {
@@ -124,11 +111,10 @@
124111
category: 'IBM Watson',
125112
defaults: {
126113
name: {value: ''},
127-
environment_id: {value: ''},
128-
collection_id: {value: ''},
114+
environmentId: {value: ''},
115+
collectionId: {value: ''},
129116
filename: {value: ''},
130-
'default-endpoint' :{value: true},
131-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/discovery/api'}
117+
'service-endpoint' :{value: ''}
132118
},
133119
credentials: {
134120
username: {type:'text'},

services/discovery/v1-document-loader.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,18 @@ module.exports = function (RED) {
6969
function determineSuffix(msg) {
7070
// Let's assume that if we can't determine the suffix that
7171
// its a word doc.
72-
var ext = '.json',
73-
ft = fileType(msg.payload);
72+
let ext = '.json';
73+
if (! discoveryutils.isJsonObject(msg.payload)) {
74+
let ext = '.json',
75+
ft = fileType(msg.payload);
7476

75-
if (ft && ft.ext) {
76-
ext = '.' + ft.ext;
77-
}
78-
if (isDocx(msg.payload)) {
79-
ext = '.docx';
77+
if (ft && ft.ext) {
78+
ext = '.' + ft.ext;
79+
}
80+
81+
if (isDocx(msg.payload)) {
82+
ext = '.docx';
83+
}
8084
}
8185

8286
return Promise.resolve(ext);
@@ -131,20 +135,22 @@ module.exports = function (RED) {
131135
// modify as getting addJsonDocument will be deprecated messages
132136
if ('.json' === suffix) {
133137
//method = 'addJsonDocument';
134-
params.file = JSON.stringify(params.file);
138+
//params.file = JSON.stringify(params.file);
139+
140+
params.file = Buffer.from(JSON.stringify(params.file));
135141
//} else {
136142
//method = 'addDocument';
137143
}
138144
method = 'addDocument';
139145

140-
discovery[method](params, function (err, response) {
141-
if (err) {
142-
reject(err);
143-
} else {
144-
msg.document = response.document ? response.document : response;
146+
discovery[method](params)
147+
.then((response) => {
148+
msg.document = response.result ? response.result : response;
145149
resolve();
146-
}
147-
});
150+
})
151+
.catch((err) => {
152+
reject(err);
153+
});
148154

149155
});
150156
return p;
@@ -166,7 +172,7 @@ module.exports = function (RED) {
166172
var node = this;
167173
RED.nodes.createNode(this, config);
168174

169-
this.on('input', function (msg) {
175+
this.on('input', function(msg, send, done) {
170176
var message = '',
171177
fileInfo = '',
172178
fileSuffix = '',
@@ -177,7 +183,7 @@ module.exports = function (RED) {
177183
apikey = sApikey || this.credentials.apikey;
178184

179185
endpoint = sEndpoint;
180-
if ((!config['default-endpoint']) && config['service-endpoint']) {
186+
if (config['service-endpoint']) {
181187
endpoint = config['service-endpoint'];
182188
}
183189

@@ -223,12 +229,13 @@ module.exports = function (RED) {
223229
.then(function(){
224230
temp.cleanup();
225231
node.status({});
226-
node.send(msg);
232+
send(msg);
233+
done();
227234
})
228235
.catch(function(err){
229236
temp.cleanup();
230-
payloadutils.reportError(node,msg,err);
231-
node.send(msg);
237+
let errMsg = payloadutils.reportError(node, msg, err);
238+
done(errMsg);
232239
});
233240
});
234241
}

0 commit comments

Comments
 (0)