Files
OpenBB/openbb_sdk/providers/fred/openbb_fred/fred_base.py
Theodore Aptekarev e5a9a7f427 Update folder structure for the SDK v4 (#5212)
* Move openbb_provider

* Add lock file for provider core

* Move sdk core

* Move openbb-provider readme text to the correct location

* Move data providers code

* Remove decoupling leftovers

* Lower the supported python version to 3.8

* Move sdk extensions

* Rename provider imports

* Update openbb pyproject toml

* Fix import statements after the move

* Update lock files

* Change ignored path for the static assets

* Add obb mascot

* Fix providers not being available in the openbb package

* Remove providers extension in favor of the coverage router in sdk core

* username is optional

* linting

* Change openbb_sdk_core to openbb_core

* Bump ruff pre-commit to latest version

* Bump ruff to latest in github actions

* Make ruff and pylint lint only the code in diff in GHA

* Attempt to fix broken linting action

* Attempt to fix the broken linting action II

* Lint code with pre-commit

* Revert all changes to github actions

* Revert to using ruff 0.0.256

* Use dot for local imports

* Re-commit lost forex pairs

* Fix fmp's typo that made it to the openbb data points

* Add other_investing_activities field that has a typo in fmp's api

---------

Co-authored-by: montezdesousa <montezdesousa@gmail.com>
Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
2023-07-12 18:43:30 +02:00

62 lines
1.9 KiB
Python

# IMPORT STANDARD
from datetime import date
from typing import Optional
from urllib.parse import urlencode
# IMPORT THIRD-PARTY
import requests
# IMPORT INTERNAL
root_url = "https://api.stlouisfed.org/fred"
class Fred:
def __init__(self, api_key: str):
self.api_key = api_key
def __fetch_data(self, url: str):
full_url = f"{url}&api_key={self.api_key}&file_type=json"
response = requests.get(full_url, timeout=10)
return response.json()
def get_series(
self,
series_id: str,
start_date: Optional[date] = None,
end_date: Optional[date] = None,
**kwargs,
) -> dict:
"""
Get data for a Fred series id. This fetches the latest known data.
Code copied from: https://github.com/mortada/fredapi/blob/master/fredapi/fred.py
Parameters
----------
series_id : str
Fred series id such as 'CPIAUCSL'
start_date : date
earliest observation date
end_date : date
latest observation date
kwargs : additional parameters
Any additional parameters supported by FRED. You can see the full list here:
https://api.stlouisfed.org/docs/fred/series_observations.html
Returns
-------
data : Series
a Series where each index is the observation date and the value is the data for the Fred series
"""
url = f"{root_url}/series/observations?series_id={series_id}"
if start_date:
url += "&observation_start=" + start_date.strftime("%Y-%m-%d")
if end_date:
url += "&observation_end=" + end_date.strftime("%Y-%m-%d")
if kwargs.keys():
url += "&" + urlencode(kwargs)
root = self.__fetch_data(url)
if root is None:
raise ValueError("No data exists for series id: " + series_id)
return root["observations"]