refactored RevenueBusinessLine for stocks.fa.revseg

This commit is contained in:
Pratyush Shukla
2023-08-02 01:17:00 +05:30
parent 9fe93c0456
commit 7bbe2bccd5
2 changed files with 24 additions and 54 deletions

View File

@@ -9,32 +9,24 @@ from openbb_provider.models.revenue_business_line import (
RevenueBusinessLineData,
RevenueBusinessLineQueryParams,
)
from pydantic import validator
from openbb_fmp.utils.helpers import create_url, get_data
# This part is only provided by FMP and not by the other providers for now.
from pydantic import validator
class FMPRevenueBusinessLineQueryParams(RevenueBusinessLineQueryParams):
"""FMP Revenue Business Line QueryParams.
"""FMP Revenue Business Line Query.
Source: https://site.financialmodelingprep.com/developer/docs/sales-revenue-by-segments-api/
Parameter
---------
symbol : Optional[str]
The symbol of the company if no CIK is provided.
period : Literal["annual", "quarterly"]
The period of the income statement. Default is "annual".
structure : Literal["hierarchical", "flat"]
The structure of the revenue business line. Default is "flat".
"""
class FMPRevenueBusinessLineData(RevenueBusinessLineData):
@validator("date", pre=True)
def time_validate(cls, v): # pylint: disable=E0213
"""FMP Revenue Business Line Data."""
@validator("date", pre=True, check_fields=False)
def date_validate(cls, v): # pylint: disable=E0213
return datetime.strptime(v, "%Y-%m-%d")
@@ -48,12 +40,7 @@ class FMPRevenueBusinessLineFetcher(
):
@staticmethod
def transform_query(params: Dict[str, Any]) -> FMPRevenueBusinessLineQueryParams:
# Type has to be ignored below because we are using something different than the literal type
transformed_params = params
transformed_params["period"] = (
"annual" if params.get("period", "") == "annually" else "quarter"
)
return FMPRevenueBusinessLineQueryParams(**transformed_params)
return FMPRevenueBusinessLineQueryParams(**params)
@staticmethod
def extract_data(
@@ -61,16 +48,16 @@ class FMPRevenueBusinessLineFetcher(
) -> List[FMPRevenueBusinessLineData]:
api_key = credentials.get("fmp_api_key") if credentials else ""
query.period = "annual" if query.period == "annually" else "quarter"
url = create_url(4, "revenue-product-segmentation", api_key, query)
data = get_data(url)
if isinstance(data, dict):
raise ValueError("Expected list of dicts, got dict")
data = [
{
"date": list(d.keys())[0],
"data_and_service": list(d.values())[0],
}
{"date": list(d.keys())[0], "business_line": list(d.values())[0]}
for d in data
]

View File

@@ -1,49 +1,32 @@
"""Revenue by business line data model."""
"""Revenue by Business Line data model."""
from datetime import date
from datetime import date as dateType
from typing import Dict, Literal
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.metadata import QUERY_DESCRIPTIONS, DATA_DESCRIPTIONS
from openbb_provider.models.base import BaseSymbol
from pydantic import Field
class RevenueBusinessLineQueryParams(QueryParams, BaseSymbol):
"""Revenue business line query.
Parameter
---------
symbol : str
The symbol of the company.
period : Literal["quarterly", "annually"]
The period of the income statement.
structure : Literal["hierarchical", "flat"]
The structure of the revenue business line. Should always be flat.
"""
"""Revenue Business Line Query."""
period: Literal["quarterly", "annually"] = Field(
default="quarterly", description=QUERY_DESCRIPTIONS.get("period", "")
default="annually", description=QUERY_DESCRIPTIONS.get("period", "")
)
structure: Literal["hierarchical", "flat"] = Field(
default="flat"
default="flat", description="The structure of the returned data."
) # should always be flat
class RevenueBusinessLineData(Data):
"""Revenue by business line data.
"""Revenue by Business Line Data."""
Returns
-------
date : date
The date of the revenue.
data_and_service : Dict[str, int]
The data and service of the revenue.
"""
date: date
data_and_service: Dict[str, int]
date: dateType = Field(description=DATA_DESCRIPTIONS.get("date", ""))
business_line: Dict[str, int] = Field(
description="Day level data containing the revenue of the business line."
)