-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget.js
More file actions
42 lines (40 loc) · 1.1 KB
/
get.js
File metadata and controls
42 lines (40 loc) · 1.1 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
const fs = require('fs');
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
const argv = require('minimist')(process.argv.slice(2));
let ep = argv._[0];
delete argv._;
let dbug = process.env.DEBUG;
(async () => {
try {
if (!ep) throw(`Usage node get.js <end_point> [ --header-name header-value ]`);
const config = await getAuthToken(superagent);
ep = ep.replace(/^_\//, '');
ep = ep.replace(/__/g, '/');
let url = `${config.okapi}/${ep}`;
let headers = {
'User-Agent': config.agent,
'cookie': config.cookie,
'x-okapi-tenant': config.tenant,
'x-okapi-token': config.token,
'accept': 'application/json'
};
for (let k in argv) {
let v = argv[k];
headers[k] = v;
}
console.warn('GET', url);
try {
const res = await superagent
.get(url)
.set(headers);
if (dbug) console.log(res);
console.log(JSON.stringify(res.body, null, 2));
} catch (e) {
let msg = (dbug) ? e : `${e}`;
console.log(msg);
}
} catch(e) {
console.log(`${e}`);
}
})();