-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref_client.js
More file actions
executable file
·101 lines (80 loc) · 2.78 KB
/
ref_client.js
File metadata and controls
executable file
·101 lines (80 loc) · 2.78 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
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
#!/usr/local/bin/node
const https = require('https')
const jsSHA = require('jssha')
var accessKeyID = '01234567-89ab-cdef-0123-456789abcdef'
var secretKey = 'this-is-just-a-test-replace-with-your-keys'
accountList()
async function accountList () {
// https://www.shiftdata.com/#account-list
var params = {
select: {
},
page: {
batch: 120,
start: 1,
}
}
var accountListResult = await shiftboardAPI('account.list', params )
if ( accountListResult.result.page.next ) {
params.page = accountListResult.result.page.next
var accountListResult2 = await shiftboardAPI('account.list', params )
console.log(JSON.stringify(accountListResult2))
}
else {
console.log(JSON.stringify(accountListResult))
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------
async function shiftboardAPI (api_method, obj) {
var options = {
host: 'api.shiftdata.com',
path: '/servola/api/api.cgi',
method: 'POST',
}
var post_data
var params = JSON.stringify(obj)
var path = '?access_key_id=' + encodeURIComponent(accessKeyID)
var shaObj = new jsSHA('SHA-1', 'TEXT')
shaObj.setHMACKey(secretKey, 'TEXT')
if (options.method == 'POST') {
post_data = JSON.stringify({ 'id': 1, 'jsonrpc': '2.0', 'method': api_method, 'params': JSON.parse(params) })
shaObj.update(post_data)
options.headers = { 'Content-Type': 'application/json', 'Content-Length': post_data.length }
}
else if (options.method == 'GET') {
path += '&jsonrpc=2.0&id=1&method=' + encodeURIComponent(api_method) + '¶ms=' + encodeURIComponent(btoa(params))
shaObj.update('method' + api_method + 'params' + params)
}
var hmac = shaObj.getHMAC('B64')
path += '&signature=' + encodeURIComponent(hmac)
options.path += path
let p = new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
//console.log(options)
let responseBody = '';
res.on('data', (chunk) => {
responseBody += chunk
})
res.on('end', () => {
resolve(JSON.parse(responseBody))
})
})
req.on('error', reject)
if (options.method == 'POST') {
//console.log(JSON.parse(post_data))
req.write(post_data)
}
req.end()
})
return p
}
function btoa(str) {
var buffer;
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = Buffer.from(str.toString(), 'binary');
}
return buffer.toString('base64');
}