Skip to content

Commit

Permalink
Merge pull request #1 from oychang/bug/response-failure
Browse files Browse the repository at this point in the history
Verify response type and fix exception reporting
  • Loading branch information
SteelPangolin committed Jun 20, 2014
2 parents aaeee55 + d818a66 commit 1914cb6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions genderize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ def get(self, names, country_id=None, language_id=None):
response = self.session.get(
'http://api.genderize.io/',
params=params)

if 'application/json' not in response.headers.get('content-type', ''):
status = "server responded with {http_code}: {reason}".format(
http_code=response.status_code, reason=response.reason)
raise GenderizeException(
'response not in JSON format ({status})'.format(status=status))

decoded = response.json()
if response.ok:
return [self._fixtypes(data) for data in decoded]
else:
raise GenderizeException(decoded['error'], r.status)
raise GenderizeException(decoded['error'], response.status_code)

def get1(self, name, **kwargs):
"""
Expand All @@ -72,5 +79,3 @@ def get1(self, name, **kwargs):
@see: get
"""
return self.get([name], **kwargs)[0]


0 comments on commit 1914cb6

Please sign in to comment.