From ed7e1287cd3ebec99f47d7e143a36d64a9198517 Mon Sep 17 00:00:00 2001 From: Fabio Genoese Date: Fri, 6 Nov 2020 14:16:42 +0100 Subject: [PATCH] cache requests using requests_cache module --- entsoe/entsoe.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/entsoe/entsoe.py b/entsoe/entsoe.py index 63f59b7..a4ed3cf 100644 --- a/entsoe/entsoe.py +++ b/entsoe/entsoe.py @@ -8,6 +8,7 @@ from pandas.tseries.offsets import YearBegin, YearEnd import pytz import requests +import requests_cache from bs4 import BeautifulSoup from entsoe.exceptions import InvalidPSRTypeError, InvalidBusinessParameterError @@ -26,7 +27,6 @@ URL = 'https://transparency.entsoe.eu/api' - def retry(func): """Catches connection errors, waits and retries""" @@ -64,7 +64,8 @@ class EntsoeRawClient: def __init__( self, api_key: str, session: Optional[requests.Session] = None, retry_count: int = 1, retry_delay: int = 0, - proxies: Optional[Dict] = None, timeout: Optional[int] = None): + proxies: Optional[Dict] = None, timeout: Optional[int] = None, + cache_expire_after=0): """ Parameters ---------- @@ -77,12 +78,17 @@ def __init__( proxies : dict requests proxies timeout : int + cache_expire_after: int + amount of seconds after cache expires (0 implies no cache) """ if api_key is None: raise TypeError("API key cannot be None") self.api_key = api_key if session is None: session = requests.Session() + if cache_expire_after > 0: + requests_cache.install_cache(cache_name='entsoe-py_cache', backend='sqlite', expire_after=cache_expire_after) + self.cache_expire_after = cache_expire_after self.session = session self.proxies = proxies self.retry_count = retry_count @@ -114,6 +120,8 @@ def _base_request(self, params: Dict, start: pd.Timestamp, params.update(base_params) logging.debug(f'Performing request to {URL} with params {params}') + if self.cache_expire_after > 0: + requests_cache.remove_expired_responses() response = self.session.get(url=URL, params=params, proxies=self.proxies, timeout=self.timeout) try: