RateLimitReachedError exception sets message, code and headers.
Added docstring documentation describing HTTP-date format
translation on Retry-After header.
@@ -51,7 +51,14 @@ class RateLimitReachedError(BaseHTTPError):
message = '%s Rate limit exceeded' % (code)
def __init__(self, *args, **kwargs):
- self.retry_after = int(kwargs.pop('headers', {}).get('retry-after', 0))
+ headers = kwargs.pop('headers', None)
+ super(RateLimitReachedError, self).__init__(self.code,
+ self.message,
+ headers)
+ if self.headers is not None:
+ self.retry_after = int(self.headers.get('retry-after', 0))
+ else:
+ self.retry_after = 0
_error_classes = [RateLimitReachedError]
@@ -62,9 +69,22 @@ def exception_from_message(code, message, headers=None):
"""
Return an instance of BaseHTTPException or subclass based on response code.
+ If headers include Retry-After, RFC 2616 says that its value may be one of
+ two formats: HTTP-date or delta-seconds, for example:
+
+ Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
+ Retry-After: 120
+
+ If Retry-After comes in HTTP-date, it'll be translated to a positive
+ delta-seconds value when passing it to the exception constructor.
+
+ Also, RFC 2616 says that Retry-After isn't just only applicable to 429
+ HTTP status, but also to other responses, like 503 and 3xx.
+
Usage::
raise exception_from_message(code=self.status,
- message=self.parse_error())
+ message=self.parse_error(),
+ headers=self.headers)
"""
kwargs = {
'code': code,
