-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (34 loc) · 1.54 KB
/
utils.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 requests
import json
def get(payload, endpoint, api_url, api_token):
print("Doing authenticated GET request to the API")
print(endpoint)
headers = {'User-Agent': 'python-requests', 'Content-Type': 'application/json',
'Authorization': api_token}
result = requests.get('/'.join((api_url, endpoint)), json=payload, headers=headers)
status_code, result = result.status_code, result.text
print(f"Status code: {status_code}")
if status_code != 200:
print(result)
return json.loads(result)
def delete(payload, endpoint, api_url, api_token):
print("Doing authenticated DELETE request to the API")
print(endpoint)
headers = {'User-Agent': 'python-requests', 'Content-Type': 'application/json',
'Authorization': api_token}
result = requests.delete('/'.join((api_url, endpoint)), json=payload, headers=headers)
status_code, result = result.status_code, result.text
print(f"Status code: {status_code}")
if status_code != 200:
print(result)
return json.loads(result)
def post(payload, endpoint, api_url, api_token):
print("Doing authenticated POST request to the API")
headers = {'User-Agent': 'python-requests', 'Content-Type': 'application/json',
'Authorization': api_token}
result = requests.post('/'.join((api_url, endpoint)), json=payload, headers=headers)
status_code, result = result.status_code, result.text
print(f"Status code: {status_code}")
if status_code != 200:
print(result)
return json.loads(result)