Files
OpenBB/openbb_platform/providers/tiingo/openbb_tiingo/utils/helpers.py
Danglewood 258e158606 [BugFix] Add Exception Handling For Unauthorized API Key Error. (#6800)
* add exception handling for unauthorized api call request.

* linting

* more linting

* more linting

* missed files

* biztoc cassette

* more linting

* crypto interval choices as 7d and 30d instead of 5 and 21

* update the default message and a few Intrinio endpoints to use the unauthorized error.

* mypy

* no-else-raise

---------

Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
2024-10-23 08:26:20 +00:00

42 lines
1.4 KiB
Python

"""Tiingo Helpers Module."""
from typing import Optional, Union
async def get_data(url: str) -> Union[dict, list]:
"""Get data from Tiingo endpoint and parse the JSON response."""
# pylint: disable=import-outside-toplevel
from json import JSONDecodeError # noqa
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.utils.errors import EmptyDataError, UnauthorizedError
from openbb_core.provider.utils.helpers import amake_request
response: Optional[Union[dict, list]] = None
try:
response = await amake_request(url)
if (
response
and isinstance(response, dict)
and len(list(response.keys())) == 1
and response.get("detail")
):
if (
"token" in response["detail"]
or "access" in response["detail"]
or "permission" in response["detail"]
or "authorized" in response["detail"]
):
raise UnauthorizedError(
f"Unauthorized Tiingo request -> {response['detail']}"
)
raise OpenBBError(response["detail"])
if not response:
raise EmptyDataError("The response is empty")
return response
except JSONDecodeError as e:
raise OpenBBError(f"Failed to parse JSON response -> {e}") from e