forked from chkgk/otree-hr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotree_api.py
46 lines (32 loc) · 1.04 KB
/
otree_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
41
42
43
44
45
46
import requests # pip3 install requests
from pprint import pprint
import requests.exceptions
import urllib.parse
GET = requests.get
POST = requests.post
class BaseOTreeApiError(Exception):
pass
class OTree4xxOr5xx(BaseOTreeApiError):
pass
class OTree4xx(OTree4xxOr5xx):
pass
class OTree5xx(OTree4xxOr5xx):
pass
class OTreeServerUnreachable(BaseOTreeApiError):
pass
def call_api(site_url, rest_key, method, *path_parts, **params) -> dict:
path = '/api/' + '/'.join(path_parts)
url = urllib.parse.urljoin(site_url, path)
try:
resp = method(url, json=params, headers={'otree-rest-key': rest_key})
except requests.exceptions.RequestException as exc:
raise OTreeServerUnreachable(f'Could not reach your oTree site at {site_url}')
if not resp.ok:
msg = (
f'Request to "{url}" failed '
f'with status code {resp.status_code}: {resp.text}'
)
if resp.status_code >= 500:
raise OTree5xx(msg)
raise OTree4xx(msg)
return resp.json()