forked from burnerlee/compextAI-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
75 lines (70 loc) · 2.56 KB
/
Copy pathapi.py
File metadata and controls
75 lines (70 loc) · 2.56 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
class APIClient:
"""
A class to make HTTP requests to the Compext AI API.
"""
def __init__(self, base_url:str, api_key:str, timeout:int=10, retries:int=3):
self.base_url = base_url + "/api/v1"
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
self.timeout = timeout
self.retries = retries
# set timeout for all the requests and retry if the request times out
def get(self, route:str, data:dict={},**kwargs):
for _ in range(self.retries):
try:
response = requests.get(self.base_url + route, headers=self.headers, json=data, timeout=self.timeout, **kwargs)
return {
"status": response.status_code,
"data": response.json()
}
except requests.exceptions.Timeout:
continue
return {
"status": 500,
"data": {}
}
def post(self, route:str, data:dict={},**kwargs):
for _ in range(self.retries):
try:
response = requests.post(self.base_url + route, headers=self.headers, json=data,timeout=self.timeout, **kwargs)
return {
"status": response.status_code,
"data": response.json()
}
except requests.exceptions.Timeout:
continue
return {
"status": 500,
"data": {}
}
def put(self, route:str, data:dict={},**kwargs):
for _ in range(self.retries):
try:
response = requests.put(self.base_url + route, headers=self.headers, json=data,timeout=self.timeout, **kwargs)
return {
"status": response.status_code,
"data": response.json()
}
except requests.exceptions.Timeout:
continue
return {
"status": 500,
"data": {}
}
def delete(self, route:str, data:dict={},**kwargs):
for _ in range(self.retries):
try:
response = requests.delete(self.base_url + route, headers=self.headers, json=data,timeout=self.timeout, **kwargs)
return {
"status": response.status_code,
"data": response.json()
}
except requests.exceptions.Timeout:
continue
return {
"status": 500,
"data": {}
}