diff --git a/tests.py b/tests.py index e3dc5a8..239adb3 100644 --- a/tests.py +++ b/tests.py @@ -1,6 +1,5 @@ import logging -import sys -import unittest +import requests import usabilla as ub from mock import Mock @@ -64,16 +63,18 @@ def test_client_constants(self): self.assertEqual(self.client.method, 'GET') self.assertEqual(self.client.host, 'data.usabilla.com') self.assertEqual(self.client.host_protocol, 'https://') - self.assertEqual('',self.client.query_parameters) + self.assertEqual('', self.client.query_parameters) def test_sign_key(self): signed_key = self.client.sign(self.secret_key.encode('utf-8'), 'usbl1_request'.encode('utf-8')) - self.assertEqual(signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac") + self.assertEqual( + signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac") def test_get_signature_key(self): datestamp = '20150115' signing_key = self.client.get_signature_key(self.secret_key, datestamp) - self.assertEqual(signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae") + self.assertEqual( + signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae") def test_query_parameters(self): params = {'limit': 1} @@ -131,9 +132,9 @@ def test_item_iterator(self): index += 1 self.client.set_query_parameters.assert_called_with({'since': 1400000000002}) self.assertEqual(self.client.send_signed_request.call_count, 2) - self.client.send_signed_request.side_effect = ub.GeneralError('mocked', 'error') - for item in self.client.item_iterator('/some/url'): - raise ub.GeneralError('should not', 'come here') + self.client.send_signed_request.side_effect = requests.exceptions.HTTPError('mocked error') + with self.assertRaises(requests.exceptions.HTTPError): + list(self.client.item_iterator('/some/url')) def test_get_resource(self): self.client.item_iterator = Mock() diff --git a/usabilla.py b/usabilla.py index 49fe12f..74b1b71 100644 --- a/usabilla.py +++ b/usabilla.py @@ -72,11 +72,11 @@ class APIClient(object): """ resources = { - 'scopes' : { + 'scopes': { 'live': { 'products': { 'websites': { - 'resources' : { + 'resources': { 'button': '/button', 'feedback': '/button/:id/feedback', 'campaign': '/campaign', @@ -87,13 +87,13 @@ class APIClient(object): } }, 'email': { - 'resources' : { + 'resources': { 'button': '/button', 'feedback': '/button/:id/feedback' } }, 'apps': { - 'resources' : { + 'resources': { 'app': '', 'feedback': '/:id/feedback', 'campaign': '/campaign', @@ -238,12 +238,9 @@ def send_signed_request(self, scope): # Send the request. request_url = self.host + scope + '?' + canonical_querystring r = requests.get(self.host_protocol + request_url, headers=headers) + r.raise_for_status() - if r.status_code != 200: - return r - else: - return r.json() - + return r.json() def check_resource_validity(self, scope, product, resource): """Checks whether the resource exists @@ -287,7 +284,7 @@ def handle_id(self, url, resource_id): if resource_id == '': raise GeneralError('invalid id', 'Invalid resource ID') if resource_id == '*': - resource_id = '%2A' + resource_id = '%2A' url = url.replace(':id', str(resource_id)) @@ -300,20 +297,17 @@ def item_iterator(self, url): :type url: str - :returns: An `generator` that yeilds the requested data. + :returns: A `generator` that yields the requested data. :rtype: generator + :raises requests.exceptions.HTTPError: if an HTTP error occurred """ has_more = True while has_more: - try: - results = self.send_signed_request(url) - has_more = results['hasMore'] - for item in results['items']: - yield item - self.set_query_parameters({'since': results['lastTimestamp']}) - except: - return - + results = self.send_signed_request(url) + has_more = results['hasMore'] + for item in results['items']: + yield item + self.set_query_parameters({'since': results['lastTimestamp']}) def get_resource(self, scope, product, resource, resource_id=None, iterate=False): """Retrieves resources of the specified type @@ -330,7 +324,7 @@ def get_resource(self, scope, product, resource, resource_id=None, iterate=False :type resource_id: str :type iterate: bool - :returns: An `generator` that yeilds the requested data or a single resource + :returns: A `generator` that yields the requested data or a single resource :rtype: generator or single resource """ url = self.handle_id(self.check_resource_validity(scope, product, resource), resource_id)