mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
[Feature] Warn limit number of countries in TE request (#6334)
* limit number of countries in TE request * const * disable=unused-argument * warn and info when countries above 28 --------- Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt> Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
This commit is contained in:
@@ -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 (
|
||||
@@ -31,6 +32,7 @@ GROUPS = Literal[
|
||||
"trade",
|
||||
"business",
|
||||
]
|
||||
TE_COUNTRY_LIMIT = 28
|
||||
|
||||
|
||||
class TEEconomicCalendarQueryParams(EconomicCalendarQueryParams):
|
||||
@@ -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
|
||||
@@ -57,6 +61,13 @@ class TEEconomicCalendarQueryParams(EconomicCalendarQueryParams):
|
||||
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")
|
||||
@@ -130,11 +141,15 @@ class TEEconomicCalendarFetcher(
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user