Skip to content

Commit

Permalink
Move get_retriable_requests_session to dl_api_commons (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCPN authored Dec 4, 2023
1 parent 093fe07 commit 63d85e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
37 changes: 37 additions & 0 deletions lib/dl_api_commons/dl_api_commons/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import requests
import requests.adapters

from dl_constants.api_constants import (
DLCookies,
DLHeaders,
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/dl_core/dl_core/united_storage_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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__)
Expand Down
37 changes: 0 additions & 37 deletions lib/dl_core/dl_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
CIMultiDictProxy,
)
import opentracing
import requests
import requests.adapters
import requests.exceptions
import shortuuid
import sqlalchemy as sa

Expand All @@ -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,
Expand Down

0 comments on commit 63d85e6

Please sign in to comment.