Skip to content

Commit

Permalink
refactor some exception processing
Browse files Browse the repository at this point in the history
  • Loading branch information
brad committed Jan 3, 2017
1 parent 3053f47 commit 24cd6c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
42 changes: 13 additions & 29 deletions fitbit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
# Python 2.x
from urllib import urlencode

from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2, OAuth2Session
from oauthlib.oauth2.rfc6749.errors import TokenExpiredError
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
HTTPUnauthorized, HTTPForbidden,
HTTPServerError, HTTPConflict, HTTPNotFound,
HTTPTooManyRequests, Timeout)
from fitbit.utils import curry

from . import exceptions
from .utils import curry


class FitbitOauth2Client(object):
Expand Down Expand Up @@ -61,7 +60,7 @@ def _request(self, method, url, **kwargs):
try:
return self.session.request(method, url, **kwargs)
except requests.Timeout as e:
raise Timeout(*e.args)
raise exceptions.Timeout(*e.args)

def make_request(self, url, data={}, method=None, **kwargs):
"""
Expand All @@ -75,7 +74,7 @@ def make_request(self, url, data={}, method=None, **kwargs):
try:
auth = OAuth2(client_id=self.client_id, token=self.token)
response = self._request(method, url, data=data, auth=auth, **kwargs)
except (HTTPUnauthorized, TokenExpiredError) as e:
except (exceptions.HTTPUnauthorized, TokenExpiredError) as e:
self.refresh_token()
auth = OAuth2(client_id=self.client_id, token=self.token)
response = self._request(method, url, data=data, auth=auth, **kwargs)
Expand All @@ -94,23 +93,8 @@ def make_request(self, url, data={}, method=None, **kwargs):
except:
pass

if response.status_code == 401:
raise HTTPUnauthorized(response)
elif response.status_code == 403:
raise HTTPForbidden(response)
elif response.status_code == 404:
raise HTTPNotFound(response)
elif response.status_code == 409:
raise HTTPConflict(response)
elif response.status_code == 429:
exc = HTTPTooManyRequests(response)
exc.retry_after_secs = int(response.headers['Retry-After'])
raise exc

elif response.status_code >= 500:
raise HTTPServerError(response)
elif response.status_code >= 400:
raise HTTPBadRequest(response)
exceptions.detect_and_raise_error(response)

return response

def authorize_token_url(self, scope=None, redirect_uri=None, **kwargs):
Expand Down Expand Up @@ -168,7 +152,7 @@ def refresh_token(self):
self.token = self.oauth.refresh_token(
self.refresh_token_url,
refresh_token=self.token['refresh_token'],
auth=requests.auth.HTTPBasicAuth(self.client_id, self.client_secret)
auth=HTTPBasicAuth(self.client_id, self.client_secret)
)

if self.refresh_cb:
Expand Down Expand Up @@ -244,11 +228,11 @@ def make_request(self, *args, **kwargs):
if response.status_code == 204:
return True
else:
raise DeleteError(response)
raise exceptions.DeleteError(response)
try:
rep = json.loads(response.content.decode('utf8'))
except ValueError:
raise BadResponse
raise exceptions.BadResponse

return rep

Expand Down Expand Up @@ -390,9 +374,9 @@ def body_weight_goal(self, start_date=None, start_weight=None, weight=None):
"""
Implements the following APIs
* https://dev.fitbit.com/docs/body/#get-body-goals
* https://dev.fitbit.com/docs/body/#get-body-goals
* https://dev.fitbit.com/docs/body/#update-weight-goal
Pass no arguments to get the body weight goal. Pass ``start_date``,
``start_weight`` and optionally ``weight`` to set the weight goal.
``weight`` is required if it hasn't been set yet.
Expand Down
19 changes: 19 additions & 0 deletions fitbit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,22 @@ class HTTPServerError(HTTPException):
"""Generic >= 500 error
"""
pass


def detect_and_raise_error(response):
if response.status_code == 401:
raise HTTPUnauthorized(response)
elif response.status_code == 403:
raise HTTPForbidden(response)
elif response.status_code == 404:
raise HTTPNotFound(response)
elif response.status_code == 409:
raise HTTPConflict(response)
elif response.status_code == 429:
exc = HTTPTooManyRequests(response)
exc.retry_after_secs = int(response.headers['Retry-After'])
raise exc
elif response.status_code >= 500:
raise HTTPServerError(response)
elif response.status_code >= 400:
raise HTTPBadRequest(response)

0 comments on commit 24cd6c2

Please sign in to comment.