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
69 changes: 40 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
});
});
}
});
});

Expand Down