Skip to content

Commit

Permalink
add cache option to APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
theOGognf committed May 14, 2024
1 parent c2620c7 commit dff0e9b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/finagg/fred/api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def get(url: str, /, **kwargs: Any) -> requests.Response:
A valid FRED API response.
"""
response = session.get(url, params=pformat(**kwargs))
params = pformat(**kwargs)
if params.pop("cache", True):
response = session.get(url, params=params)
else:
response = requests.get(url, params=params)
response.raise_for_status()
return response

Expand Down
24 changes: 22 additions & 2 deletions src/finagg/fred/api/_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get(
*,
realtime_start: None | int | str = None,
realtime_end: None | int | str = None,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get all child categories for a specific parent category.
Expand All @@ -44,6 +45,7 @@ def get(
according to their publication date.
realtime_end: End date for fetching results according
to their publication date.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -68,6 +70,7 @@ def get(
category_id=category_id,
realtime_start=realtime_start,
realtime_end=realtime_end,
cache=cache,
api_key=api_key,
).json()
data = data["categories"]
Expand All @@ -93,6 +96,7 @@ def get(
*,
realtime_start: None | int | str = None,
realtime_end: None | int | str = None,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get categories related to a category.
Expand All @@ -108,6 +112,7 @@ def get(
according to their publication date.
realtime_end: End date for fetching results according
to their publication date.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -121,6 +126,7 @@ def get(
category_id=category_id,
realtime_start=realtime_start,
realtime_end=realtime_end,
cache=cache,
api_key=api_key,
).json()
data = data["categories"]
Expand Down Expand Up @@ -155,6 +161,7 @@ def get(
tag_names: None | str | list[str] = None,
exclude_tag_names: None | str | list[str] = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get series within a category.
Expand Down Expand Up @@ -195,6 +202,7 @@ def get(
exclude_tag_names: Exclude tags related to these tags.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand Down Expand Up @@ -226,6 +234,7 @@ def get(
tag_names=tag_names,
exclude_tag_names=exclude_tag_names,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -257,6 +266,7 @@ def get(
order_by: None | str = None,
sort_order: None | str = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get a FRED category's tags.
Expand Down Expand Up @@ -298,6 +308,7 @@ def get(
descending ("desc") order.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand Down Expand Up @@ -329,6 +340,7 @@ def get(
tag_group_id=tag_group_id,
search_text=search_text,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -361,6 +373,7 @@ def get(
order_by: None | str = None,
sort_order: None | str = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get data for tags related to a category.
Expand Down Expand Up @@ -403,6 +416,7 @@ def get(
descending ("desc") order.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand Down Expand Up @@ -444,6 +458,7 @@ def get(
order_by=order_by,
sort_order=sort_order,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -500,12 +515,15 @@ class Category(_api.API):
url = "https://api.stlouisfed.org/fred/category"

@classmethod
def get(cls, category_id: int = 0, *, api_key: None | str = None) -> pd.DataFrame:
def get(
cls, category_id: int = 0, *, cache: bool = True, api_key: None | str = None
) -> pd.DataFrame:
"""Get a category's details.
Args:
category_id: The category's ID. Use the
"category/children" API to explore categories.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -518,6 +536,8 @@ def get(cls, category_id: int = 0, *, api_key: None | str = None) -> pd.DataFram
0 0 Categories 0
"""
data = _api.get(cls.url, category_id=category_id, api_key=api_key).json()
data = _api.get(
cls.url, category_id=category_id, cache=cache, api_key=api_key
).json()
data = data["categories"]
return pd.DataFrame(data)
27 changes: 27 additions & 0 deletions src/finagg/fred/api/_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def get(
sort_order: None | str = "desc",
include_release_dates_with_no_data: None | bool = False,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get all release dates of economic data.
Expand Down Expand Up @@ -61,6 +62,7 @@ def get(
dates that don't contain any data.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -80,6 +82,7 @@ def get(
sort_order=sort_order,
include_release_dates_with_no_data=include_release_dates_with_no_data,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -108,6 +111,7 @@ def get(
order_by: None | str = None,
sort_order: None | str = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get all releases of economic data.
Expand Down Expand Up @@ -135,6 +139,7 @@ def get(
descending ("desc") order.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -153,6 +158,7 @@ def get(
order_by=order_by,
sort_order=sort_order,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -181,6 +187,7 @@ def get(
sort_order: None | str = None,
include_release_dates_with_no_data: None | bool = False,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get data on release dates for a particular release of economic data.
Expand All @@ -203,6 +210,7 @@ def get(
dates that don't contain any data.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -221,6 +229,7 @@ def get(
sort_order=sort_order,
include_release_dates_with_no_data=include_release_dates_with_no_data,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -253,6 +262,7 @@ def get(
tag_names: None | str | list[str] = None,
exclude_tag_names: None | str | list[str] = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get data on the series related to a release of economic data.
Expand Down Expand Up @@ -299,6 +309,7 @@ def get(
exclude_tag_names: Exclude tags related to these tags.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -321,6 +332,7 @@ def get(
tag_names=tag_names,
exclude_tag_names=exclude_tag_names,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand All @@ -344,6 +356,7 @@ def get(
*,
realtime_start: None | int | str = None,
realtime_end: None | int | str = None,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get sources related to an economic release.
Expand All @@ -358,6 +371,7 @@ def get(
according to their publication date.
realtime_end: End date for fetching results according
to their publication date.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -370,6 +384,7 @@ def get(
release_id=release_id,
realtime_start=realtime_start,
realtime_end=realtime_end,
cache=cache,
api_key=api_key,
).json()
data = data["sources"]
Expand Down Expand Up @@ -403,6 +418,7 @@ def get(
order_by: None | str = None,
sort_order: None | str = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get tags for an economic release.
Expand Down Expand Up @@ -443,6 +459,7 @@ def get(
descending ("desc") order.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -465,6 +482,7 @@ def get(
order_by=order_by,
sort_order=sort_order,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand Down Expand Up @@ -497,6 +515,7 @@ def get(
order_by: None | str = None,
sort_order: None | str = None,
paginate: bool = False,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get data for tags related to an economic release.
Expand Down Expand Up @@ -538,6 +557,7 @@ def get(
descending ("desc") order.
paginate: Whether to manage `offset` automatically, making multiple
API calls until all results are returned.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -561,6 +581,7 @@ def get(
order_by=order_by,
sort_order=sort_order,
paginate=paginate,
cache=cache,
api_key=api_key,
)

Expand All @@ -585,6 +606,7 @@ def get(
element_id: None | int = 0,
include_observation_values: None | bool = False,
observation_date: None | str = None,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get release tables for a given economic release.
Expand All @@ -600,6 +622,7 @@ def get(
need to be returned.
observation_date: The observation date to be included with the
returned release table.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -613,6 +636,7 @@ def get(
element_id=element_id,
include_observation_values=include_observation_values,
observation_date=observation_date,
cache=cache,
api_key=api_key,
).json()
data = data["tables"]
Expand Down Expand Up @@ -685,6 +709,7 @@ def get(
*,
realtime_start: None | int | str = None,
realtime_end: None | int | str = None,
cache: bool = True,
api_key: None | str = None,
) -> pd.DataFrame:
"""Get overview data of an economic release.
Expand All @@ -699,6 +724,7 @@ def get(
according to their publication date.
realtime_end: End date for fetching results according
to their publication date.
cache: Whether to cache the response from the API.
api_key: Your FRED API key. Defaults to the ``FRED_API_KEY``
environment variable.
Expand All @@ -711,6 +737,7 @@ def get(
release_id=release_id,
realtime_start=realtime_start,
realtime_end=realtime_end,
cache=cache,
api_key=api_key,
).json()
data = data["releases"]
Expand Down
Loading

0 comments on commit dff0e9b

Please sign in to comment.