Skip to content

Commit

Permalink
Returns structured rate limit error.
Browse files Browse the repository at this point in the history
  • Loading branch information
braincore committed Aug 11, 2016
1 parent 4ff8627 commit 3f8bf40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 16 additions & 5 deletions dropbox/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import requests

from . import stone_serializers
from .auth import AuthError_validator
from .auth import (
AuthError_validator,
RateLimitError_validator,
)
from .base import DropboxBase
from .base_team import DropboxTeamBase
from .exceptions import (
Expand Down Expand Up @@ -382,10 +385,18 @@ def request_json_string(self,
AuthError_validator, r.json()['error'])
raise AuthError(request_id, err)
elif r.status_code == 429:
retry_after = r.headers.get('retry-after')
if retry_after is not None:
retry_after = int(retry_after)
raise RateLimitError(request_id, retry_after)
err = None
if r.headers.get('content-type') == 'application/json':
err = stone_serializers.json_compat_obj_decode(
RateLimitError_validator, r.json()['error'])
retry_after = err.retry_after
else:
retry_after_str = r.headers.get('retry-after')
if retry_after_str is not None:
retry_after = int(retry_after_str)
else:
retry_after = None
raise RateLimitError(request_id, err, retry_after)
elif 200 <= r.status_code <= 299:
if route_style == self._ROUTE_STYLE_DOWNLOAD:
raw_resp = r.headers['dropbox-api-result']
Expand Down
6 changes: 4 additions & 2 deletions dropbox/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ def __repr__(self):
class RateLimitError(HttpError):
"""Error caused by rate limiting."""

def __init__(self, request_id, backoff=None):
def __init__(self, request_id, error=None, backoff=None):
super(RateLimitError, self).__init__(request_id, 429, None)
self.error = error
self.backoff = backoff

def __repr__(self):
return 'RateLimitError({!r}, {!r})'.format(self.request_id, self.backoff)
return 'RateLimitError({!r}, {!r}, {!r})'.format(
self.request_id, self.error, self.backoff)


class InternalServerError(HttpError):
Expand Down

0 comments on commit 3f8bf40

Please sign in to comment.