-
Notifications
You must be signed in to change notification settings - Fork 1
/
WML.js
63 lines (54 loc) · 1.44 KB
/
WML.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
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
function main(params) {
const request = require('request-promise');
const btoa = require("btoa");
/*
var params={
GENDER:"M",
AGE:33,
MARITAL_STATUS:"Married",
PROFESSION:"Retail"
};*/
var wml_username = "XXXX";
var wml_password = "XXXX";
var wml_url = "https://ibm-watson-ml.mybluemix.net"
const tokenUrl = wml_url + "/v3/identity/token";
/*
Has to be changed regarding your model.
*/
var GENDER = params.gender || "M";
var AGE = params.age || 33;
var MARITAL_STATUS = params.marital_status || "Married";
var PROFESSION = params.profession || "Retail";
var values = new Array(GENDER, AGE, MARITAL_STATUS, PROFESSION);
var info = '{"fields": ["GENDER", "AGE", "MARITAL_STATUS", "PROFESSION"], "values": [["'+ GENDER +'",'+AGE+',"'+MARITAL_STATUS+'","'+PROFESSION+'"]]}';
const tokenHeader = "Basic " + btoa((wml_username + ":" + wml_password));
var options = { method: 'GET',
url: tokenUrl,
json: true,
headers:
{ authorization: tokenHeader }
};
var promise = request(options)
.then(function (body) {
var auth = "Bearer " + body.token;
var options2 = { method: 'POST',
url: 'XXX', headers:
{
authorization: auth },
body: info };
return options2
})
.then(function (repos) {
console.log(repos);
return request(repos);
})
.then(function (repos2) {
console.log(repos2);
return JSON.parse(repos2);
})
.catch(function (err) {
console.log(err);
});
return promise;
}