Provide session initialization with cache
This commit is contained in:
parent
47c9ad958e
commit
2220c31c5f
|
@ -1,20 +1,61 @@
|
|||
"""Utility module for setting up ratelimiting on third-party sessions."""
|
||||
|
||||
from _version import __version__
|
||||
|
||||
import logging
|
||||
import pathlib
|
||||
import os
|
||||
import datetime
|
||||
|
||||
import requests
|
||||
|
||||
from ._version import __version__
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
import requests_cache
|
||||
except ImportError:
|
||||
requests_cache = None
|
||||
logger.warning(
|
||||
'Library requests_cache could not be loaded. Automatic cache configuration will not be used.',
|
||||
)
|
||||
|
||||
|
||||
try:
|
||||
import requests_ratelimiter
|
||||
except ImportError:
|
||||
requests_ratelimiter = None
|
||||
logger.warning(
|
||||
'Requests ratelimiter could not be loaded. Automatic ratelimit configuration have not be set up.',
|
||||
'Library requests_ratelimiter could not be loaded. Automatic ratelimit configuration will not be used.',
|
||||
)
|
||||
|
||||
|
||||
CACHE_PATH_FROM_HOME = '.local/share/usagi-keiretsu/fin_data/http_cache'
|
||||
|
||||
def initialize_session() -> requests.Session:
|
||||
'''
|
||||
Creates Session with or without caching depending upon current
|
||||
capabilities.
|
||||
'''
|
||||
logger.info('Setting up session')
|
||||
|
||||
# Setup cache if it can be loaded.
|
||||
if requests_cache:
|
||||
home = pathlib.Path(os.environ['HOME'])
|
||||
cache_path = home / CACHE_PATH_FROM_HOME
|
||||
cache_path.mkdir(parents=True, exist_ok=True)
|
||||
session = requests_cache.CachedSession(
|
||||
cache_path,
|
||||
expire_after=datetime.timedelta(days=1),
|
||||
stale_if_error=True,
|
||||
cache_control=False,
|
||||
)
|
||||
else:
|
||||
session = requests.Session()
|
||||
|
||||
# Return initialized session.
|
||||
return session
|
||||
|
||||
|
||||
def setup_limiter(
|
||||
session: requests.Session, url_prefix: str, **limiter_args
|
||||
) -> requests.Session:
|
||||
|
|
Loading…
Reference in New Issue
Block a user