-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostman.js
More file actions
61 lines (50 loc) · 1.91 KB
/
postman.js
File metadata and controls
61 lines (50 loc) · 1.91 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
var accessKeyID = pm.environment.get('accessKeyID');
var secretKey = pm.environment.get('secretKey');
var CryptoJS = require('crypto-js');
const query = pm.request.url.query;
const apiMethod = query.find(x => x.key == 'method').value;
fixBadParams(query);
const params = query.find(x => x.key == 'params').value;
var shaData, queryString;
if (request.method == 'POST') {
var postData = JSON.stringify({ 'id': 1, 'jsonrpc': '2.0', 'method': apiMethod, 'params': JSON.parse(params) })
shaData = postData;
pm.globals.set('postData', postData);
queryString = 'access_key_id=' + encodeURIComponent(accessKeyID);
}
else if (request.method == 'GET') {
pm.request.url.addQueryParams('access_key_id=' + encodeURIComponent(accessKeyID));
pm.request.url.addQueryParams('jsonrpc=2.0');
pm.request.url.addQueryParams('id=1');
queryString = query.filter(x => !x.disabled).map(x => x.key + '=' + encodeURIComponent(x.key == 'params' ? btoa(params) : x.value )).join('&');
shaData = 'method' + apiMethod + 'params' + params;
}
var signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(shaData, secretKey));
queryString += '&signature=' + encodeURIComponent(signature);
pm.request.url.query = queryString;
// handle missing / disabled / empty params
function fixBadParams(query) {
// if missing, add
if (!query.find(x => x.key == 'params')) {
pm.request.url.addQueryParams('params={}');
}
var qParams = query.find(x => x.key == 'params');
// if disabled, enable but make it empty
if (qParams.disabled) {
qParams.value = '{}';
qParams.disabled = false;
}
// if no value, give default value
else if (!qParams.value) {
qParams.value = '{}';
}
}
function btoa(str) {
var buffer;
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = Buffer.from(str.toString(), 'binary');
}
return buffer.toString('base64');
}