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

[BugFix] AV Historical EPS - Expose Error Message On Request Fail #6406

Merged
merged 3 commits into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# pylint: disable=unused-argument

import warnings
from datetime import date as dateType
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.historical_eps import (
Expand All @@ -20,8 +20,6 @@
)
from pydantic import Field, field_validator

_warn = warnings.warn


class AlphaVantageHistoricalEpsQueryParams(HistoricalEpsQueryParams):
"""
Expand Down Expand Up @@ -103,17 +101,13 @@ async def aextract_data(
**kwargs: Any,
) -> List[Dict]:
"""Return the raw data from the AlphaVantage endpoint."""

api_key = credentials.get("alpha_vantage_api_key") if credentials else ""

BASE_URL = "https://www.alphavantage.co/query?function=EARNINGS&"

# We are allowing multiple symbols to be passed in the query, so we need to handle that.
symbols = query.symbol.split(",")

urls = [f"{BASE_URL}symbol={symbol}&apikey={api_key}" for symbol in symbols]

results = []
results: List = []
messages: List = []

# We need to make a custom callback function for this async request.
async def response_callback(response: ClientResponse, _: ClientSession):
Expand All @@ -123,7 +117,11 @@ async def response_callback(response: ClientResponse, _: ClientSession):
target = (
"annualEarnings" if query.period == "annual" else "quarterlyEarnings"
)
result = []
message = data.get("Information", "")
if message:
messages.append(message)
warn(f"Symbol Error for {symbol}: {message}")
result: List = []
# If data is returned, append it to the results list.
if data:
result = [
Expand All @@ -137,13 +135,15 @@ async def response_callback(response: ClientResponse, _: ClientSession):
results.extend(result[: query.limit])
else:
results.extend(result)

# If no data is returned, raise a warning and move on to the next symbol.
if not data:
_warn(f"Symbol Error: No data found for {symbol}")
warn(f"Symbol Error: No data found for {symbol}")

await amake_requests(urls, response_callback, **kwargs) # type: ignore

if not results:
raise EmptyDataError(f"No data was returned -> \n{messages[-1]}")

return results

@staticmethod
Expand Down
Loading