mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-06 17:31:53 +00:00
* 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>
42 lines
1.4 KiB
Python
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
|