Skip to content

Commit

Permalink
Handle broken rate limit as best effort
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatelmas committed Aug 16, 2022
1 parent d8844fd commit 5583bdd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 0 additions & 2 deletions stream_chat/tests/test_stream_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

from datetime import datetime, timezone

from stream_chat.types.stream_response import StreamResponse
Expand Down
12 changes: 6 additions & 6 deletions stream_chat/types/stream_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timezone
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, Optional

from stream_chat.types.rate_limit import RateLimitInfo

Expand Down Expand Up @@ -38,19 +38,19 @@ def __init__(
)
if limit and remaining and reset:
self.__rate_limit = RateLimitInfo(
limit=self._clean_header(limit),
remaining=self._clean_header(remaining),
limit=int(self._clean_header(limit)),
remaining=int(self._clean_header(remaining)),
reset=datetime.fromtimestamp(
self._clean_header(reset, float), timezone.utc
float(self._clean_header(reset)), timezone.utc
),
)

super(StreamResponse, self).__init__(response_dict)

def _clean_header(self, header: str, parser=int) -> Union[int, float]:
def _clean_header(self, header: str) -> int:
try:
values = (v.strip() for v in header.split(","))
return parser(next(v for v in values if v))
return int(next(v for v in values if v))
except ValueError:
return 0

Expand Down

0 comments on commit 5583bdd

Please sign in to comment.