mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com> Co-authored-by: Xingyao Wang <xingyao@all-hands.dev> Co-authored-by: Xingyao Wang <xingyao6@illinois.edu>
63 lines
1.2 KiB
Python
63 lines
1.2 KiB
Python
from typing import Any
|
|
|
|
import requests
|
|
from requests.exceptions import (
|
|
ChunkedEncodingError,
|
|
ConnectionError,
|
|
)
|
|
from urllib3.exceptions import IncompleteRead
|
|
|
|
|
|
def is_server_error(exception):
|
|
return (
|
|
isinstance(exception, requests.HTTPError)
|
|
and exception.response.status_code >= 500
|
|
)
|
|
|
|
|
|
def is_404_error(exception):
|
|
return (
|
|
isinstance(exception, requests.HTTPError)
|
|
and exception.response.status_code == 404
|
|
)
|
|
|
|
|
|
def is_429_error(exception):
|
|
return (
|
|
isinstance(exception, requests.HTTPError)
|
|
and exception.response.status_code == 429
|
|
)
|
|
|
|
|
|
def is_503_error(exception):
|
|
return (
|
|
isinstance(exception, requests.HTTPError)
|
|
and exception.response.status_code == 503
|
|
)
|
|
|
|
|
|
def is_502_error(exception):
|
|
return (
|
|
isinstance(exception, requests.HTTPError)
|
|
and exception.response.status_code == 502
|
|
)
|
|
|
|
|
|
DEFAULT_RETRY_EXCEPTIONS = [
|
|
ConnectionError,
|
|
IncompleteRead,
|
|
ChunkedEncodingError,
|
|
]
|
|
|
|
|
|
def send_request(
|
|
session: requests.Session,
|
|
method: str,
|
|
url: str,
|
|
timeout: int = 10,
|
|
**kwargs: Any,
|
|
) -> requests.Response:
|
|
response = session.request(method, url, **kwargs)
|
|
response.raise_for_status()
|
|
return response
|