-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.js
151 lines (140 loc) · 3.46 KB
/
request.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
var superagent = require('superagent')
, Q = require('q');
exports = module.exports = Request;
function Request(baseUrl) {
this.baseUrl = addTrailingSlash(baseUrl);
}
Request.prototype.get = function(url) {
return Request.get(this.baseUrl + url);
}
Request.prototype.post = function(url, data) {
return Request.post(this.baseUrl + url, data);
}
Request.prototype.put = function(url, data) {
return Request.put(this.baseUrl + url, data);
}
Request.prototype.del = function(url) {
return Request.del(this.baseUrl + url);
}
var _headers = [];
exports.setHeader = function(key, value) {
_headers.push({
key: key,
value: value
});
}
//GET request
exports.get = function(url) {
var deferred = Q.defer();
var req = superagent
.get(url)
.set('Accept', 'application/json')
.set('X-Stream', 'true');
for(var i in _headers) {
var h = _headers[i];
req.set(h.key, h.value);
}
req.end(function(r) {
if (r && r.ok) {
if(r.body && r.body.error) {
deferred.reject(r.body);
} else {
deferred.resolve(r);
}
} else {
deferred.reject(new Error(r.text));
}
})
.on('error', function(err) {
deferred.reject(err);
});
return deferred.promise;
}
//POST request
exports.post = function(url, data) {
var deferred = Q.defer();
var req = superagent
.post(url)
.set('Accept', 'application/json')
.set('X-Stream', 'true');
for(var i in _headers) {
var h = _headers[i];
req.set(h.key, h.value);
}
req.send(data)
.end(function(r) {
if (r && r.ok) {
if(r.body && r.body.error) {
deferred.reject(r.body);
} else {
deferred.resolve(r);
}
} else {
deferred.reject(new Error(r.text));
}
})
.on('error', function(err) {
deferred.reject(err);
});
return deferred.promise;
}
//PUT request
exports.put = function(url, data) {
var deferred = Q.defer();
var req = superagent
.put(url)
.set('Accept', 'application/json')
.set('X-Stream', 'true');
for(var i in _headers) {
var h = _headers[i];
req.set(h.key, h.value);
}
req.send(data)
.end(function(r) {
if (r && r.ok) {
if(r.body && r.body.error) {
deferred.reject(r.body);
} else {
deferred.resolve(r);
}
} else {
deferred.reject(new Error(r.text));
}
})
.on('error', function(err) {
deferred.reject(err);
});
return deferred.promise;
}
//DELETE request
exports.del = function(url) {
var deferred = Q.defer();
var req = superagent
.del(url)
.set('Accept', 'application/json')
.set('X-Stream', 'true');
for(var i in _headers) {
var h = _headers[i];
req.set(h.key, h.value);
}
req.end(function(r) {
if (r && r.ok) {
if(r.body && r.body.error) {
deferred.reject(r.body);
} else {
deferred.resolve(r);
}
} else {
deferred.reject(new Error(r.text));
}
})
.on('error', function(err) {
deferred.reject(err);
});
return deferred.promise;
}
function addTrailingSlash(baseUrl) {
var lastChar = baseUrl.substr(baseUrl.length - 1, 1);
if(lastChar != '/') baseUrl += '/';
return baseUrl;
}