Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions lib/redmine.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var http = require('http');
var https = require('https');
var util = require('util');
var url = require('url');
var querystring = require('querystring');
var util = require('util');

function escapeJSONString(key, value) {
if (typeof value == 'string') {
Expand All @@ -26,6 +26,9 @@ function Redmine(config) {

this.setApiKey(config.apiKey);
this.setHost(config.host);
this.setProtocol(config.protocol);
this.setPort(config.port);
this.setPrefix(config.prefix);
}

Redmine.prototype.version = '0.2.3';
Expand All @@ -48,28 +51,62 @@ Redmine.prototype.getHost = function() {
return this.host;
};

Redmine.prototype.setPort = function(port) {
this.port = port;
};

Redmine.prototype.getPort = function() {
return this.port;
};

Redmine.prototype.setPrefix = function(prefix) {
this.prefix = prefix;
};

Redmine.prototype.getPrefix = function() {
return this.prefix;
};

Redmine.prototype.setProtocol = function(protocol) {
this.protocol = protocol;
};

Redmine.prototype.getProtocol = function() {
return this.protocol;
};

Redmine.prototype.generatePath = function(path, params) {
if (path.slice(0, 1) != '/') {
path = '/' + path;
}
return path + '?' + querystring.stringify(params);
};

Redmine.prototype.detectProtocol = function() {
var protocol = {
"https:": https,
"http:": http
};
if(this.getProtocol()) return protocol[this.getProtocol()];
if(this.getPort() === 443) return protocol["https:"];
return protocol["http:"];
};

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 = {
host: this.getHost(),
path: method == 'GET' ? this.generatePath(path, params) : path,
path: (this.getPrefix() || "") + (method == 'GET' ? this.generatePath(path, params) : path),
method: method,
headers: {
'X-Redmine-API-Key': this.getApiKey()
}
};

var req = http.request(options, function(res) {
var req = this.detectProtocol().request(options, function(res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));

Expand Down