-
Notifications
You must be signed in to change notification settings - Fork 0
/
wadata.py
50 lines (40 loc) · 1.74 KB
/
wadata.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
41
42
43
44
45
46
47
48
49
50
import auth
import logging
from time import sleep
import json
logger = logging.getLogger(__name__)
def call_api(category, filter_string=None, select_string=None, event_id=None, asynchronous=False):
oauth_session = auth.get_oauth_session()
url = f"{auth.WA_API_PREFIX}/{category}"
params = []
if not asynchronous:
params.append(("$async", "false"))
if filter_string:
params.append(("$filter", filter_string))
if select_string:
params.append(("$select", select_string))
if event_id:
params.append(("eventId", str(event_id)))
logger.debug(f"API url will be: {url}")
logger.debug(f"API parameters will be: {params}")
request = oauth_session.get(url = url, params = params)
if request.status_code != 200:
if request.status_code == 401:
logger.debug("API call failed with 401, refreshing token and trying again")
auth.refresh_token()
oauth_session = auth.get_oauth_session()
elif request.status_code == 429:
logger.debug("API call failed with 429, sleeping for 10 seconds and trying again")
sleep(10)
else:
raise Exception(f"API call failed with {request.status_code} {request.text}")
# did we get it?
if request.status_code != 200:
logger.debug("Trying API call again.")
request = oauth_session.get(url = url, params = params)
if request.status_code != 200:
raise Exception(f"Repeated API call failed with {request.status_code} {request.text}")
logger.debug(f"API call successful, returning data.")
logger.debug("*************************************")
logger.debug(f"{json.dumps(request.json(), indent=4)}")
return request.json()