Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (84 loc) · 2.71 KB
/
Copy pathindex.js
File metadata and controls
108 lines (84 loc) · 2.71 KB
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
var _ = require('lodash');
var request = require('request');
function Blimp(credentials) {
this.appId = credentials.appId;
this.apiKey = credentials.apiKey;
this.secret = credentials.secret;
this.username = credentials.username;
this.headers = {
'content-type': 'application/json',
'authorization': 'ApiKey ' + this.username + ':' + this.apiKey,
'x_blimp_appid': this.appId,
'x_blimp_secret': this.secret
};
this.url = 'https://app.getblimp.com/api/v2/';
}
Blimp.prototype.get = function(resource, id, params, callback) {
var options = {
url: this.url + resource + '/',
headers: this.headers
};
if (arguments.length === 2) {
if (_.isFunction(id)) callback = id;
}
if (arguments.length === 3) {
callback = params;
if (_.isPlainObject(id)) params = id;
}
if (arguments.length === 4) options.qs = params;
if (_.isNumber(id)) options.url += id + '/';
request.get(options, function(error, response, body) {
if (!error && callback) callback(body);
});
};
Blimp.prototype.post = function(resource, data, params, callback) {
var options = {
url: this.url + resource + '/',
body: JSON.stringify(data),
headers: this.headers
};
if (arguments.length === 3) callback = params;
if (arguments.length === 4) options.qs = params;
request.post(options, function(error, response, body) {
if (!error && callback) callback(body);
});
};
Blimp.prototype.put = function(resource, id, params, data, callback) {
var options = {
url: this.url + resource + '/' + id + '/',
body: JSON.stringify(data),
headers: this.headers
};
if (arguments.length === 4) {
callback = data;
data = params;
options.body = JSON.stringify(data);
}
if (arguments.length === 5) options.qs = params;
request.put(options, function(error, response, body) {
if (!error && callback) callback(body);
});
};
Blimp.prototype.del = function(resource, id, callback) {
var options = {
url: this.url + resource + '/',
headers: this.headers
};
if (arguments.length === 2) {
if (_.isFunction(id)) callback = id;
}
if (_.isNumber(id)) options.url += id + '/';
request.del(options, function(error, response, body) {
if (!error && callback) callback(body);
});
};
Blimp.prototype.schema = function(resource, callback) {
var options = {
url: this.url + resource + '/schema',
headers: this.headers
};
request.get(options, function(error, response, body) {
if (!error && callback) callback(body);
});
};
module.exports = Blimp;