mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-04-29 03:00:45 -04:00
39 lines
971 B
Python
39 lines
971 B
Python
from typing import Any
|
|
|
|
import requests
|
|
|
|
|
|
class RequestHTTPError(requests.HTTPError):
|
|
"""Exception raised when an error occurs in a request with details."""
|
|
|
|
def __init__(self, *args, detail=None, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.detail = detail
|
|
|
|
def __str__(self) -> str:
|
|
s = super().__str__()
|
|
if self.detail is not None:
|
|
s += f'\nDetails: {self.detail}'
|
|
return s
|
|
|
|
|
|
def send_request(
|
|
session: requests.Session,
|
|
method: str,
|
|
url: str,
|
|
timeout: int = 10,
|
|
**kwargs: Any,
|
|
) -> requests.Response:
|
|
response = session.request(method, url, **kwargs)
|
|
try:
|
|
response.raise_for_status()
|
|
except requests.HTTPError as e:
|
|
try:
|
|
_json = response.json()
|
|
except requests.JSONDecodeError:
|
|
raise e
|
|
raise RequestHTTPError(
|
|
e, response=e.response, detail=_json.get('detail')
|
|
) from e
|
|
return response
|