Skip to content

Commit

Permalink
Feature: Add details RateLimitException to parse x-ratelimit header (#36
Browse files Browse the repository at this point in the history
)

Example:

```py
{'name': 'AUTHED_API_POST_LIMIT', 'period': 1, 'limit': 1, 'remaining': 0, 'until': '2024-01-08T22:31:48Z'}
```
  • Loading branch information
glensc authored Jan 8, 2024
2 parents 8ba2819 + 961a95d commit 13963e5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions trakt/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'BadResponseException',

# Exceptions by HTTP status code
# https://trakt.docs.apiary.io/#introduction/status-codes
'BadRequestException',
'OAuthException',
'ForbiddenException',
Expand Down Expand Up @@ -76,7 +77,7 @@ class NotFoundException(TraktException):
class MethodNotAllowedException(TraktException):
"""TraktException type to be raised when a 405 return code is received"""
http_code = 405
message = 'Method not Allowed'
message = 'Method Not Found - method doesn\'t exist'


class ConflictException(TraktException):
Expand Down Expand Up @@ -104,7 +105,16 @@ class RateLimitException(TraktException):

@property
def retry_after(self):
return int(self.response.headers.get("Retry-After", 1))
return int(self.response.headers.get("retry-after", 1))

@property
def details(self):
from json import JSONDecodeError, loads

try:
return loads(self.response.headers.get("x-ratelimit", ""))
except JSONDecodeError:
return None


class TraktInternalException(TraktException):
Expand Down

0 comments on commit 13963e5

Please sign in to comment.