Skip to content

Instantly share code, notes, and snippets.

@readytheory
Created June 7, 2020 15:17
Show Gist options
  • Save readytheory/8c429cfa510788f5a6976fd53d072d51 to your computer and use it in GitHub Desktop.
Save readytheory/8c429cfa510788f5a6976fd53d072d51 to your computer and use it in GitHub Desktop.
Call here.com from AWS lambda -- Part 1 urllib instead of requests --
# AWS doesn't allow use of request library we'd normally use, so sub out urllib
# Here, we alredy have a bearer token. That gets replaced daily, want to schedule it into parameter store
# We'll also write the data to s3 instead of returning it
import urllib.request as UR
import json
url = "https://traffic.ls.hereapi.com/traffic/6.2/flow.xml?bbox=38.93036,-77.17164;38.93154,-77.16965"
auth_info = {"access_token":"btJhbGciOiJSXlKaGJHY2lPaUprYVMamAMiAnk5TElUOXpRHAyUjRQRjZMdjGLUQREX1dnTk4wbG. . . .1GUvAlJAXe_qbJoJydw","token_type":"bearer","expires_in":86399}
# to make the bearer token see https://gist.github.com/readytheory/002944329327675bb2a32fbb7cae9bd7
headers = {'Authorization': 'Bearer ' + auth_info["access_token"]}
req = UR.Request(url=url, data=None, headers=headers)
flow_result = UR.urlopen(req)
if flow_result.status != 200:
raise RuntimeError("Status code for headers != 200")
print(flow_result.status)
return json.dumps(flow_result.read().decode(encoding='UTF-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment