From d426d0b9f6cb1fa1c8ed24055eed49cf642aae07 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 30 Mar 2024 13:26:35 +0100 Subject: [PATCH] Allow setting of cache behaviour --- requests_util/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/requests_util/__init__.py b/requests_util/__init__.py index 5875ccc..e71891b 100644 --- a/requests_util/__init__.py +++ b/requests_util/__init__.py @@ -48,6 +48,7 @@ def initialize_session() -> requests.Session: expire_after=datetime.timedelta(days=1), stale_if_error=True, cache_control=False, + urls_expire_after = {}, ) else: session = requests.Session() @@ -57,7 +58,10 @@ def initialize_session() -> requests.Session: def setup_limiter( - session: requests.Session, url_prefix: str, **limiter_args + session: requests.Session, + url_prefix: str, + expire_after: datetime.datetime | None = None, + **limiter_args ) -> requests.Session: """ Initializes limiter on session for the given domain with the given @@ -67,4 +71,6 @@ def setup_limiter( """ if requests_ratelimiter: session.mount(url_prefix, requests_ratelimiter.LimiterAdapter(**limiter_args)) + if requests_cache and expire_after: + session.settings.urls_expire_after[url_prefix] = expire_after return session