Skip to content

Commit 87b1011

Browse files
bmwillyBrandon Williams
authored and
Brandon Williams
committed
Raise bad response status code
If a request results in a non-200 status code, raise the exception instead of returning a `requests.Response`. Fixes #11
1 parent ee06476 commit 87b1011

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

tests.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
2-
import sys
3-
import unittest
2+
import requests
43
import usabilla as ub
54

65
from mock import Mock
@@ -64,16 +63,18 @@ def test_client_constants(self):
6463
self.assertEqual(self.client.method, 'GET')
6564
self.assertEqual(self.client.host, 'data.usabilla.com')
6665
self.assertEqual(self.client.host_protocol, 'https://')
67-
self.assertEqual('',self.client.query_parameters)
66+
self.assertEqual('', self.client.query_parameters)
6867

6968
def test_sign_key(self):
7069
signed_key = self.client.sign(self.secret_key.encode('utf-8'), 'usbl1_request'.encode('utf-8'))
71-
self.assertEqual(signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac")
70+
self.assertEqual(
71+
signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac")
7272

7373
def test_get_signature_key(self):
7474
datestamp = '20150115'
7575
signing_key = self.client.get_signature_key(self.secret_key, datestamp)
76-
self.assertEqual(signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae")
76+
self.assertEqual(
77+
signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae")
7778

7879
def test_query_parameters(self):
7980
params = {'limit': 1}
@@ -131,9 +132,9 @@ def test_item_iterator(self):
131132
index += 1
132133
self.client.set_query_parameters.assert_called_with({'since': 1400000000002})
133134
self.assertEqual(self.client.send_signed_request.call_count, 2)
134-
self.client.send_signed_request.side_effect = ub.GeneralError('mocked', 'error')
135-
for item in self.client.item_iterator('/some/url'):
136-
raise ub.GeneralError('should not', 'come here')
135+
self.client.send_signed_request.side_effect = requests.exceptions.HTTPError('mocked error')
136+
with self.assertRaises(requests.exceptions.HTTPError):
137+
list(self.client.item_iterator('/some/url'))
137138

138139
def test_get_resource(self):
139140
self.client.item_iterator = Mock()

usabilla.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ class APIClient(object):
7272
"""
7373

7474
resources = {
75-
'scopes' : {
75+
'scopes': {
7676
'live': {
7777
'products': {
7878
'websites': {
79-
'resources' : {
79+
'resources': {
8080
'button': '/button',
8181
'feedback': '/button/:id/feedback',
8282
'campaign': '/campaign',
@@ -87,13 +87,13 @@ class APIClient(object):
8787
}
8888
},
8989
'email': {
90-
'resources' : {
90+
'resources': {
9191
'button': '/button',
9292
'feedback': '/button/:id/feedback'
9393
}
9494
},
9595
'apps': {
96-
'resources' : {
96+
'resources': {
9797
'app': '',
9898
'feedback': '/:id/feedback',
9999
'campaign': '/campaign',
@@ -238,12 +238,9 @@ def send_signed_request(self, scope):
238238
# Send the request.
239239
request_url = self.host + scope + '?' + canonical_querystring
240240
r = requests.get(self.host_protocol + request_url, headers=headers)
241+
r.raise_for_status()
241242

242-
if r.status_code != 200:
243-
return r
244-
else:
245-
return r.json()
246-
243+
return r.json()
247244

248245
def check_resource_validity(self, scope, product, resource):
249246
"""Checks whether the resource exists
@@ -287,7 +284,7 @@ def handle_id(self, url, resource_id):
287284
if resource_id == '':
288285
raise GeneralError('invalid id', 'Invalid resource ID')
289286
if resource_id == '*':
290-
resource_id = '%2A'
287+
resource_id = '%2A'
291288

292289
url = url.replace(':id', str(resource_id))
293290

@@ -300,20 +297,17 @@ def item_iterator(self, url):
300297
301298
:type url: str
302299
303-
:returns: An `generator` that yeilds the requested data.
300+
:returns: A `generator` that yields the requested data.
304301
:rtype: generator
302+
:raises requests.exceptions.HTTPError: if an HTTP error occurred
305303
"""
306304
has_more = True
307305
while has_more:
308-
try:
309-
results = self.send_signed_request(url)
310-
has_more = results['hasMore']
311-
for item in results['items']:
312-
yield item
313-
self.set_query_parameters({'since': results['lastTimestamp']})
314-
except:
315-
return
316-
306+
results = self.send_signed_request(url)
307+
has_more = results['hasMore']
308+
for item in results['items']:
309+
yield item
310+
self.set_query_parameters({'since': results['lastTimestamp']})
317311

318312
def get_resource(self, scope, product, resource, resource_id=None, iterate=False):
319313
"""Retrieves resources of the specified type
@@ -330,7 +324,7 @@ def get_resource(self, scope, product, resource, resource_id=None, iterate=False
330324
:type resource_id: str
331325
:type iterate: bool
332326
333-
:returns: An `generator` that yeilds the requested data or a single resource
327+
:returns: A `generator` that yields the requested data or a single resource
334328
:rtype: generator or single resource
335329
"""
336330
url = self.handle_id(self.check_resource_validity(scope, product, resource), resource_id)

0 commit comments

Comments
 (0)