refactored MajorIndicesConstituents for economy.const

This commit is contained in:
Pratyush Shukla
2023-08-02 00:13:11 +05:30
parent f06f4333b5
commit 609ca78fdb
2 changed files with 43 additions and 50 deletions

View File

@@ -2,17 +2,18 @@
from datetime import datetime
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
from openbb_provider.abstract.fetcher import Fetcher
from openbb_provider.models.major_indices_constituents import (
MajorIndicesConstituentsData,
MajorIndicesConstituentsQueryParams,
)
from pydantic import validator
from openbb_fmp.utils.helpers import get_data_many
from pydantic import validator
class FMPMajorIndicesConstituentsQueryParams(MajorIndicesConstituentsQueryParams):
"""FMP Major Indices Constituents query.
@@ -20,18 +21,20 @@ class FMPMajorIndicesConstituentsQueryParams(MajorIndicesConstituentsQueryParams
Source: https://site.financialmodelingprep.com/developer/docs/list-of-dow-companies-api/
https://site.financialmodelingprep.com/developer/docs/list-of-sp-500-companies-api/
https://site.financialmodelingprep.com/developer/docs/list-of-nasdaq-companies-api/
Parameter
---------
index : Literal['nasdaq', 'sp500', 'dowjones']
The index for which we want to fetch the constituents. Default is 'dowjones'.
"""
class FMPMajorIndicesConstituentsData(MajorIndicesConstituentsData):
"""FMP Major Indices Constituents data."""
@validator("dateFirstAdded", pre=True)
class Config:
fields = {
"sub_sector": "subSector",
"headquarter": "headQuarter",
"date_first_added": "dateFirstAdded",
}
@validator("dateFirstAdded", pre=True, check_fields=False)
def date_first_added_validate(cls, v): # pylint: disable=E0213
try:
return datetime.strptime(v, "%Y-%m-%d") if v else None
@@ -39,7 +42,7 @@ class FMPMajorIndicesConstituentsData(MajorIndicesConstituentsData):
# For returning string in case of mismatched dates
return v
@validator("founded", pre=True)
@validator("founded", pre=True, check_fields=False)
def founded_validate(cls, v): # pylint: disable=E0213
try:
return datetime.strptime(v, "%Y-%m-%d") if v else None
@@ -64,10 +67,14 @@ class FMPMajorIndicesConstituentsFetcher(
@staticmethod
def extract_data(
query: FMPMajorIndicesConstituentsQueryParams, api_key: str
query: FMPMajorIndicesConstituentsQueryParams,
credentials: Optional[Dict[str, str]],
) -> List[FMPMajorIndicesConstituentsData]:
api_key = credentials.get("fmp_api_key") if credentials else ""
base_url = "https://financialmodelingprep.com/api/v3"
url = f"{base_url}/{query.index}_constituent/?apikey={api_key}"
return get_data_many(url, FMPMajorIndicesConstituentsData)
@staticmethod

View File

@@ -4,55 +4,41 @@
from datetime import date
from typing import Literal, Optional, Union
from pydantic import Field
from openbb_provider.abstract.data import Data
from openbb_provider.abstract.query_params import QueryParams
from openbb_provider.metadata import QUERY_DESCRIPTIONS
from openbb_provider.models.base import BaseSymbol
from pydantic import Field
class MajorIndicesConstituentsQueryParams(QueryParams):
"""Major Indices Constituents query.
Parameter
---------
index : Literal['nasdaq', 'sp500', 'dowjones']
The index for which we want to fetch the constituents. Default is 'dowjones'.
"""
"""Major Indices Constituents Query."""
index: Literal["nasdaq", "sp500", "dowjones"] = Field(
description=QUERY_DESCRIPTIONS.get("index", ""),
default="dowjones",
description="The index for which we want to fetch the constituents.",
)
class MajorIndicesConstituentsData(Data):
"""Major Indices Constituents price data.
class MajorIndicesConstituentsData(Data, BaseSymbol):
"""Major Indices Constituents Data."""
Returns
-------
symbol : str
The symbol of the constituent company in the index.
name : str
The name of the constituent company in the index.
sector : str
The sector the constituent company in the index belongs to.
subSector : str
The sub-sector the constituent company in the index belongs to.
headQuarter : str
The location of the headquarter of the constituent company in the index.
dateFirstAdded : date
The date the constituent company was added to the index.
cik : int
The Central Index Key of the constituent company in the index.
founded : date
The founding year of the constituent company in the index.
"""
name: str
sector: str
subSector: Optional[str] = Field(alias="sub_sector")
headQuarter: Optional[str] = Field(alias="headquarter")
dateFirstAdded: Optional[Union[date, str]] = Field(alias="date_first_added")
cik: int
founded: Union[date, str]
name: str = Field(description="The name of the constituent company in the index.")
sector: str = Field(
description="The sector the constituent company in the index belongs to."
)
sub_sector: Optional[str] = Field(
description="The sub-sector the constituent company in the index belongs to."
)
headquarter: Optional[str] = Field(
description="The location of the headquarter of the constituent company in the index."
)
date_first_added: Optional[Union[date, str]] = Field(
description="The date the constituent company was added to the index."
)
cik: int = Field(
description="The Central Index Key of the constituent company in the index."
)
founded: Union[date, str] = Field(
description="The founding year of the constituent company in the index."
)