|
27 | 27 | import requests |
28 | 28 | import validators |
29 | 29 | from requests.adapters import HTTPAdapter, Retry |
| 30 | +from requests.exceptions import ReadTimeout |
| 31 | +from requests.exceptions import ConnectionError |
| 32 | +from requests.exceptions import Timeout |
| 33 | +from requests.exceptions import HTTPError |
| 34 | +from requests.exceptions import RequestException |
| 35 | +from datetime import datetime |
30 | 36 | from tqdm import tqdm |
31 | 37 | from tqdm.asyncio import tqdm as tqdm_async |
32 | 38 |
|
@@ -261,14 +267,46 @@ def post( |
261 | 267 |
|
262 | 268 | for attempt in range(MAX_NB_RETRIES): |
263 | 269 | begin = perf_counter() |
264 | | - response = self.session.post( |
265 | | - url, |
266 | | - headers=full_headers, |
267 | | - stream=True, |
268 | | - timeout=REQUESTS_TIMEOUT, |
269 | | - data=json.dumps(data), |
270 | | - verify=self.verify, |
271 | | - ) |
| 270 | + try: |
| 271 | + response = self.session.post( |
| 272 | + url, |
| 273 | + headers=full_headers, |
| 274 | + stream=True, |
| 275 | + timeout=REQUESTS_TIMEOUT, |
| 276 | + data=json.dumps(data), |
| 277 | + verify=self.verify, |
| 278 | + ) |
| 279 | + except ReadTimeout: |
| 280 | + if attempt == MAX_NB_RETRIES - 1: |
| 281 | + raise Exception("Request timed out after multiple retries") |
| 282 | + logger.debug(f"{datetime.now()} : ReadTimeout occurred, retrying...") |
| 283 | + time.sleep(TIME_BEFORE_RETRY) |
| 284 | + continue |
| 285 | + except ConnectionError: |
| 286 | + if attempt == MAX_NB_RETRIES - 1: |
| 287 | + raise Exception("Connection error: Could not connect to the server after multiple retries") |
| 288 | + logger.debug(f"{datetime.now()} : Connection error, retrying...") |
| 289 | + time.sleep(TIME_BEFORE_RETRY) |
| 290 | + continue |
| 291 | + except Timeout: |
| 292 | + if attempt == MAX_NB_RETRIES - 1: |
| 293 | + raise Exception("Request timeout occurred after multiple retries") |
| 294 | + logger.debug(f"{datetime.now()} : Timeout occurred, retrying...") |
| 295 | + time.sleep(TIME_BEFORE_RETRY) |
| 296 | + continue |
| 297 | + except RequestException as e: |
| 298 | + if attempt == MAX_NB_RETRIES - 1: |
| 299 | + raise Exception(f"Request failed: {str(e)} after multiple retries") |
| 300 | + logger.debug("Request exception occurred, retrying...") |
| 301 | + time.sleep(TIME_BEFORE_RETRY) |
| 302 | + continue |
| 303 | + except Exception as e: |
| 304 | + if attempt == MAX_NB_RETRIES - 1: |
| 305 | + raise Exception(f"Unexpected error: {str(e)} after multiple retries") |
| 306 | + logger.debug("Unexpected error occurred, retrying...") |
| 307 | + time.sleep(TIME_BEFORE_RETRY) |
| 308 | + continue |
| 309 | + |
272 | 310 | end = perf_counter() |
273 | 311 | logger.debug(f"request made in {end-begin} seconds") |
274 | 312 |
|
|
0 commit comments