Skip to content

Commit

Permalink
Added more error handling for invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
reefab committed Feb 17, 2018
1 parent e9f5abf commit 7d0b09d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
31 changes: 24 additions & 7 deletions foobot_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ def parse_data(self, response):
* allpollu: `foobot index <https://help.foobot.io/hc/en-us/articles/204814371-What-does-central-number-mean->`_, unit: %
"""
parsed = []
items = response['sensors']
for datapoint in response['datapoints']:
line = {}
for index, data in enumerate(datapoint):
line[items[index]] = data
parsed.append(line)
return parsed
try:
items = response['sensors']
for datapoint in response['datapoints']:
line = {}
for index, data in enumerate(datapoint):
line[items[index]] = data
parsed.append(line)
return parsed
except (KeyError, IndexError, TypeError):
raise FoobotClient.InvalidData()

@asyncio.coroutine
def _get(self, path, **kwargs):
Expand All @@ -165,20 +168,34 @@ def _get(self, path, **kwargs):
raise FoobotClient.ClientError(resp.text())
return (yield from resp.json())

@property
def last_data_request(self):
return self._last_data_request

class ClientError(Exception):
"""Generic Error."""
pass

class AuthFailure(ClientError):
"""Failed Authentication."""
pass

class BadFormat(ClientError):
"""Request is malformed."""
pass

class ForbiddenAccess(ClientError):
"""Access is prohibited."""
pass

class TooManyRequests(ClientError):
"""Too many requests for this time period."""
pass

class InternalError(ClientError):
"""Server Internal Error."""
pass

class InvalidData(ClientError):
"""Can't parse response data."""
pass
15 changes: 15 additions & 0 deletions tests/test_foobot_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ def test_get_historical_data_request():
co2 = 1178.0,
voc = 325.5,
allpollu= 131.19643) == resp[0]

def test_get_bad_data_request():
body = '''{"uuid": "1234127987696AB",
"start": 1518131274,
"end": 1518131874}'''

with aioresponses() as mocked:
mocked.get('https://api.foobot.io/v2/device/1234127987696AB/datapoint/1518121274/1518131274/3600/',
status=200, body=body)

with pytest.raises(FoobotClient.InvalidData):
loop.run_until_complete(client.get_historical_data("1234127987696AB",
datetime.utcfromtimestamp(1518121274),
datetime.utcfromtimestamp(1518131274),
3600))

0 comments on commit 7d0b09d

Please sign in to comment.