Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: simplify rate limit parsing with extract helper
Co-authored-by: Erwin Douna <e.douna@gmail.com>
  • Loading branch information
banter240 and erwindouna committed Mar 30, 2026
commit 7eb265b43c58d6bff176211c13e6db1a7dc3e509
19 changes: 8 additions & 11 deletions src/tadoasync/tadoasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,20 +847,17 @@ def _ensure_session(self) -> ClientSession:

def _parse_rate_limit(self) -> tuple[int | None, int | None]:
"""Parse rate limit from RateLimit headers."""
limit = None
remaining = None
def extract(pattern: str, value: str) -> int | None:
match = re.search(pattern, value)
return int(match.group(1)) if match else None

if (policy := self._last_headers.get("RateLimit-Policy", "")) and (
match := re.search(r"quota=(\d+)", policy)
):
limit = int(match[1])
policy = self._last_headers.get("RateLimit-Policy", "")
rl = self._last_headers.get("RateLimit", "")

if (rl := self._last_headers.get("RateLimit", "")) and (
match := re.search(r"remaining=(\d+)", rl)
):
remaining = int(match[1])
limit = extract(r"quota=(\d+)", policy)
remaining = extract(r"remaining=(\d+)", rl)

return limit, remaining
return limit, remaining

async def __aenter__(self) -> Self:
"""Async enter."""
Expand Down