Skip to content

Commit

Permalink
fix: properly handle retry-after header
Browse files Browse the repository at this point in the history
  • Loading branch information
waza-ari committed Jan 15, 2024
1 parent 971dcea commit e72197e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion easyverein/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Middleware for FastAPI that supports authenticating users against Keycloak
"""

__version__ = "0.2.0"
__version__ = "0.2.1"

# Export EasyVerein API directly
from .api import EasyvereinAPI # noqa: F401
Expand Down
12 changes: 11 additions & 1 deletion easyverein/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,23 @@ def _do_request( # noqa: PLR0913

if res.status_code == 429:
retry_after = res.headers["Retry-After"]

try:
retry_after = int(retry_after)
except ValueError:
self.logger.error(
"Unable to parse Retry-After header while handling 429 response code."
)
self.logger.debug("Retry-After header: %s", retry_after)
retry_after = 0

self.logger.warning(
"Request returned status code 429, too many requests. Wait %d seconds",
retry_after,
)
raise EasyvereinAPITooManyRetriesException(
retry_after,
f"Too many requests, please wait {retry_after} seconds and try again.",
retry_after=retry_after,
)

if res.status_code == 404:
Expand Down
6 changes: 6 additions & 0 deletions easyverein/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ class EasyvereinAPITooManyRetriesException(EasyvereinAPIException):
Exception if the API returns a 429 Too Many Requests error
"""

def __init__(self, message, retry_after: int = 0):
# Call the base class constructor with the parameters it needs
super().__init__(message)

self.retry_after = retry_after

retry_after = 0
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-easyverein"
version = "0.2.0"
version = "0.2.1"
description = "Python library to interact with the EasyVerein API"
authors = ["Daniel Herrmann <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit e72197e

Please sign in to comment.