Skip to content

Commit

Permalink
Moved exceptions classes inside main client class
Browse files Browse the repository at this point in the history
  • Loading branch information
reefab committed Feb 13, 2018
1 parent 6b275b9 commit 23f301d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
44 changes: 22 additions & 22 deletions foobot_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FoobotClient(object):
"""
Foobot API client
:param token: API secret key used for authentication, see main doc on how
:param token: API secret key used for authentication, see main doc on how
to obtain one
:type token: str
:param username: Your username for your Foobot account
Expand Down Expand Up @@ -44,7 +44,7 @@ def get_devices(self):
Get a list of devices associated with that account.
:returns: list of devices
:raises: FoobotClientError, AuthFailure, BadFormat, ForbiddenAccess,
:raises: ClientError, AuthFailure, BadFormat, ForbiddenAccess,
TooManyRequests, InternalError
.. note::
Expand All @@ -70,7 +70,7 @@ def get_last_data(self, uuid, period=0, average_by=0):
long range requests (e.g. more than 1 day)
:type average_by: integer
:returns: list of datapoints
:raises: FoobotClientError, AuthFailure, BadFormat, ForbiddenAccess,
:raises: ClientError, AuthFailure, BadFormat, ForbiddenAccess,
TooManyRequests, InternalError
.. note::
Expand Down Expand Up @@ -107,7 +107,7 @@ def get_historical_data(self, uuid, start, end, average_by=0):
long range requests (e.g. more than 1 day)
:type average_by: integer
:returns: list of datapoints
:raises: FoobotClientError, AuthFailure, BadFormat, ForbiddenAccess,
:raises: ClientError, AuthFailure, BadFormat, ForbiddenAccess,
TooManyRequests, InternalError
.. seealso:: :func:`parse_data` for return data syntax
Expand Down Expand Up @@ -152,33 +152,33 @@ def _get(self, path, **kwargs):
resp = yield from self._session.get(
path, headers=dict(self._headers, **kwargs))
if resp.status == 400:
raise BadFormat(resp.text())
raise FoobotClient.BadFormat(resp.text())
elif resp.status == 401:
raise AuthFailure(resp.text())
raise FoobotClient.AuthFailure(resp.text())
elif resp.status == 403:
raise ForbiddenAccess(resp.text())
raise FoobotClient.ForbiddenAccess(resp.text())
elif resp.status == 429:
raise TooManyRequests(resp.text())
raise FoobotClient.TooManyRequests(resp.text())
elif resp.status == 500:
raise InternalError(resp.text())
raise FoobotClient.InternalError(resp.text())
elif resp.status != 200:
raise FoobotClientError(resp.text())
raise FoobotClient.ClientError(resp.text())
return (yield from resp.json())

class FoobotClientError(Exception):
pass
class ClientError(Exception):
pass

class AuthFailure(FoobotClientError):
pass
class AuthFailure(ClientError):
pass

class BadFormat(FoobotClientError):
pass
class BadFormat(ClientError):
pass

class ForbiddenAccess(FoobotClientError):
pass
class ForbiddenAccess(ClientError):
pass

class TooManyRequests(FoobotClientError):
pass
class TooManyRequests(ClientError):
pass

class InternalError(FoobotClientError):
pass
class InternalError(ClientError):
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="foobot_async",
version="0.1.0",
version="0.2.0",
url="https://github.com/reefab/foobot_async",

author="Fabien Piuzzi",
Expand Down
14 changes: 7 additions & 7 deletions tests/test_foobot_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from aioresponses import aioresponses
from datetime import datetime
from foobot_async import *
from foobot_async import FoobotClient

client = FoobotClient('token', 'example@example.com')
loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_failed_auth_request():
mocked.get('https://api.foobot.io/v2/owner/example@example.com/device/',
status=401, body='{"message": "invalid key provided"}')

with pytest.raises(AuthFailure):
with pytest.raises(FoobotClient.AuthFailure):
resp = loop.run_until_complete(client.get_devices())

def test_failed_bad_format_request():
Expand All @@ -57,7 +57,7 @@ def test_failed_bad_format_request():
"stack": "[obfuscated]",
"propagatedException": null}''')

with pytest.raises(BadFormat):
with pytest.raises(FoobotClient.BadFormat):
resp = loop.run_until_complete(client.get_devices())

def test_forbidden_access_request():
Expand All @@ -68,31 +68,31 @@ def test_forbidden_access_request():
"propagatedException": null }":
"invalid key provided"}''')

with pytest.raises(ForbiddenAccess):
with pytest.raises(FoobotClient.ForbiddenAccess):
resp = loop.run_until_complete(client.get_devices())

def test_overquota_request():
with aioresponses() as mocked:
mocked.get('https://api.foobot.io/v2/owner/example@example.com/device/',
status=429, body='')

with pytest.raises(TooManyRequests):
with pytest.raises(FoobotClient.TooManyRequests):
resp = loop.run_until_complete(client.get_devices())

def test_internal_error_request():
with aioresponses() as mocked:
mocked.get('https://api.foobot.io/v2/owner/example@example.com/device/',
status=500, body='')

with pytest.raises(InternalError):
with pytest.raises(FoobotClient.InternalError):
resp = loop.run_until_complete(client.get_devices())

def test_unhandled_error_request():
with aioresponses() as mocked:
mocked.get('https://api.foobot.io/v2/owner/example@example.com/device/',
status=404, body='')

with pytest.raises(FoobotClientError):
with pytest.raises(FoobotClient.ClientError):
resp = loop.run_until_complete(client.get_devices())

def test_get_last_data_request():
Expand Down

0 comments on commit 23f301d

Please sign in to comment.