Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to cache requests #83

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions entsoe/entsoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +27,6 @@

URL = 'https://transparency.entsoe.eu/api'


def retry(func):
"""Catches connection errors, waits and retries"""

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