Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Improve API error handling, update example config
Browse files Browse the repository at this point in the history
  • Loading branch information
i-like-robots committed Jun 30, 2014
1 parent f5fc8c1 commit bff421e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/browser/bootstrap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var React = require("react");
var networkData = require("../common/data");
var TubeTracker = require("../component/tube-tracker.jsx");

window.app = (function(scope) {
window.app = (function() {
var requiredFeatures = {
"JSON decoding": window.JSON,
"the selectors API": document.querySelector,
Expand Down
24 changes: 18 additions & 6 deletions app/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ APIRequest.prototype.for = function(line, station) {

APIRequest.prototype.get = function(callback) {
if (!utils.isStationOnLine(this.line, this.station, networkData)) {
callback(null, null);
return callback(null, "");
}

var formatCallback = this.format.bind(this);
Expand All @@ -23,8 +23,7 @@ APIRequest.prototype.get = function(callback) {

var options = {
path: path + queryString,
hostname: "api.beta.tfl.gov.uk",
headers: { "Content-Type": "application/json" }
hostname: "api.beta.tfl.gov.uk"
};

var request = http.request(options, function(response) {
Expand All @@ -37,7 +36,14 @@ APIRequest.prototype.get = function(callback) {
});

response.on("end", function() {
callback(null, formatCallback(str));
var responseJSON = formatCallback(str);

if (responseJSON instanceof Error) {
callback(responseJSON, null);
}
else {
callback(null, responseJSON);
}
});
});

Expand All @@ -48,7 +54,13 @@ APIRequest.prototype.get = function(callback) {
request.end();
};

APIRequest.prototype.format = function(responseText) {
APIRequest.prototype.format = function(responseText, callback) {
var parsedResponse = parseResponse(responseText);

if (parsedResponse instanceof Error) {
return parsedResponse;
}

return {
request: {
lineCode: this.line,
Expand All @@ -58,7 +70,7 @@ APIRequest.prototype.format = function(responseText) {
lineName: networkData.lines[this.line],
stationName: networkData.stations[this.station]
},
platforms: formatData(parseResponse(responseText))
platforms: formatData(parsedResponse)
};
};

Expand Down
4 changes: 2 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"API_KEY": "",
"API_SECRET": ""
"APP_ID": "",
"APP_KEY": ""
}

0 comments on commit bff421e

Please sign in to comment.