forked from alertlogic/al-collector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
al_util.js
71 lines (67 loc) · 1.84 KB
/
al_util.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
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Helper utilities for Alert Logic log collector.
*
* @end
* -----------------------------------------------------------------------------
*/
const rp = require('request-promise-native');
const path = require('path');
let MAX_CONNS_PER_SERVICE = 128;
/**
* @class
* Rest client.
*
* @constructor
* @param {string} endpoint - hostname/address to sent HTTPS
* requests to.
*
*/
class RestServiceClient {
constructor(endpoint) {
this._host = endpoint;
this._url = 'https://' + endpoint;
this._pool = {
maxSockets: MAX_CONNS_PER_SERVICE
};
}
_initRequestOptions(method, path, extra) {
const defaultOptions = {
method: method,
url: this._url + path,
json: true,
headers: {},
pool: this._pool
};
const options = Object.assign({}, defaultOptions, extra);
const defaultHeaders = {
'Accept': 'application/json'
};
Object.assign(options.headers,
defaultHeaders,
extra.headers ? extra.headers : {});
return options;
}
request(method, path, extraOptions) {
const options = this._initRequestOptions(method, path, extraOptions);
return rp(options);
}
post(path, extraOptions) {
return this.request('POST', path, extraOptions);
}
get(path, extraOptions) {
return this.request('GET', path, extraOptions);
}
deleteRequest(path, extraOptions) {
return this.request('DELETE', path, extraOptions);
}
get host() {
return this._host;
}
get url() {
return this._url;
}
}
exports.RestServiceClient = RestServiceClient;