-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (80 loc) · 3.13 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const axios = require('axios');
const crypto = require('crypto');
const date = require('date-and-time');
class apigwCLient{
constructor(baseUrl, apiKey, apiSecret){
this.baseUrl = baseUrl;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}
computeHeader( jsonData){
var authToken = "SELCOM" +' '+ Buffer.from(this.apiKey, 'ascii').toString('base64');
var now= new Date();
var timestamp = date.format(now,"YYYY-MM-DD[T]HH:mm:ssZZ" );
var signedFields = ""
var data= "timestamp=" + timestamp;
for (var key in jsonData){
data = data + "&" + key + "=" + jsonData[key];
if (signedFields == '')
signedFields = signedFields + key;
else
signedFields = signedFields + "," + key;
}
var hmac = crypto.createHmac('sha256', this.apiSecret);
hmac.update(data);
var digest= hmac.digest('base64');
return ([authToken, timestamp, digest, signedFields]);
}
async postFunc(path, jsonData){
var [authToken, timestamp, digest, signedFields] = this.computeHeader(jsonData);
return await axios({
method:'post',
url:this.baseUrl + path,
headers:{
"Content-type": "application/json",
"Authorization":authToken,
"Digest-Method": "HS256",
"Digest": digest,
"Timestamp": timestamp,
"Signed-Fields":signedFields,
},
data:jsonData,
}).then((response)=> {return response.data} )
.catch((error)=>{return error.response.data});
}
async getFunc(path,jsonData){
var [authToken, timestamp, digest, signedFields] = this.computeHeader(jsonData);
return await axios({
method:'get',
url:this.baseUrl + path,
headers:{
"Content-type": "application/json",
"Authorization":authToken,
"Digest-Method": "HS256",
"Digest": digest,
"Timestamp": timestamp,
"Signed-Fields":signedFields,
},
params:jsonData
}).then(response=>{return response.data})
.catch((error)=>{return error.response.data});
}
async deleteFunc(path,jsonData){
var [authToken, timestamp, digest, signedFields] = this.computeHeader(jsonData);
return await axios({
method:'delete',
url: this.baseUrl + path,
headers:{
"Content-type": "application/json",
"Authorization":authToken,
"Digest-Method": "HS256",
"Digest": digest,
"Timestamp": timestamp,
"Signed-Fields":signedFields,
},
params:jsonData,
}).then((response)=>{return response.data})
.catch((error)=>{return error.response.data});
}
}
module.exports = {apigwCLient};