diff --git a/index.js b/index.js index baab1d7..7d7fc25 100644 --- a/index.js +++ b/index.js @@ -91,6 +91,12 @@ var Pivotal = function (token) { */ Pivotal.debug = false; +/* + Defaults to version 3 of the PivotalTracker API, + but can be changed to support newer versions of the API such as version 5 +*/ +Pivotal.apiVersion = 3; + /** ### pivotal.getToken : retrieve a user's token @@ -736,7 +742,7 @@ Pivotal.apiCall = function (method, pathSegments, query, data, file, cb, token) } // Build Request URL - var path = "/services/v3/" + pathSegments.join("/"), + var path = "/services/v" + Pivotal.apiVersion + "/" + pathSegments.join("/"), postData = null, options = null, req = null, @@ -807,7 +813,7 @@ Pivotal.apiCall = function (method, pathSegments, query, data, file, cb, token) content += chunk; }); - res.on('end', function(){ + res.on('end', function () { if (this.statusCode !== 200) { return cb({code: this.statusCode, desc: "API returned an HTTP error"}, null); @@ -817,34 +823,39 @@ Pivotal.apiCall = function (method, pathSegments, query, data, file, cb, token) return cb(null, null); } - parser.parseString(content, function (err, ret) { - - Pivotal.log("info", "Result:", content); - - if (err) { - - Pivotal.log("error", "Result:", content); - - err = { - "errors" : { - "error" : [ "Error while parsing PT HTTP service response", err ] - } - }; - } - - if (ret && ret.errors) { - err = ret; - ret = null; - } - - // - // We do this so that any error generated by the callback function - // will not bubble up and trigger the on('error') of the parser. - // - process.nextTick(function () { - cb(err, ret); + Pivotal.log("info", "Result:", content); + + try { + var jsonContent = JSON.parse(content); + cb(null, jsonContent); + } catch (e) { + parser.parseString(content, function (err, ret) { + + if (err) { + + Pivotal.log("error", "Result:", content); + + err = { + "errors": { + "error": [ "Error while parsing PT HTTP service response", err ] + } + }; + } + + if (ret && ret.errors) { + err = ret; + ret = null; + } + + // + // We do this so that any error generated by the callback function + // will not bubble up and trigger the on('error') of the parser. + // + process.nextTick(function () { + cb(err, ret); + }); }); - }); + } }); });