Last active
August 29, 2015 14:09
-
-
Save trevorgerhardt/1a96116b5157d51ab50e to your computer and use it in GitHub Desktop.
MWCOG API Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Valid request | |
curl "http://localhost:8085/api/availableCarpools?key=SECRET&origin=-77.00591535980521,38.90512185099374&destination=-77.06398575433832,38.86583364990412&departureTime=07:00,08:00&returnTime=17:00,18:00" | |
# Invalid parameters | |
curl "http://localhost:8085/api/availableCarpools?key=SECRET&origin=asdfasdf" | |
# Valid request, but no API key | |
curl "http://localhost:8085/api/availableCarpools?origin=-77.00591535980521,38.90512185099374&destination=-77.06398575433832,38.86583364990412&departureTime=07:00,08:00&returnTime=17:00,18:00" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"meta": { | |
"query": { | |
"departureTime": [ "07:00", "08:00" ], // time window | |
"destination": [ -77.06398575433832, 38.86583364990412 ], // [ longitude, latitude ] | |
"origin": [ -77.00591535980521, 38.90512185099374 ], // [ longitude, latitude ] | |
"returnTime": [ "17:00", "18:00" ], // time window | |
"searchRadius": 2000 // meters, assume it will be the same on both ends | |
}, | |
"requestDate": "Mon Nov 17 2014 12:39:28 GMT-0500 (EST)", // ISO date | |
"responseTime": 263, // milliseconds | |
"resource": "http://www.commuterconnections.org/api/availableCarpools", | |
"success": true | |
}, | |
"results": { | |
"carpools": 21, // total available carpools found based on the query | |
"vanpools": 6 // total available vanpools found based on the query | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var parse = require('url').parse; | |
var PORT = process.argv[2] || 0; | |
/** | |
* API server | |
*/ | |
var server = http.createServer(function(req, res) { | |
var url = parse(req.url, true); | |
var query = url.query; | |
var start = new Date(); | |
var meta = { | |
query: query, | |
requestDate: start, | |
resource: (url.host || '') + url.pathname | |
}; | |
/** | |
* Check if API key exists, authenticate internally | |
*/ | |
if (!query.key) { | |
meta.responseTime = new Date() - start; | |
meta.success = false; | |
res.writeHead(401, { 'Content-Type': 'application/json' }); | |
res.end(JSON.stringify({ | |
meta: meta, | |
error: 'Unauthorized access. Please pass in a valid API key.' | |
}, null, '\t')); | |
return; | |
} | |
/** | |
* Validate all the query parameters | |
*/ | |
try { | |
query.origin = query.origin.split(',').map(parseFloat); | |
query.destination = query.destination.split(',').map(parseFloat); | |
query.departureTime = query.departureTime.split(','); | |
query.returnTime = query.returnTime.split(','); | |
query.searchRadius = parseInt(query.searchRadius || 2000); | |
} catch (e) { | |
meta.responseTime = new Date() - start; | |
meta.success = false; | |
res.writeHead(400, { 'Content-Type': 'application/json' }); | |
res.end(JSON.stringify({ | |
meta: meta, | |
error: 'Invalid query parameters.' | |
}, null, '\t')); | |
return; | |
} | |
/** | |
* After cleaning up parameters, execute query to the Oracle server with the parameters and API key | |
*/ | |
commuterConnectionsResults(query, function(err, results) { | |
meta.responseTime = new Date() - start; | |
meta.success = !err; | |
if (err) { | |
res.writeHead(400, { 'Content-Type': 'application/json' }); | |
res.end(JSON.stringify({ | |
meta: meta, | |
error: err | |
}, null, '\t')); | |
} else { | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
res.end(JSON.stringify({ | |
meta: meta, | |
results: results | |
}, null, '\t')); | |
} | |
}); | |
}).listen(PORT, function() { | |
var address = server.address(); | |
console.log('listening on', address.port); | |
}); | |
/** | |
* Replace this function with the actual Oracle server query code | |
*/ | |
function commuterConnectionsResults(query, callback) { | |
setTimeout(function() { | |
callback(null, { | |
carpools: Math.random() * 50 | 0, | |
vanpools: Math.random() * 50 | 0 | |
}); | |
}, Math.random() * 50 | 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment