-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
40 lines (32 loc) · 1.3 KB
/
index.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
var b64 = require('b64');
var request = require('request');
function EloquaRequest(site, user, password, baseUrl) {
this.baseUrl = baseUrl || 'https://secure.eloqua.com';
this.credential = b64.encode(site + "\\" + user + ":" + password);
}
EloquaRequest.prototype.request = function(url, options, callback) {
var _this = this;
var requestOptions = { url: _this.baseUrl + url, headers: { Authorization: "Basic " + _this.credential }, json: true };
for (var key in options) {
var val = options[key];
requestOptions[key] = val;
}
return request(requestOptions, function(err, response, body) {
if (err !== null) return callback(err, null);
if (response.statusCode !== 200 && response.statusCode !== 201) return callback({ code: response.statusCode, msg: body }, null);
return callback(null, body);
});
};
EloquaRequest.prototype.get = function(uri, callback) {
this.request(uri, null, callback);
};
EloquaRequest.prototype.put = function(uri, data, callback) {
this.request(uri, { method: "PUT", body: data }, callback);
};
EloquaRequest.prototype.post = function(uri, data, callback) {
this.request(uri, { method : "POST", body: data }, callback);
};
EloquaRequest.prototype.remove = function(uri, callback) {
this.request(uri, { method: "DELETE" }, callback);
};
module.exports = EloquaRequest;