Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP status code to APIException #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pycrest/errors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class APIException(Exception):
pass

def __init__(self, *args, **kwargs):
self.status_code = kwargs.pop("status_code", None)
Exception.__init__(self, *args, **kwargs)
6 changes: 4 additions & 2 deletions pycrest/eve.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def get(self, resource, params=None):
logger.debug('Getting resource %s (params=%s)', resource, prms)
res = self._session.get(resource, params=prms)
if res.status_code != 200:
raise APIException("Got unexpected status code from server: %i" % res.status_code)
raise APIException("Got unexpected status code from server: %i" % res.status_code,
status_code=res.status_code)

ret = res.json()

Expand Down Expand Up @@ -219,7 +220,8 @@ def _authorize(self, params):
headers = {"Authorization": "Basic %s" % auth}
res = self._session.post("%s/token" % self._oauth_endpoint, params=params, headers=headers)
if res.status_code != 200:
raise APIException("Got unexpected status code from API: %i" % res.status_code)
raise APIException("Got unexpected status code from API: %i" % res.status_code,
status_code=res.status_code)
return res.json()

def authorize(self, code):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ def test_pagination(url, request):
self.assertEqual(eve.marketData().items[3], "baz")
self.assertEqual(eve().status().eve, "online")
self.assertRaises(APIException, lambda: eve.incursions()) # Scala's notation would be nice
# Test the APIException status code attribute
try:
eve.incursions()
except APIException as e:
self.assertEqual(e.status_code, 404)
# cache miss
eve = pycrest.EVE(cache_dir='/cachedir')
eve()
Expand Down