-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp.py
283 lines (246 loc) · 9.48 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
from urllib3.util import Retry
from requests import Session, Timeout
from requests.adapters import HTTPAdapter
from requests.exceptions import ConnectionError
from sotabenchapi.errors import (
HttpClientError,
HttpClientTimeout,
HttpRateLimitExceeded,
)
class HttpClient:
"""Generic requests handler.
Handles retries and HTTP errors.
"""
ERRORS = {
401: "Unauthorized",
403: "Forbidden!",
404: "Not found.",
429: "SotaBench under pressure! (Too many requests)",
500: "You broke SotaBench!!!",
502: "SotaBench server not reachable.",
503: "SotaBench server under maintenance.",
}
def __init__(
self,
url,
token="",
timeout=60,
max_retries=3,
backoff_factor=0.05,
backoff_max=10,
status_forcelist=(500, 502, 503, 504),
):
"""Initialize.
Args:
url (str): URL to the SotaBench server.
token (str): SotaBench authentication token.
timeout (int): Request timeout time.
max_retries (int): Maximal number of retries.
backoff_factor (float): Backoff factor.
backoff_max (int): Maximal number of backoffs.
status_forcelist (tuple of int): Tuple of HTTP statuses for
which the service should retry.
"""
self.url = url
self.token = token
self.timeout = timeout
self.max_retries = max_retries
self.backoff_factor = backoff_factor
self.backoff_max = backoff_max
self.status_forcelist = status_forcelist
# Setup headers
self.headers = {"Content-Type": "application/json"}
if self.token.strip() != "":
self.headers["Authorization"] = f"JWT {self.token}"
self.response = None
# setup connection pool
self.session = Session()
retry = Retry(
total=max_retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
retry.BACKOFF_MAX = backoff_max
adapter = HTTPAdapter(max_retries=retry)
self.session.mount(self.url, adapter)
def request(
self, method, url, headers=None, params=None, data=None, timeout=None
):
"""Request method.
Request method handles all the url joining, header merging, logging and
error handling.
Args:
method (str): Method for the request - GET or POST
url (str): Partial url of the request. It is added to the base url
headers (dict): Dictionary of additional HTTP headers
params (dict): Dictionary of query parameters for the request
data (dict): A JSON serializable Python object to send in the body
of the request. Used only in POST requests.
timeout (float): How many seconds to wait for the server to send
data before giving up.
"""
full_url = os.path.join(self.url, url.lstrip("/"))
headers = {**self.headers, **(headers or {})}
timeout = timeout or self.timeout
try:
if method.lower() == "get":
self.response = self.session.get(
url=full_url,
headers=headers,
params=params,
timeout=timeout,
)
elif method.lower() == "patch":
self.response = self.session.patch(
url=full_url,
headers=headers,
params=params,
json=data,
timeout=timeout,
)
elif method.lower() == "post":
self.response = self.session.post(
url=full_url,
headers=headers,
params=params,
json=data,
timeout=timeout,
)
elif method.lower() == "delete":
self.response = self.session.delete(
url=full_url,
headers=headers,
params=params,
json=data,
timeout=timeout,
)
else:
raise HttpClientError(f"Unsupported method: {method}")
except Timeout as e:
# If request timed out, let upper level handle it they way it sees
# fit one place might want to retry another might not.
raise HttpClientTimeout() from e
except ConnectionError as e:
raise HttpClientError("SotaBench server not reachable.") from e
except Exception as e:
raise HttpClientError(f"Unknown error. {e!r}") from e
if self.response.status_code == 200:
try:
return self.response.json() if self.response.text else {}
except Exception as e:
raise HttpClientError(
f"Error while parsing server response: {e!r}",
response=self.response,
) from e
# Check rate limit
limit = self.response.headers.get("X-Ratelimit-Limit", None)
if limit is not None:
remaining = self.response.headers["X-Ratelimit-Remaining"]
reset = self.response.headers["X-Ratelimit-Reset"]
retry = self.response.headers["X-Ratelimit-Retry"]
if remaining == 0:
raise HttpRateLimitExceeded(
response=self.response,
limit=limit,
remaining=remaining,
reset=reset,
retry=retry,
)
# Try known error messages
message = self.ERRORS.get(self.response.status_code, None)
if message is not None:
raise HttpClientError(message, response=self.response)
if self.response.status_code == 400:
try:
message = "\n".join(self.response.json()["errors"])
except Exception:
message = "Bad Request."
raise HttpClientError(message, response=self.response)
# Generalize unknown messages.
try:
message = self.response.json()["message"]
except Exception:
message = "Unknown error."
raise HttpClientError(message, response=self.response)
def get(self, url, headers=None, params=None, timeout=None):
"""Perform get request.
Args:
url (str): Partial url of the request. It is added to the base url
headers (dict): Dictionary of additional HTTP headers
params (dict): Dictionary of query parameters for the request
timeout (float): How many seconds to wait for the server to send
data before giving up
Returns:
dict: Deserialized json response.
"""
return self.request(
method="get",
url=url,
headers=headers,
params=params,
timeout=timeout,
)
def patch(self, url, headers=None, params=None, data=None, timeout=None):
"""Perform patch request.
Args:
url (str): Partial url of the request. It is added to the base url
headers (dict): Dictionary of additional HTTP headers
params (dict): Dictionary of query parameters for the request
data (dict): A JSON serializable Python object to send in the body
of the request.
timeout (float): How many seconds to wait for the server to send
data before giving up
Returns:
dict: Deserialized json response.
"""
return self.request(
method="patch",
url=url,
headers=headers,
params=params,
data=data,
timeout=timeout,
)
def post(self, url, headers=None, params=None, data=None, timeout=None):
"""Perform post request.
Args:
url (str): Partial url of the request. It is added to the base url
headers (dict): Dictionary of additional HTTP headers
params (dict): Dictionary of query parameters for the request
data (dict): A JSON serializable Python object to send in the body
of the request.
timeout (float): How many seconds to wait for the server to send
data before giving up
Returns:
dict: Deserialized json response.
"""
return self.request(
method="post",
url=url,
headers=headers,
params=params,
data=data,
timeout=timeout,
)
def delete(self, url, headers=None, params=None, data=None, timeout=None):
"""Perform delete request.
Args:
url (str): Partial url of the request. It is added to the base url
headers (dict): Dictionary of additional HTTP headers
params (dict): Dictionary of query parameters for the request
data (dict): A JSON serializable Python object to send in the body
of the request.
timeout (float): How many seconds to wait for the server to send
data before giving up
Returns:
dict: Deserialized json response.
"""
return self.request(
method="delete",
url=url,
headers=headers,
params=params,
data=data,
timeout=timeout,
)