Skip to content

Commit

Permalink
Fixes FMP index endpoint to get 1 day interval data (#5535)
Browse files Browse the repository at this point in the history
* fix FMP endpoint for getting index data for `1day` interval

* fix `open` type to prevent validation error

* modifed `validator` to `field_validator`

* type of fields is `StrictFloat`

* re-record and pass test

* linting
  • Loading branch information
the-praxs authored Oct 16, 2023
1 parent 08549aa commit fdbc6d7
Show file tree
Hide file tree
Showing 3 changed files with 2,278 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from typing import List, Optional, Set, Union

from dateutil import parser
from pydantic import Field, PositiveFloat, validator
from pydantic import Field, StrictFloat, StrictInt, field_validator

from openbb_provider.abstract.data import Data, StrictInt
from openbb_provider.abstract.data import Data
from openbb_provider.abstract.query_params import QueryParams
from openbb_provider.utils.descriptions import DATA_DESCRIPTIONS, QUERY_DESCRIPTIONS

Expand All @@ -26,7 +26,7 @@ class MajorIndicesHistoricalQueryParams(QueryParams):
description=QUERY_DESCRIPTIONS.get("end_date", ""), default=None
)

@validator("symbol", pre=True, check_fields=False, always=True)
@field_validator("symbol", mode="after", check_fields=False)
def upper_symbol(cls, v: Union[str, List[str], Set[str]]):
"""Convert symbol to uppercase."""
if isinstance(v, str):
Expand All @@ -38,15 +38,15 @@ class MajorIndicesHistoricalData(Data):
"""Major Indices end of day price data."""

date: datetime = Field(description=DATA_DESCRIPTIONS.get("date", ""))
open: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("open", ""))
high: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("high", ""))
low: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("low", ""))
close: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("close", ""))
open: StrictFloat = Field(description=DATA_DESCRIPTIONS.get("open", ""))
high: StrictFloat = Field(description=DATA_DESCRIPTIONS.get("high", ""))
low: StrictFloat = Field(description=DATA_DESCRIPTIONS.get("low", ""))
close: StrictFloat = Field(description=DATA_DESCRIPTIONS.get("close", ""))
volume: Optional[StrictInt] = Field(
default=None, description=DATA_DESCRIPTIONS.get("volume", "")
)

@validator("date", pre=True, check_fields=False)
@field_validator("date", mode="after", check_fields=False)
def date_validate(cls, v): # pylint: disable=E0213
"""Return formatted datetime."""
return parser.isoparse(str(v))
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class FMPMajorIndicesHistoricalQueryParams(MajorIndicesHistoricalQueryParams):
Source: https://site.financialmodelingprep.com/developer/docs/historical-index-price-api/
"""

__alias_dict__ = {"start_date": "from", "end_date": "to"}

timeseries: Optional[NonNegativeInt] = Field(
default=None, description="Number of days to look back."
)
Expand Down Expand Up @@ -89,17 +91,13 @@ def extract_data(
api_key = credentials.get("fmp_api_key") if credentials else ""

base_url = "https://financialmodelingprep.com/api/v3"
query_str = (
get_querystring(query.model_dump(), ["symbol"])
.replace("start_date", "from")
.replace("end_date", "to")
)
query_str = get_querystring(query.model_dump(), ["symbol", "interval"])

url_params = f"{query.symbol}?{query_str}&apikey={api_key}"
url = f"{base_url}/historical-chart/{query.interval}/{url_params}"

if query.interval == "1day":
url = f"{base_url}/historical-price-full/index/{url_params}"
url = f"{base_url}/historical-chart/1day/{query.symbol}?apikey={api_key}"

return get_data_many(url, "historical", **kwargs)

Expand Down
Loading

0 comments on commit fdbc6d7

Please sign in to comment.