redmine.js
3.31 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
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
var http = require('http');
var util = require('util');
var url = require('url');
var querystring = require('querystring');
var util = require('util');
function escapeJSONString(key, value) {
if (typeof value == 'string') {
return value.replace(/[^ -~\b\t\n\f\r"\\]/g, function(a) {
return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
});
}
return value;
}
function JSONStringify(data) {
return JSON.stringify(data, escapeJSONString).replace(/\\\\u([\da-f]{4}?)/g, '\\u$1');
}
/**
* Redmine
*/
function Redmine(config) {
if (!config.apiKey || !config.host) {
throw new Error("Error: apiKey and host must be configured.");
}
this.setApiKey(config.apiKey);
this.setHost(config.host);
}
Redmine.prototype.version = '0.2.3';
Redmine.JSONStringify = JSONStringify;
Redmine.prototype.setApiKey = function(key) {
this.apiKey = key;
};
Redmine.prototype.getApiKey = function() {
return this.apiKey;
};
Redmine.prototype.setHost = function(host) {
this.host = host;
};
Redmine.prototype.getHost = function() {
return this.host;
};
Redmine.prototype.generatePath = function(path, params) {
if (path.slice(0, 1) != '/') {
path = '/' + path;
}
return path + '?' + querystring.stringify(params);
};
Redmine.prototype.request = function(method, path, params, callback) {
if (!this.getApiKey() || !this.getHost()) {
throw new Error("Error: apiKey and host must be configured.");
}
var options = {
port:8080,
host: this.getHost(),
path: method == 'GET' ? this.generatePath(path, params) : path,
method: method,
headers: {
'X-Redmine-API-Key': this.getApiKey()
}
};
var req = http.request(options, function(res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
if (res.statusCode != 200 && res.statusCode != 201) {
callback({message: 'Server returns stats code: ' + res.statusCode, response: res}, null);
callback = null;
return ;
}
var body = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(e) {
var data = JSON.parse(body);
callback(null, data);
callback = null;
});
});
req.on('error', function(err) {
callback(err, null);
callback = null;
});
if (method != 'GET') {
var body = JSONStringify(params);
req.setHeader('Content-Length', body.length);
req.setHeader('Content-Type', 'application/json');
req.write(body);
}
req.end();
};
/**
* crud apis
*/
Redmine.prototype.getIssue = function(id, callback) {
if (typeof id == 'integer') {
throw new Error('Error: Argument #1 id must be integer');
}
this.request('GET', '/issues/' + id + '.json', {}, callback);
};
Redmine.prototype.getIssues = function(params, callback) {
this.request('GET', '/issues.json', params, callback);
};
Redmine.prototype.postIssue = function(params, callback) {
this.request('POST', '/issues.json', {issue: params}, callback);
};
Redmine.prototype.updateIssue = function(id, params, callback) {
this.request('PUT', '/issues/' + id + '.json', {issue: params}, callback);
};
Redmine.prototype.deleteIssue = function(id, callback) {
this.request('DELETE', '/issues/' + id + '.json', {}, callback);
};
/*
* Exports
*/
module.exports = Redmine;