Skip to content

Commit 9a82482

Browse files
committed
refactor(http): extract session teardown into _close_session helper
1 parent ded0279 commit 9a82482

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

rocrate_validator/utils/http.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,19 @@ def initialize_cache(cls,
357357
:param no_cache: When ``True``, disable the HTTP cache entirely and
358358
use a plain ``requests.Session``. Incompatible with ``offline``.
359359
"""
360-
return cls(cache_max_age=cache_max_age, cache_path=cache_path,
361-
offline=offline, no_cache=no_cache)
360+
def _close_session(self) -> None:
361+
"""Close the current session and remove its cache file if it is temporary."""
362+
session = getattr(self, "session", None)
363+
if session is not None and hasattr(session, "close"):
364+
try:
365+
session.close()
366+
except Exception as e:
367+
logger.debug("Error closing previous session: %s", e)
368+
if getattr(self, "permanent_cache", True) is False:
369+
try:
370+
self.cleanup()
371+
except Exception as e:
372+
logger.debug("Error cleaning up previous cache: %s", e)
362373

363374
@classmethod
364375
def reset(cls) -> None:
@@ -369,17 +380,7 @@ def reset(cls) -> None:
369380
with cls._lock:
370381
instance = cls._instance
371382
if instance is not None:
372-
try:
373-
session = getattr(instance, "session", None)
374-
if session is not None and hasattr(session, "close"):
375-
session.close()
376-
except Exception as e:
377-
logger.debug("Error closing previous session: %s", e)
378-
if getattr(instance, "permanent_cache", True) is False:
379-
try:
380-
instance.cleanup()
381-
except Exception as e:
382-
logger.debug("Error cleaning up previous cache: %s", e)
383+
instance._close_session()
383384
cls._instance = None
384385

385386

0 commit comments

Comments
 (0)