-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_api.py
40 lines (31 loc) · 1.17 KB
/
query_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import logging
import json
import os
import requests
import conf
AUTH_HEADERS = json.loads(os.environ.get('AUTH_HEADERS'))
def get_all_stations():
logging.debug("Requesting all stations...")
try:
result = requests.get(conf.GET_ALL_STATIONS_URL, headers=AUTH_HEADERS).json()
except json.decoder.JSONDecodeError:
raise Exception("Could not parse JSON response!")
stations = [(station["stationId"], f'{station["name"]} ({station["city"]})') for station in result if station["active"]]
logging.debug(f"Retrieved {len(stations)} stations")
return stations
def get_latest(station_id):
try:
result = requests.get(conf.GET_STATION_AVERAGE.format(station_id),
headers=AUTH_HEADERS).json()
except json.decoder.JSONDecodeError:
raise Exception("Could not parse JSON response!")
return result
def get_data():
results = []
logging.debug("Querying stations...")
for station_id, station_name in get_all_stations():
data = get_latest(station_id)
data[conf.NAME_KEY] = station_name
results.append(data)
logging.debug("Finished querying stations...")
return results