forked from SuprDewd/uva-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestClient.js
64 lines (58 loc) · 1.84 KB
/
requestClient.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
var extend = require('extend');
var request = require('request');
function RequestClient(protocol, host){
this.protocol = protocol;
this.host = host;
this.jar = request.jar();
}
RequestClient.prototype.request = function(opts, callback){
opts = extend(true, {
jar: this.jar,
followAllRedirects: true,
headers: {
'Accept-Charset': 'utf-8,ISO-8859-1',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent' : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) "+
"AppleWebKit/537.17 (KHTML, like Gecko) "+
"Chrome/24.0.1312.57 Safari/537.17",
"Accept" : "text/html, application/xml, text/xml, */*",
},
}, opts);
request(opts, callback);
};
RequestClient.prototype.post = function(path, data, opts, callback){
if (typeof opts === 'function'){
callback = opts;
opts = {};
}
opts = extend({
method: "POST",
form: data,
url: path.charAt(0) === '/' ? this.protocol + "://" + this.host + path : path,
}, opts);
this.request(opts, callback);
};
RequestClient.prototype.postMultipart = function(path, data, opts, callback){
if (typeof opts === 'function'){
callback = opts;
opts = {};
}
opts = extend({
method: "POST",
formData: data,
url: path.charAt(0) === '/' ? this.protocol + "://" + this.host + path : path,
}, opts);
this.request(opts, callback);
};
RequestClient.prototype.get = function(path, opts, callback){
if (typeof opts === 'function'){
callback = opts;
opts = {};
}
opts = extend({
method: "GET",
url: path.charAt(0) === '/' ? this.protocol + "://" + this.host + path : path,
}, opts);
this.request(opts, callback);
};
module.exports = RequestClient;