diff --git a/lib/dl_api_commons/dl_api_commons/utils.py b/lib/dl_api_commons/dl_api_commons/utils.py index 453d6befd..80b175824 100644 --- a/lib/dl_api_commons/dl_api_commons/utils.py +++ b/lib/dl_api_commons/dl_api_commons/utils.py @@ -1,3 +1,6 @@ +import requests +import requests.adapters + from dl_constants.api_constants import ( DLCookies, DLHeaders, @@ -10,3 +13,37 @@ def stringify_dl_headers(headers: dict[DLHeaders, str]) -> dict[str, str]: def stringify_dl_cookies(headers: dict[DLCookies, str]) -> dict[str, str]: return {name.value: val for name, val in headers.items()} + + +def get_requests_session() -> requests.Session: + session = requests.Session() + ua = "{}, Datalens".format(requests.utils.default_user_agent()) + session.headers.update({"User-Agent": ua}) + return session + + +def get_retriable_requests_session() -> requests.Session: + session = get_requests_session() + + retry_conf = requests.adapters.Retry( + total=5, + backoff_factor=0.5, + status_forcelist=[500, 501, 502, 503, 504, 521], + redirect=10, + method_whitelist=frozenset(["HEAD", "TRACE", "GET", "PUT", "OPTIONS", "DELETE", "POST"]), + # # TODO: + # # (the good: will return a response when it's an error response) + # # (the bad: need to raise_for_status() manually, same as without retry conf) + # raise_on_status=False, + ) + + for schema in ("http://", "https://"): + session.mount( + schema, + # noinspection PyUnresolvedReferences + requests.adapters.HTTPAdapter( + max_retries=retry_conf, + ), + ) + + return session diff --git a/lib/dl_core/dl_core/united_storage_client.py b/lib/dl_core/dl_core/united_storage_client.py index 692ff9989..c9d79594d 100644 --- a/lib/dl_core/dl_core/united_storage_client.py +++ b/lib/dl_core/dl_core/united_storage_client.py @@ -30,6 +30,7 @@ from dl_api_commons.logging import mask_sensitive_fields_by_name_in_json_recursive from dl_api_commons.tracing import get_current_tracing_headers from dl_api_commons.utils import ( + get_retriable_requests_session, stringify_dl_cookies, stringify_dl_headers, ) @@ -41,7 +42,6 @@ from dl_core.base_models import EntryLocation from dl_core.enums import USApiType import dl_core.exc as exc -from dl_core.utils import get_retriable_requests_session LOGGER = logging.getLogger(__name__) diff --git a/lib/dl_core/dl_core/utils.py b/lib/dl_core/dl_core/utils.py index d874688d3..da1e34c6c 100644 --- a/lib/dl_core/dl_core/utils.py +++ b/lib/dl_core/dl_core/utils.py @@ -26,9 +26,6 @@ CIMultiDictProxy, ) import opentracing -import requests -import requests.adapters -import requests.exceptions import shortuuid import sqlalchemy as sa @@ -47,40 +44,6 @@ LOGGER = logging.getLogger(__name__) -def get_requests_session() -> requests.Session: - session = requests.Session() - ua = "{}, Datalens".format(requests.utils.default_user_agent()) - session.headers.update({"User-Agent": ua}) - return session - - -def get_retriable_requests_session() -> requests.Session: - session = get_requests_session() - - retry_conf = requests.adapters.Retry( - total=5, - backoff_factor=0.5, - status_forcelist=[500, 501, 502, 503, 504, 521], - redirect=10, - method_whitelist=frozenset(["HEAD", "TRACE", "GET", "PUT", "OPTIONS", "DELETE", "POST"]), - # # TODO: - # # (the good: will return a response when it's an error response) - # # (the bad: need to raise_for_status() manually, same as without retry conf) - # raise_on_status=False, - ) - - for schema in ("http://", "https://"): - session.mount( - schema, - # noinspection PyUnresolvedReferences - requests.adapters.HTTPAdapter( - max_retries=retry_conf, - ), - ) - - return session - - def make_user_auth_headers( rci: RequestContextInfo, auth_data_override: Optional[AuthData] = None,