-
Notifications
You must be signed in to change notification settings - Fork 9
OTProxy
OTProxy is used to map OTP 'plan' requests from a common API endpoint to the appropriate graph on a collection of one or more OTP servers. The script uses the specified fromPlace and toPlace coordinates to match requests to the appropriate graph (if any exists). OTProxy also provides support for API keys and HMAC-based authentication using a shared secret.
OTProxy will be run on the machine that hosts the common API endpoint. All files from the OTPSetup/otproxy directory should be copied to this server.
You will also want to install Cube, which is very straightforward.
OTProxy needs to know where to look to access the get_servers function of the OTPSetup controller instance. Currently this is hard-coded in line 33 of otproxy.py.
A file, secrets.txt, should be created in the same directory as the OTProxy scripts, mapping API keys to the shared secret keys, e.g.:
my_key:my_secret
The value "test" for the key means that signatures will not be checked; this makes ad-hoc testing easier.
To start the proxy, simply run 'python otproxy.py'. By default it listens on port 1234. You may want to run Nginx (or similar) in front of OTProxy to route relevant incoming HTTP requests to it.
Trip plan requests to the common endpoint are constructed using the same basic structure and parameters used when querying an OTP instance directly, e.g.:
http://your_proxy_server/opentripplanner-api-webapp/ws/plan?fromPlace=...
(Do not include the routerId parameter; it will be added by the proxy provided the search locations were successfully matched to a graph.)
In addition, two other parameters related to API key authentication must be added to any 'plan' query. One, 'apiKey', is the identifier of the API key, and should match one of the API keys specified on the server in secrets. txt. The other, 'signature', represents the HMAC (md5-based) signature of the combined values of the apiKey, fromPlace, and toPlace parameters, signed using the secret key corresponding to that API key.
The following python script can be used to test the client connection:
import requests, sys, urllib, hmac
proxyServer = "http://your_proxy_server"
apiKey = "your_api_key"
secret = "your_secret"
fr = sys.argv[1]
to = sys.argv[2]
h = hmac.new(secret)
h.update(apiKey)
h.update(fr)
h.update(to)
sig = h.hexdigest()
fr = urllib.quote_plus(fr)
to = urllib.quote_plus(to)
url = '%s/opentripplanner-api-webapp/ws/plan?fromPlace=%s&toPlace=%s&apiKey=%s&signature=%s' % (proxyServer, fr, to, apiKey, sig)
r = requests.get(url)
print r.status_code
print r.text
To run, simply pass in the lat/lon coordinates of the start and end locations:
python proxy.py lat1,lon1 lat2,lon2