Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Warn limit number of countries in TE request #6334

Merged
merged 10 commits into from
Apr 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import datetime
from typing import Any, Dict, List, Literal, Optional, Union
from warnings import warn

from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.economic_calendar import (
Expand Down Expand Up @@ -31,6 +32,7 @@
"trade",
"business",
]
TE_COUNTRY_LIMIT = 28


class TEEconomicCalendarQueryParams(EconomicCalendarQueryParams):
Expand All @@ -48,6 +50,8 @@ class TEEconomicCalendarQueryParams(EconomicCalendarQueryParams):
)
group: Optional[GROUPS] = Field(default=None, description="Grouping of events")

_number_of_countries: int = 0

@field_validator("country", mode="before", check_fields=False)
@classmethod
def validate_country(cls, c: str): # pylint: disable=E0213
Expand All @@ -57,6 +61,13 @@ def validate_country(cls, c: str): # pylint: disable=E0213
for v in values:
check_item(v.lower(), COUNTRIES)
result.append(v.lower())

cls._number_of_countries = len(result)
if cls._number_of_countries >= TE_COUNTRY_LIMIT:
warn(
f"Trading Economics API tend to fail if the number of countries is above {TE_COUNTRY_LIMIT}."
)

return ",".join(result)

@field_validator("importance")
Expand Down Expand Up @@ -130,11 +141,15 @@ async def aextract_data(
async def callback(response: ClientResponse, _: Any) -> Union[dict, List[dict]]:
"""Return the response."""
if response.status != 200:
raise RuntimeError(f"Error in TE request -> {await response.text()}")
raise RuntimeError(
f"Error in TE request: \n{await response.text()}"
f"\nInfo -> TE API tend to fail if the number of countries is above {TE_COUNTRY_LIMIT}."
)
return await response.json()

return await amake_request(url, response_callback=callback, **kwargs)

# pylint: disable=unused-argument
@staticmethod
def transform_data(
query: TEEconomicCalendarQueryParams, data: List[Dict], **kwargs: Any
Expand Down
Loading