/
api.js
143 lines (115 loc) · 4.02 KB
/
api.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var assert = require('assert');
var request = require('superagent-promise');
var debug = require('debug')('papertrail');
var debugResponse = require('debug')('papertrail:response');
var debugRequest = require('debug')('papertrail:request');
var util = require('util');
var DEFAULT_BASE_URL = 'https://papertrailapp.com/api/v1/';
function Response(result) {
this.result = result.body;
}
function decorateRequest(method) {
return function() {
var args = Array.prototype.slice.call(arguments);
var ctx = this;
var req = method.apply(ctx, args);
req.set('X-Papertrail-Token', this.token);
return req.end().then(function(res) {
// Handle errors in successful requests...
if (res.error) {
var err = new Error(
'Papertrail api error unexpected status: ' + res.status
);
err.status = res.statusCode;
err.body = res.body;
debug('request error | %d | %s | %j', err.status, err, err.body);
throw err;
}
debugResponse(
'%s | %d | %j', req.url, res.statusCode, res.body
);
return res.body;
});
};
}
function sendRequest(method, req, options) {
debugRequest(req.url, 'body:', options);
if (method !== 'get') {
// for put/post/del we use form.
req.set('Content-Type', 'application/x-www-form-urlencoded');
req.send(options);
} else {
// for get requests all options are query params.
req.query(options);
}
return req;
}
function api(method, url) {
return decorateRequest(function(options) {
var req = request[method](this.baseUrl + url);
return sendRequest(method, req, options);
});
}
function apiWithId(method, url) {
return decorateRequest(function(id, options) {
assert(id, 'id is required');
var req = request[method](this.baseUrl + util.format(url, id));
return sendRequest(method, req, options);
});
}
/**
@param {Object} options for client.
@param {String} options.token from your papertrail client.
@param {String} [options.baseUrl] baseUrl for the papertrail api.
*/
function Papertrail(options) {
options = options || {};
options.token = options.token || process.env.PAPERTRAIL_API_TOKEN;
options.baseUrl = options.baseUrl || DEFAULT_BASE_URL;
assert(options, 'You must pass options');
assert(options.token, 'You must pass a `token` field');
assert(options.baseUrl, 'You must pass a `baseUrl` field');
this.token = options.token;
this.baseUrl = options.baseUrl;
}
Papertrail.prototype = {
/** event searching */
/**
@param {Object} options for search events.
*/
searchEvents: decorateRequest(function(options) {
options = options || {};
return request.get(this.baseUrl + 'events/search').
query(options);
}),
/** system apis */
listSystems: api('get', 'systems.json'),
getSystem: apiWithId('get', 'systems/%s.json'),
registerSystem: api('post', 'systems.json'),
updateSystem: apiWithId('put', 'systems/%s.json'),
deleteSystem: apiWithId('del', 'systems/%s.json'),
systemJoinGroup: apiWithId('post', 'systems/%s/join.json'),
systemLeaveGroup: apiWithId('post', 'systems/%s/leave.json'),
/** save searches */
savedSearches: api('get', 'searches.json'),
savedSearch: apiWithId('get', 'searches/%s.json'),
createSavedSearch: api('post', 'searches.json'),
updateSavedSearch: apiWithId('put', 'searches/%s.json'),
deleteSavedSearch: apiWithId('del', 'searches/%s.json'),
/** manage groups */
listGroups: api('get', 'groups.json'),
getGroup: apiWithId('get', 'groups/%s.json'),
createGroup: api('post', 'groups.json'),
updateGroup: apiWithId('put', 'groups/%s.json'),
deleteGroup: apiWithId('del', 'groups/%s.json'),
/** manage log destinations */
listLogDestinations: api('get', 'destinations.json'),
getLogDestination: apiWithId('get', 'destinations/%s.json'),
/** manage users */
listUsers: api('get', 'users.json'),
inviteUser: api('post', 'users/invite.json'),
deleteUser: apiWithId('del', 'users/%s.json'),
/** account details */
getUsage: api('get', 'account.json')
};
module.exports = Papertrail;