Skip to content

Instantly share code, notes, and snippets.

@georg90
Created June 8, 2016 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save georg90/99f5685b6e21679269fc976e26588aac to your computer and use it in GitHub Desktop.
Save georg90/99f5685b6e21679269fc976e26588aac to your computer and use it in GitHub Desktop.
HVV API Geofox proof of concept
#!/usr/bin/python
# coding: utf8
### imports (Pakete evtl. mit "sudo pip install --upgrade requests" installieren!)
import requests
import hmac
import base64
import hashlib
import json
from requests.auth import AuthBase
### Config
requestType = "checkName" # M�gliche Typen siehe 1.7 Funktions�bersicht im Handbuch
username = "" # HVV Zugang
password = "" # HVV Zugang
url = "http://api-test.geofox.de/gti/public/"
requestUrl = url + requestType
### Auth erstellen
class HVVAuth(AuthBase):
def __init__(self, payloadFunc):
# setup any auth-related data here
print(json.dumps(payloadFunc))
self.sig = base64.b64encode(hmac.new(password.encode("UTF-8"), json.dumps(payloadFunc).encode('UTF-8'), hashlib.sha1).digest())
print(hmac.new(password.encode("UTF-8"), json.dumps(payloadFunc).encode('UTF-8'), hashlib.sha1).digest());
print(self.sig);
def __call__(self, r):
# modify and return the request
r.headers['geofox-auth-signature'] = self.sig
r.headers['geofox-auth-user'] = username
return r
### Request an die API stellen (siehe Handbuch)
payload = {
"coordinateType": "EPSG_4326",
"maxList": '1',
"theName": {
"name": "Informatikum",
"type": "STATION"
}
}
print(payload)
### tats�chlichen Request abschicken
req = requests.post(requestUrl, json=payload, auth=HVVAuth(payload))
### Consolen Ausgabe
print 'HTTP Headers:' # debug only
print req.headers
print 'HTTP Code: ' + str(req.status_code) # 200 = OK, 401 = Unauthorisiert etc.
print req.json() # HTTP Request Output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment