-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
requestbin.js
212 lines (173 loc) · 4.39 KB
/
requestbin.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
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
205
206
207
208
209
210
211
212
/*
Name: requestbin
Description: Node.js package to access the Requestb.in API
Author: Franklin van de Meent (https://frankl.in)
License: Unlicence (public domain, see LICENSE file)
Source & docs: https://github.com/fvdm/nodejs-requestbin
Feedback: https://github.com/fvdm/nodejs-requestbin/issues
*/
var httpreq = require ('httpreq');
var config = {
timeout: 5000,
iface: null,
baseURL: null,
userAgent: null
};
/**
* Build an error
*
* @callback callback
* @param {string} msg Error message
* @param {mixed} err Error details
* @param {object} res httpreq response
* @param {object} options request options
* @param {function} callback `(err)`
* @return {void}
*/
function doError (msg, err, res, options, callback) {
var error = new Error (msg);
error.httpCode = res && res.statusCode || null;
error.error = err || null;
error.request = options;
error.response = {
headers: res && res.headers,
body: res && res.body
};
callback (error);
}
/**
* Process response
*
* argument order is to prevent callback
* handler detection in code parsers
*
* @callback callback
* @param res {object} - httpreq response
* @param err {Error|null} - httpreq error
* @param options {object} - httpreq options
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function processResponse (res, err, options, callback) {
var data = res && res.body || null;
var error = null;
if (err) {
doError ('request failed', err, res, callback);
return;
}
if (res && res.statusCode >= 300) {
doError ('HTTP error', null, res, callback);
return;
}
try {
data = JSON.parse (data);
callback (null, data);
} catch (e) {
doError ('invalid response', e, res, callback);
}
}
/**
* Communicate with API
*
* @callback callback
* @param method {string} - HTTP method, GET, POST, etc
* @param path {string} - Request path
* @param [props] {object} - Send query or POST parameters
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function talk (method, path, props, callback) {
var options = {
url: (config.baseURL || 'http://requestb.in/api/v1/') + path,
method: method,
parameters: props,
timeout: config.timeout || 5000,
localAddress: config.iface,
headers: {
'Accept': 'application/json',
'User-Agent': (config.userAgent || 'requestbin.js (https://github.com/fvdm/nodejs-requestbin)')
}
};
if (typeof props === 'function') {
callback = props;
props = {};
}
httpreq.doRequest (options, function (err, res) {
processResponse (res, err, options, callback);
});
}
/**
* Set configuration
*
* @param props {object} - Config parameters
* @return {void}
*/
function methodConfig (props) {
var name;
for (name in props) {
config [name] = props [name];
}
}
/**
* Create request instance
*
* @callback callback
* @param [isPrivate = false] {boolean} - Make instance private
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function methodCreate (isPrivate, callback) {
var props = {};
if (typeof isPrivate === 'function') {
callback = isPrivate;
} else if (isPrivate === true) {
props.private = 'true';
}
talk ('POST', 'bins', props, callback);
}
/**
* Get a request instance
*
* @callback callback
* @param bin {string} - Bin instance ID
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function methodGet (bin, callback) {
talk ('GET', 'bins/' + bin, callback);
}
/**
* Get received requests
*
* @callback callback
* @param bin {string} - Bin instance ID
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function methodRequests (bin, callback) {
talk ('GET', 'bins/' + bin + '/requests', callback);
}
/**
* Get one received request
*
* @callback callback
* @param bin {string} - Bin instance ID
* @param request {string} - Request ID
* @param callback {function} - `function (err, data) {}`
* @return {void}
*/
function methodRequest (bin, request, callback) {
talk ('GET', 'bins/' + bin + '/requests/' + request, callback);
}
/**
* Interface methods
*
* @return {object} - Methods
*/
module.exports = {
config: methodConfig,
create: methodCreate,
get: methodGet,
requests: methodRequests,
request: methodRequest
};