forked from SAP-archive/yaas-node-red-contrib-yaas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommender.js
50 lines (40 loc) · 1.86 KB
/
recommender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
module.exports = function(RED) {
var recommenderBasePath = '/icn/recommender/v1/{{projectId}}/recommendations';
var YaaS = require('yaas.js');
function YaasRecommendationGetNode(config) {
RED.nodes.createNode(this, config);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.tenant_id = config.tenantId;
this.status({fill: 'yellow', shape: 'ring', text: 'idle'});
var yaas = new YaaS();
yaas.init(
this.yaasCredentials.client_id,
this.yaasCredentials.client_secret,
'ml.recommender_view',
this.tenant_id)
.then(() => {
this.on('input', msg => {
var productCode = msg.payload.code || msg.payload;
this.status({fill: 'yellow', shape: 'dot', text: 'Getting recommendations for ' + productCode});
var params = {
productCode : productCode,
recommendationCount : 2
};
yaas.requestHelper.get(recommenderBasePath, params)
.then(response => {
var statusText = 'Received ' + response.body.length + ' recommendations';
this.status({fill: 'green', shape: 'dot', text: statusText});
this.send({payload: response.body});
})
.catch(error => {
this.error('Error while getting recommendations for ' + productCode);
var statusText = 'Error while getting recommendations for ' + productCode;
this.status({fill: 'red', shape: 'dot', text: statusText});
console.error(JSON.stringify(error));
});
});
});
}
RED.nodes.registerType('get recommendation', YaasRecommendationGetNode);
};