diff --git a/py_clob_client/client.py b/py_clob_client/client.py index e6be3c56..5e8c9ade 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -84,6 +84,7 @@ ) from .exceptions import PolyException from .http_helpers.helpers import ( + set_http_timeout, add_query_trade_params, add_query_open_orders_params, delete, @@ -124,6 +125,7 @@ def __init__( funder: str = None, builder_config: BuilderConfig = None, tick_size_ttl: float = 300.0, + timeout: float = None, ): """ Initializes the clob client @@ -143,6 +145,9 @@ def __init__( self.creds = creds self.mode = self._get_client_mode() + if timeout is not None: + set_http_timeout(timeout) + if self.signer: self.builder = OrderBuilder( self.signer, sig_type=signature_type, funder=funder diff --git a/py_clob_client/http_helpers/helpers.py b/py_clob_client/http_helpers/helpers.py index 7b47cd63..f30965bc 100644 --- a/py_clob_client/http_helpers/helpers.py +++ b/py_clob_client/http_helpers/helpers.py @@ -19,6 +19,14 @@ _http_client = httpx.Client(http2=True) +def set_http_timeout(timeout: float) -> None: + """ + Reconfigure the module-level HTTP client with a custom timeout (in seconds). + """ + global _http_client + _http_client = httpx.Client(http2=True, timeout=timeout) + + def overloadHeaders(method: str, headers: dict) -> dict: if headers is None: headers = dict() @@ -61,8 +69,8 @@ def request(endpoint: str, method: str, headers=None, data=None): except ValueError: return resp.text - except httpx.RequestError: - raise PolyApiException(error_msg="Request exception!") + except httpx.RequestError as exc: + raise PolyApiException(error_msg=f"Request exception: {exc}") from exc def post(endpoint, headers=None, data=None):