-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhttp.js
208 lines (173 loc) · 4.62 KB
/
http.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
/**
* A wrapper for mikeal's request module for Yakuza
* @module Http
* @author Rafael Vidaurre
*/
'use strict';
var OptionsTemplate, Q, _, needle, defaults;
OptionsTemplate = require('./options-template');
Q = require('q');
needle = require('needle');
_ = require('lodash');
defaults = {
follow_max: 0,
user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like '
+ 'Gecko) Chrome/41.0.2272.76 Safari/537.36',
open_timeout: 60000
};
needle.defaults(defaults);
/**
* @class
* @param {object} defaultCookies cookie jar for the class instance to be initialized with, defaults
* to a brand new cookie jar
*/
function Http (defaultCookies) {
/**
* Cookie jar for the Http instance
* @private
*/
this._cookieJar = defaultCookies || {};
/**
* Array of requests logged
* @private
*/
this._log = [];
}
/**
* Pushes a response object to the request log and responds to passed callback
* @param {object} err Error object, is null if no error is present
* @param {object} res Needle's response
* @param {string} body Needle's response body only, is null if an error is present
* @param {function} callback Callback to be executed
* @private
*/
Http.prototype.__interceptResponse = function (err, res, body, url, data, makeRequest, callback) {
var entry, resCookieString, _this, cb, noop, promiseResponse;
_this = this;
noop = function () {};
cb = callback || noop;
if (err) {
cb(err, null, null);
makeRequest.reject(err);
return;
}
promiseResponse = {
res: res,
body: body
};
resCookieString = '';
_.each(res.cookies, function (value, key) {
// Update our cookie jar
_this._cookieJar[key] = value;
// Build cookie string for logging
if (resCookieString) {
resCookieString += ' ';
}
resCookieString += key + '=' + value + ';';
});
entry = {
request: {
headers: res.req._headers,
cookies: res.req._headers.cookie || '',
url: url,
data: data
},
response: {
cookies: resCookieString,
headers: res.headers,
statusCode: res.statusCode,
body: body
}
};
this.__pushToLog(entry);
cb(err, res, body);
makeRequest.resolve(promiseResponse);
};
/**
* Pushes an entry to the class log
* @param {object} logEntry entry that represents a unit of the log
* @private
*/
Http.prototype.__pushToLog = function (logEntry) {
this._log.push(logEntry);
};
/**
* Does an http request
* @return {promise} a promise that resolves with the request's response
*/
Http.prototype.request = function (method, opts, callback) {
var _this, data, url, finalOpts, makeRequest, extendedCookies;
_this = this;
makeRequest = Q.defer();
url = _.isString(opts) ? opts : opts.url;
if (_.isUndefined(url) || !_.isString(url)) {
throw new Error('Url is not set');
}
opts = _.isObject(opts) ? opts : {};
data = opts.data || null;
finalOpts = _.omit(opts, ['data', 'url']);
extendedCookies = _.extend(this._cookieJar, finalOpts.cookies);
finalOpts.cookies = _.keys(extendedCookies).length > 0 ? extendedCookies : null;
needle.request(method, url, data, finalOpts, function (err, res, body) {
_this.__interceptResponse(err, res, body, url, data, makeRequest, callback);
});
return makeRequest.promise;
};
/**
* Delegate to request's `del` method
*/
Http.prototype.del = function (opts, callback) {
return this.request('delete', opts, callback);
};
/**
* Delegate to request's `get` method
*/
Http.prototype.get = function (opts, callback) {
return this.request('get', opts, callback);
};
/**
* Delegate to request's `head` method
*/
Http.prototype.head = function (opts, callback) {
return this.request('head', opts, callback);
};
/**
* Delegate to request's `patch` method
*/
Http.prototype.patch = function (opts, callback) {
return this.request('patch', opts, callback);
};
/**
* Delegate to request's `post` method
*/
Http.prototype.post = function (opts, callback) {
return this.request('post', opts, callback);
};
/**
* Delegate to request's `put` method
*/
Http.prototype.put = function (opts, callback) {
return this.request('put', opts, callback);
};
/**
* Returns the current request log as an array
* @return {array} current request log
*/
Http.prototype.getLog = function () {
return this._log;
};
/**
* Returns a clone of the current cookie jar
* @return current cookie jar clone
*/
Http.prototype.getCookieJar = function () {
return _.cloneDeep(this._cookieJar);
};
/**
* Return a new options template instace
* @param {object} POJO with base options
*/
Http.prototype.optionsTemplate = function (baseOptions) {
return new OptionsTemplate(baseOptions);
};
module.exports = Http;