Skip to content

Commit 162aec5

Browse files
Merge pull request #5 from KintsugiMindfulWellness/fix_exception_handling
fix issue with exception handling to provide proper messages.
2 parents cd1dac4 + 6de2d65 commit 162aec5

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "kintsugi-python"
3-
version = "0.1.5"
3+
version = "0.1.6"
44

55
description = "Python SDK to access Kintsugi Voice API V2."
66
authors = [

src/kintsugi/api.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
DEFAULT_URL = 'https://api.kintsugihealth.com/v2'
77

88

9+
def get_error_message(response):
10+
if response.json().get("message"):
11+
return json.dumps(response.json().get("message"))
12+
elif response.json().get("predict_error"):
13+
return json.dumps(response.json().get("predict_error"))
14+
else:
15+
return response.status_code
16+
917
class ResponseException(Exception):
10-
def __init__(self, response: requests.Response):
11-
self.code = response.status_code
12-
try:
13-
self.message = response.json().get('message')
14-
except ValueError:
15-
self.message = None
16-
18+
def __init__(self, message):
19+
pass
1720

1821
class Api:
1922
def __init__(self, x_api_key: str, url: str = DEFAULT_URL):
@@ -41,7 +44,7 @@ def new_session_id(self, user_id: str, metadata: dict = None) -> str:
4144
data=json.dumps(data)
4245
)
4346
if response.status_code != 201:
44-
raise ResponseException(response)
47+
raise ResponseException(get_error_message(response))
4548

4649
return response.json()['session_id']
4750

@@ -73,7 +76,7 @@ def predict(self, user_id: str, audio_file, metadata:dict = None, allowed_sample
7376
)
7477

7578
if response.status_code != 202:
76-
raise ResponseException(response)
79+
raise ResponseException(get_error_message(response))
7780

7881
return response.json()['session_id']
7982

@@ -84,7 +87,7 @@ def get_prediction_by_session(self, session_id: str):
8487
)
8588

8689
if response.status_code != 200:
87-
raise ResponseException(response)
90+
raise ResponseException(get_error_message(response))
8891

8992
data = response.json()
9093
data['session_id'] = session_id
@@ -97,7 +100,7 @@ def get_prediction_by_user(self, user_id: str):
97100
)
98101

99102
if response.status_code != 200:
100-
raise ResponseException(response)
103+
raise ResponseException(get_error_message(response))
101104

102105
parser = PredictionParser()
103106
list_data = response.json()
@@ -125,7 +128,7 @@ def _send_answers(self, path, session_id, answers, expected_count) -> None:
125128
)
126129

127130
if response.status_code != 200:
128-
raise ResponseException(response)
131+
raise ResponseException(get_error_message(response))
129132

130133
def depression(self, session_id: str, data: str):
131134
if data not in ('false', 'true', 'additional_consideration_required'):
@@ -142,7 +145,7 @@ def depression(self, session_id: str, data: str):
142145
)
143146

144147
if response.status_code != 200:
145-
raise ResponseException(response)
148+
raise ResponseException(get_error_message(response))
146149

147150
def phq_2(self, session_id: str, answers: list) -> None:
148151
return self._send_answers('phq', session_id, answers, 2)

0 commit comments

Comments
 (0)