Created
June 30, 2014 02:53
-
-
Save mattlenz/2ec8c47dccf0ee80cf68 to your computer and use it in GitHub Desktop.
PTV API Proxy
This file contains 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 Hapi = require('hapi'); | |
var server = module.exports = Hapi.createServer('localhost', 8000); | |
var credentials = { | |
ptv: { | |
devid: 'YOUR_PTV_DEVID', | |
key: 'YOUR_PTV_KEY' | |
} | |
} | |
var crypto = require('crypto'); | |
var request = require('request'); | |
var qs = require('qs'); | |
const PTV_API_BASE = 'http://timetableapi.ptv.vic.gov.au'; | |
server.route({ | |
method: 'GET', | |
path: '/{path*}', | |
handler: { | |
directory: { | |
path: './public', | |
listing: false, | |
index: true | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/ptv/{path*}', | |
handler: function(req, reply) { | |
var endpoint = '/v2/' + req.params.path + '?' + qs.stringify({ | |
devid: credentials.ptv.devid, | |
timestamp: (new Date()).toISOString() | |
}); | |
var signature = crypto.createHmac('sha1', credentials.ptv.key).update(endpoint).digest('hex'); | |
var url = PTV_API_BASE + endpoint + '&' + qs.stringify({ signature: signature.toUpperCase() }); | |
console.log(url); | |
reply(request({ url: url, json: true })); | |
} | |
}); | |
server.start(function() { | |
console.log('Server started at', server.info.uri); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment