Skip to content

Commit

Permalink
Raise bad response status code
Browse files Browse the repository at this point in the history
If a request results in a non-200 status code, raise the exception
instead of returning a `requests.Response`.

Fixes #11
  • Loading branch information
bmwilly authored and Brandon Williams committed Mar 15, 2018
1 parent ee06476 commit 87b1011
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
17 changes: 9 additions & 8 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import sys
import unittest
import requests
import usabilla as ub

from mock import Mock
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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()
Expand Down
36 changes: 15 additions & 21 deletions usabilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class APIClient(object):
"""

resources = {
'scopes' : {
'scopes': {
'live': {
'products': {
'websites': {
'resources' : {
'resources': {
'button': '/button',
'feedback': '/button/:id/feedback',
'campaign': '/campaign',
Expand All @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 87b1011

Please sign in to comment.