ddee9620573402c8044bb2da580abe97a1edc360.svn-base
4.8 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* common.js: Common utility functions for requesting against Loggly APIs
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENSE
*
*/
var util = require('util'),
request = require('request'),
loggly = require('../loggly');
var common = exports;
//
// Failure HTTP Response codes based
// off Loggly specification.
//
var failCodes = common.failCodes = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
409: "Conflict / Duplicate",
410: "Gone",
500: "Internal Server Error",
501: "Not Implemented",
503: "Throttled"
};
//
// Success HTTP Response codes based
// off Loggly specification.
//
var successCodes = common.successCodes = {
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-authoritative information",
204: "Deleted",
};
//
// Core method that actually sends requests to Loggly.
// This method is designed to be flexible w.r.t. arguments
// and continuation passing given the wide range of different
// requests required to fully implement the Loggly API.
//
// Continuations:
// 1. 'callback': The callback passed into every node-loggly method
// 2. 'success': A callback that will only be called on successful requests.
// This is used throughout node-loggly to conditionally
// do post-request processing such as JSON parsing.
//
// Possible Arguments (1 & 2 are equivalent):
// 1. common.loggly('some-fully-qualified-url', auth, callback, success)
// 2. common.loggly('GET', 'some-fully-qualified-url', auth, callback, success)
// 3. common.loggly('DELETE', 'some-fully-qualified-url', auth, callback, success)
// 4. common.loggly({ method: 'POST', uri: 'some-url', body: { some: 'body'} }, callback, success)
//
common.loggly = function () {
var args = Array.prototype.slice.call(arguments),
success = args.pop(),
callback = args.pop(),
requestBody,
headers,
method,
auth,
uri;
//
// Now that we've popped off the two callbacks
// We can make decisions about other arguments
//
if (args.length == 1) {
if (typeof args[0] === 'string') {
//
// If we got a string assume that it's the URI
//
method = 'GET';
uri = args[0];
}
else {
method = args[0]['method'] || 'GET',
uri = args[0]['uri'];
requestBody = args[0]['body'];
auth = args[0]['auth'];
headers = args[0]['headers'];
}
}
else if (args.length == 2) {
method = 'GET';
uri = args[0];
auth = args[1];
}
else {
method = args[0];
uri = args[1];
auth = args[2];
}
function onError (err) {
if (callback) {
callback(err);
}
}
var requestOptions = {
uri: uri,
method: method,
headers: headers || {}
};
if (auth) {
requestOptions.headers['authorization'] = 'Basic ' + new Buffer(auth.username + ':' + auth.password).toString('base64');
}
if (requestBody) {
requestOptions.body = requestBody;
}
try {
request(requestOptions, function (err, res, body) {
if (err) {
return onError(err);
}
var statusCode = res.statusCode.toString();
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode])));
}
success(res, body);
});
}
catch (ex) {
onError(ex);
}
};
//
// ### function serialize (obj, key)
// #### @obj {Object|literal} Object to serialize
// #### @key {string} **Optional** Optional key represented by obj in a larger object
// Performs simple comma-separated, `key=value` serialization for Loggly when
// logging to non-JSON inputs.
//
common.serialize = function (obj, key) {
if (obj === null) {
obj = 'null';
}
else if (obj === undefined) {
obj = 'undefined';
}
else if (obj === false) {
obj = 'false';
}
if (typeof obj !== 'object') {
return key ? key + '=' + obj : obj;
}
var msg = '',
keys = Object.keys(obj),
length = keys.length;
for (var i = 0; i < length; i++) {
if (Array.isArray(obj[keys[i]])) {
msg += keys[i] + '=['
for (var j = 0, l = obj[keys[i]].length; j < l; j++) {
msg += common.serialize(obj[keys[i]][j]);
if (j < l - 1) {
msg += ', ';
}
}
msg += ']';
}
else {
msg += common.serialize(obj[keys[i]], keys[i]);
}
if (i < length - 1) {
msg += ', ';
}
}
return msg;
};
//
// function clone (obj)
// Helper method for deep cloning pure JSON objects
// i.e. JSON objects that are either literals or objects (no Arrays, etc)
//
common.clone = function (obj) {
var clone = {};
for (var i in obj) {
clone[i] = obj[i] instanceof Object ? common.clone(obj[i]) : obj[i];
}
return clone;
};