This repository was archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
53 lines (42 loc) · 1.34 KB
/
api.py
File metadata and controls
53 lines (42 loc) · 1.34 KB
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
from json import JSONDecodeError
import requests
class Api:
url: str = ""
password: str = ""
username: str = ""
verify: bool = True
def __init__(
self, url: str, password: str, username: str, verify: bool = True
) -> None:
self.url = url
self.password = password
self.username = username
self.verify = verify
requests.packages.urllib3.disable_warnings()
def get(self, endpoint: str) -> list:
r = requests.get(
url=f"{self.url}/{endpoint}",
auth=(self.username, self.password),
verify=self.verify,
)
result = r.json()
if type(result) is dict:
return [result]
return result
def post(self, endpoint: str, payload: dict, statuscode: int = 201) -> dict:
r = requests.post(
url=f"{self.url}/{endpoint}",
auth=(self.username, self.password),
json=payload,
verify=self.verify,
)
# Catch error when r.json() is not available
try:
result = r.json()
except JSONDecodeError:
result = {}
if r.status_code != statuscode:
print("Failed!")
# add a dict key to identify if the result is an error message
result["is_failed"] = True
return result