-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
54 lines (47 loc) · 1.36 KB
/
utility.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
const API_URL = "https://api.billmate.se";
const BILLMATE_VERSION = "2.1.7";
let request = require('request');
let crypto = require('crypto');
let time = () => {
let hrTime = process.hrtime();
return (hrTime[0] * 1000000 + hrTime[1] / 1000);
};
let hash = (key, data) => {
return crypto.createHmac('sha512', key).update(JSON.stringify(data)).digest('hex');
}
let getPayload = (functionName, data, config) => {
return {
credentials: {
id: config.id,
client: config.client,
language: config.language,
serverdata: config.serverdata,
time: time(),
test: config.test,
version: BILLMATE_VERSION,
hash: hash(config.key, data)
},
data: data,
function: functionName
}
}
module.exports = {
makeRequest: (functionName, data, config) => {
return new Promise((resolve, reject) => {
request.post({
url: API_URL,
json: getPayload(functionName, data, config)
}, (error, res, body) => {
if (error) {
reject(error);
}
else if (res.statusCode == 200) {
resolve(body);
}
else {
reject(body);
}
});
});
}
};