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 committed Mar 14, 2018
1 parent ee06476 commit 80dd0c8
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 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,16 @@ def item_iterator(self, url):
:type url: str
:returns: An `generator` that yeilds the requested data.
:returns: A `generator` that yeilds the requested data.
:rtype: generator
"""
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 +323,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: An `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 80dd0c8

Please sign in to comment.