-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (58 loc) · 1.61 KB
/
index.js
File metadata and controls
66 lines (58 loc) · 1.61 KB
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
64
65
66
const easySoap = require('easysoap');
let params = {
host: 'collivery.co.za',
path: '/ws/v2',
wsdl: '/wsdl/v2',
headers: {
'app_name': 'njs_soap_test'
},
rejectUnauthorized: false
}
const soapClient = easySoap(params, {secure: true});
let authenticate = makeCall('authenticate', {'xsi:type': 'xsd:string'}, {
email: 'api@collivery.co.za',
password: 'api123',
}).catch(error => console.error(error));
authenticate.then(body => {
// The token should be aggressively cached
// Once it's used - it's active for another hour.
let token = body.token;
let data = {
location_type: 1,
town_id: 147,
suburb_id: 1936,
country: 'ZAF',
building: 'MDS House',
street_number: '58c',
street: 'Webber street',
full_name: 'Joe Soap',
cellphone: '0721234567',
};
let addAddress = makeCall("add_address",{}, {data}, token);
addAddress.then(body => {
console.log(body);
}).catch(error => console.error(error));
});
/**
*
* @param method string
* @param attributes Object
* @param params Object
* @param token string
* @return {Promise<{}>}
*/
async function makeCall(method, attributes, params, token = null)
{
if (token) {
params['token'] = token;
}
let response = await soapClient.call({method, attributes, params});
if (response.data.hasOwnProperty('Fault')) {
throw Error(response.data.Fault.faultstring);
}
// Turn the response data from `{key: {}, value: {}}[]` into `{key: value}`
return response.data[`${method}Response`].return.item.reduce((previous, item) => {
previous[item.key.value] = item.value.value;
return previous;
}, {});
}