Raise error if rate limited
This commit is contained in:
@@ -10,6 +10,8 @@ from frozendict import frozendict
|
||||
from . import utils, cache
|
||||
import threading
|
||||
|
||||
from .exceptions import YFRateLimitError
|
||||
|
||||
cache_maxsize = 64
|
||||
|
||||
|
||||
@@ -390,6 +392,10 @@ class YfData(metaclass=SingletonMeta):
|
||||
response = request_method(**request_args)
|
||||
utils.get_yf_logger().debug(f'response code={response.status_code}')
|
||||
|
||||
# Raise exception if rate limited
|
||||
if response.status_code == 429:
|
||||
raise YFRateLimitError()
|
||||
|
||||
return response
|
||||
|
||||
@lru_cache_freezeargs
|
||||
|
||||
@@ -45,3 +45,8 @@ class YFInvalidPeriodError(YFException):
|
||||
self.invalid_period = invalid_period
|
||||
self.valid_ranges = valid_ranges
|
||||
super().__init__(f"{self.ticker}: Period '{invalid_period}' is invalid, must be one of {valid_ranges}")
|
||||
|
||||
|
||||
class YFRateLimitError(YFException):
|
||||
def __init__(self):
|
||||
super().__init__("Too Many Requests. Rate limited. Try after a while.")
|
||||
|
||||
@@ -9,7 +9,7 @@ import bisect
|
||||
|
||||
from yfinance import shared, utils
|
||||
from yfinance.const import _BASE_URL_, _PRICE_COLNAMES_
|
||||
from yfinance.exceptions import YFInvalidPeriodError, YFPricesMissingError, YFTzMissingError
|
||||
from yfinance.exceptions import YFInvalidPeriodError, YFPricesMissingError, YFTzMissingError, YFRateLimitError
|
||||
|
||||
class PriceHistory:
|
||||
def __init__(self, data, ticker, tz, session=None, proxy=None):
|
||||
@@ -184,6 +184,9 @@ class PriceHistory:
|
||||
"the issue. Thank you for your patience.")
|
||||
|
||||
data = data.json()
|
||||
# Special case for rate limits
|
||||
except YFRateLimitError:
|
||||
raise
|
||||
except Exception:
|
||||
if raise_errors:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user