-
Notifications
You must be signed in to change notification settings - Fork 70
/
main.js
76 lines (67 loc) · 1.77 KB
/
main.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
var https = require('https');
var querystring = require('querystring');
var trello = function(key, token) {
this.key = key;
this.token = token;
this.host = "api.trello.com";
};
trello.prototype.invokeGeneric = function(method, apiCall, args, callback) {
if (!callback) {
// allow args to be optional and callback passed in its position.
callback = args;
args = {};
} else {
args = args || {};
}
var options = {
host: this.host,
port: 443,
path: apiCall,
method: method
};
if(this.key) {
args["key"] = this.key;
}
if(this.token) {
args["token"] = this.token;
}
if (method == 'GET') {
options.path += "?" + querystring.stringify(args);
} else {
post_data = querystring.stringify(args);
options.headers = { 'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length };
}
var req = https.request(options, function(res) {
res.setEncoding('utf8');
var data = "";
res.on('data', function(d) {
data += d;
});
res.on("end", function() {
if(res.statusCode !== 200) {
callback(data);
} else {
var j = JSON.parse(data);
callback(false, j);
}
});
});
if (method != 'GET') {
req.write(post_data);
}
req.end();
req.on('error', function(e) {
throw e;
});
};
trello.prototype.get = trello.prototype.api = function(apiCall, args, callback) {
return this.invokeGeneric('GET', apiCall, args, callback);
};
trello.prototype.post = function(apiCall, args, callback) {
return this.invokeGeneric('POST', apiCall, args, callback);
};
trello.prototype.put = function(apiCall, args, callback) {
return this.invokeGeneric('PUT', apiCall, args, callback);
};
exports = module.exports = trello;