1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- var https = require('https');
- class Trello {
- constructor() {
- this.BASEURL = "https://trello.com/1/";
- }
-
- reportBug(bugmessage, callback) {
- var bugData = {
- "idList": config.trelloBugListId,
- "name": bugmessage
- };
- this.makeRequest("cards", "post", bugData, callback)
- }
- requestFeature(featuremessage, callback) {
- var featureData = {
- "idList": config.trelloFeatureListId,
- "name": featuremessage
- };
- this.makeRequest("cards", "post", featureData, callback)
- }
- requestBugList(callback) {
- this.makeRequest("lists/" + config.trelloBugListId + "/cards?fields=id,pos,name", "get", null, callback);
- }
- requestFeatures(callback) {
- this.makeRequest("lists/" + config.trelloFeatureListId + "/cards?fields=id,pos,name", "get", null, callback);
- }
- addAuth(url) {
- if(typeof url == "object") {
- url["key"] = config.trelloApiKey;
- url["token"] = config.trelloAccessToken;
- return url;
- }
- if(url.indexOf("?") != -1) {
- return url + "&key="+config.trelloApiKey + "&token=" + config.trelloAccessToken;
- }
- return url + "?key="+config.trelloApiKey + "&token=" + config.trelloAccessToken;
-
- }
- makeRequest(path, method, data, callback) {
- var postData = "";
- if(data != null) {
- postData = [];
- for(var i in data) {
- postData.push(i + "=" + data[i]);
- }
- postData = this.addAuth(postData);
- postData = postData.join("&");
- } else {
- path = this.addAuth(path);
- }
- var options = {
- hostname: 'trello.com',
- port: 443,
- path: '/1/' + path,
- method: method.toUpperCase(),
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': postData.length
- }
- };
- var req = https.request(options, (res) => {
- var buffer = "";
- res.on('data', (chunk) => {
- buffer += chunk;
- });
- res.on('end', () => {
- try {
- callback("success", JSON.parse(buffer));
- } catch(error) {
- console.error("Trello response is invalid JSON", buffer);
- callback("error", {"error": buffer});
- }
- });
- res.on('error', () => {
- console.error("Trello request failed.");
- });
- });
- req.on('error', (e) => {
- console.error(e);
- });
- console.log(postData);
- req.write(postData);
- req.end();
- }
- }
- module.exports = new Trello();
|