Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Adding APIBaseError, plus intermediate handling of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Costello committed Apr 21, 2021
1 parent 6e2e98f commit 7d7d7ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 8 additions & 1 deletion hypervector/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ class APIKeyNotSetError(Exception):
pass


class APIBaseError(Exception):
pass


class HypervectorError(Exception):
def __init__(self, response=None):
self.response = response
self.status_code = response.status_code
if response:
self.status_code = response.status_code
else:
self.status_code = None



Expand Down
16 changes: 12 additions & 4 deletions hypervector/resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
import hypervector
from hypervector.errors import APIKeyNotSetError, HypervectorError
from hypervector.errors import APIKeyNotSetError, HypervectorError, APIBaseError


class APIResource:
Expand All @@ -19,15 +19,23 @@ def get_headers(cls):
@classmethod
def get(cls, uuid):
endpoint = f'{hypervector.API_BASE}/{cls.resource_name}/{uuid}'
response = requests.get(endpoint, headers=cls.get_headers())
try:
response = requests.get(endpoint, headers=cls.get_headers())
except requests.ConnectionError:
raise APIBaseError

if response.ok:
return cls.from_get(response)
else:
raise HypervectorError(response)
raise HypervectorError

@classmethod
def request(cls, endpoint, method=requests.get):
response = method(url=endpoint, headers=cls.get_headers())
try:
response = method(url=endpoint, headers=cls.get_headers())
except requests.ConnectionError:
raise APIBaseError

if response.ok:
return response.json()
else:
Expand Down

0 comments on commit 7d7d7ec

Please sign in to comment.