From 8cebbf743a57cae4951762f48abce35629bce167 Mon Sep 17 00:00:00 2001 From: IluaAir Date: Thu, 26 Mar 2026 00:31:13 +0300 Subject: [PATCH] Add OxiAdapter for enhanced HTTP request handling in OxiAPI - Introduced a new `OxiAdapter` class that extends `HTTPAdapter` to manage timeouts and retries for HTTP requests. - Integrated the `OxiAdapter` into the `OxiAPI` class, setting a default timeout and enabling retry logic for both HTTP and HTTPS requests. --- oxi/adapter.py | 21 +++++++++++++++++++++ oxi/core.py | 8 ++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 oxi/adapter.py diff --git a/oxi/adapter.py b/oxi/adapter.py new file mode 100644 index 0000000..87786ad --- /dev/null +++ b/oxi/adapter.py @@ -0,0 +1,21 @@ +from typing import Optional +from requests.adapters import HTTPAdapter +from urllib3.util import Retry + + +class OxiAdapter(HTTPAdapter): + def __init__( + self, + timeout: Optional[int] = None, + max_retries: int = 3, + *args, + **kwargs, + ): + self.timeout = timeout + retry = Retry(total=max_retries, backoff_factor=0.3) + super().__init__(*args, max_retries=retry, **kwargs) + + def send(self, request, **kwargs): + if kwargs.get("timeout") is None: + kwargs["timeout"] = self.timeout + return super().send(request, **kwargs) diff --git a/oxi/core.py b/oxi/core.py index fa14a57..0563c84 100644 --- a/oxi/core.py +++ b/oxi/core.py @@ -1,9 +1,10 @@ from typing import Optional from requests import Session + +from oxi.adapter import OxiAdapter from .node import Node -# TODO: Add custom adapter for Oxi class OxiAPI: def __init__( self, @@ -14,8 +15,11 @@ class OxiAPI: ): self.base_url = url.rstrip("/") self._session = Session() + self._adapter = OxiAdapter(timeout=10, max_retries=3) + self._session.mount("https://", self._adapter) + self._session.mount("http://", self._adapter) self._session.verify = verify - if username is not None and password is not None: + if username and password: self._session.auth = (username, password) self.node = Node(self._session, self.base_url)