-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.py
59 lines (49 loc) · 1.59 KB
/
http.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
51
52
53
54
55
56
57
58
59
import json
import requests
from . import config
from .decorators import jsonargs
@jsonargs
def get(url, params=None):
headers = {'Authorization': 'Bearer %s' % config.api_token()}
r = requests.get(url, headers=headers) if not params else \
requests.get(url, headers=headers, params=params)
if not r.ok:
r.raise_for_status()
return r.json()
@jsonargs
def post(url, params=None, data=None):
headers = {'Authorization': 'Bearer %s' % config.api_token()}
if params and data:
r = requests.post(url, headers=headers, params=params, data=data)
elif params:
r = requests.post(url, headers=headers, params=params)
elif data:
r = requests.post(url, headers=headers, data=data)
else:
r = requests.post(url, headers=headers)
if not r.ok:
r.raise_for_status()
return r.json()
@jsonargs
def patch(url, params=None, data=None):
headers = {'Authorization': 'Bearer %s' % config.api_token()}
if params and data:
r = requests.patch(url, headers=headers, params=params, data=data)
elif params:
r = requests.patch(url, headers=headers, params=params)
elif data:
r = requests.patch(url, headers=headers, data=data)
else:
r = requests.patch(url, headers=headers)
if not r.ok:
r.raise_for_status()
return r.json()
def delete(url):
headers = {'Authorization': 'Bearer %s' % config.api_token()}
r = requests.delete(url, headers=headers)
if not r.ok:
r.raise_for_status()
try:
return r.json()
except json.JSONDecodeError:
return r.text