Raise RatelimitException for HTTP 429 responses - #479
KetanHegde wants to merge 1 commit into
Conversation
|
The HTTP 429 classification is useful, but the new immediate re-raise in from threading import Event
from ddgs import DDGS
from ddgs.exceptions import RatelimitException
from ddgs.results import TextResult
ready = Event()
class Healthy:
name = provider = 'healthy'
def search(self, *args, **kwargs):
ready.set()
return [TextResult(title='Python', href='https://example.org/python', body='Python programming')]
class Limited:
name = provider = 'limited'
def search(self, *args, **kwargs):
assert ready.wait(2)
raise RatelimitException('HTTP 429')
client = DDGS(timeout=2)
client._get_engines = lambda *args: [Healthy(), Limited()]
try:
print(client.text('Python', max_results=10))
except Exception as exc:
print(type(exc).__name__ + ': ' + str(exc))PR head prints Could the collector retain the rate-limit exception, continue collecting successful results, and raise that typed exception only if the search finishes without results? An offline mixed-success/rate-limit regression plus an all-rate-limited regression would cover both the fallback contract and #478. |
Summary
Closes #478.
This PR raises
RatelimitExceptionwhen a search engine request receives an HTTP 429 response, allowing callers to distinguish rate limiting from otherDDGSExceptionerrors.Additionally,
_search_sync()now preservesRatelimitExceptionby re-raising it instead of wrapping it as a genericDDGSException.Changes
RatelimitExceptionfor HTTP 429 responses inBaseSearchEngine.request().RatelimitExceptioninDDGS._search_sync()by re-raising it.Testing
ruff check .pytestAll tests passed.