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

Commit 5679c97

Browse files
author
Jason Costello
committed
Migrating away from from_get() method, using abstract request() method for easier exception handling
1 parent 12b043a commit 5679c97

File tree

5 files changed

+15
-35
lines changed

5 files changed

+15
-35
lines changed

hypervector/resources/abstract/api_resource.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22
import hypervector
3-
from hypervector.errors import APIKeyNotSetError, HypervectorError, APIBaseError
3+
from hypervector.errors import APIKeyNotSetError, HypervectorError, APIConnectionError
44

55

66
class APIResource:
@@ -17,30 +17,26 @@ def get_headers(cls):
1717
return cls.headers
1818

1919
@classmethod
20-
def get(cls, uuid):
21-
endpoint = f'{hypervector.API_BASE}/{cls.resource_name}/{uuid}'
20+
def request(cls, endpoint, method=requests.get, json=None):
2221
try:
23-
response = requests.get(endpoint, headers=cls.get_headers())
22+
response = method(url=endpoint, headers=cls.get_headers(), json=json)
2423
except requests.ConnectionError:
25-
raise APIBaseError
26-
27-
if response.ok:
28-
return cls.from_get(response)
29-
else:
30-
raise HypervectorError
31-
32-
@classmethod
33-
def request(cls, endpoint, method=requests.get):
34-
try:
35-
response = method(url=endpoint, headers=cls.get_headers())
36-
except requests.ConnectionError:
37-
raise APIBaseError
24+
raise APIConnectionError(
25+
"There was a problem reaching the Hypervector API. Please "
26+
"check your hypervector.API_BASE setting"
27+
)
3828

3929
if response.ok:
4030
return response.json()
4131
else:
4232
raise HypervectorError(response)
4333

34+
@classmethod
35+
def get(cls, uuid):
36+
endpoint = f'{hypervector.API_BASE}/{cls.resource_name}/{uuid}'
37+
response = cls.request(endpoint)
38+
return cls.from_response(response)
39+
4440
@classmethod
4541
def delete(cls, uuid):
4642
endpoint = f'{hypervector.API_BASE}/{cls.resource_name}/{uuid}/delete'

hypervector/resources/core/benchmark.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ def to_response(self):
2828
"definition_uuid": self.definition_uuid
2929
}
3030

31-
@classmethod
32-
def from_get(cls, response):
33-
return cls.from_response(response.json())
34-
3531
def refresh(self):
3632
benchmark = self.get(self.benchmark_uuid)
3733
self.__dict__.update(benchmark.__dict__)
@@ -53,7 +49,7 @@ def new(cls, ensemble, expected_output):
5349
expected_output = expected_output.tolist()
5450

5551
data = {"expected_output": expected_output}
56-
response = requests.post(endpoint, json=data, headers=cls.get_headers()).json()
52+
response = cls.request(endpoint, method=requests.post, json=data)
5753
return cls.from_response(response)
5854

5955
def assert_equal(self, output_to_assert):

hypervector/resources/core/definition.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ def to_response(self):
3434
"ensembles": self.ensembles
3535
}
3636

37-
@classmethod
38-
def from_get(cls, response):
39-
return cls.from_response(response.json())
40-
4137
def refresh(self):
4238
definition = self.get(self.definition_uuid)
4339
self.__dict__.update(definition.__dict__)

hypervector/resources/core/ensemble.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def to_response(self):
4242
"benchmarks": self.benchmarks
4343
}
4444

45-
@classmethod
46-
def from_get(cls, response):
47-
return cls.from_response(response.json())
48-
4945
def refresh(self):
5046
ensemble = self.get(self.ensemble_uuid)
5147
self.__dict__.update(ensemble.__dict__)

hypervector/resources/core/project.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ def to_response(self):
2929
"definitions": self.definitions
3030
}
3131

32-
@classmethod
33-
def from_get(cls, response):
34-
return cls.from_response(response.json())
35-
3632
def refresh(self):
3733
project = self.get(self.project_uuid)
3834
self.__dict__.update(project.__dict__)
@@ -46,7 +42,7 @@ def list(cls):
4642
@classmethod
4743
def new(cls):
4844
endpoint = hypervector.API_BASE + "/" + cls.resource_name + "/new"
49-
response = requests.post(endpoint, headers=cls.get_headers()).json()
45+
response = cls.request(endpoint, method=requests.post)
5046
return cls.from_response(response)
5147

5248

0 commit comments

Comments
 (0)