From 350f584e605bfcc87b6119ef6a96a1251c55180b Mon Sep 17 00:00:00 2001 From: muddydixon Date: Wed, 2 Dec 2015 17:58:40 +0900 Subject: [PATCH] added configration * prefix: redmine subdirectory path * protocol: http: or https: * port: redmine server port --- lib/redmine.js | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/redmine.js b/lib/redmine.js index c57efdf..835402d 100644 --- a/lib/redmine.js +++ b/lib/redmine.js @@ -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') { @@ -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'; @@ -48,6 +51,30 @@ 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; @@ -55,6 +82,16 @@ Redmine.prototype.generatePath = function(path, params) { 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."); @@ -62,14 +99,14 @@ Redmine.prototype.request = function(method, path, params, callback) { 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));