Skip to content

Commit ebe1d60

Browse files
Merge pull request #826 from adamtheturtle/simpler-exceptions
Simpler exceptions
2 parents 06ec651 + dc1f6d1 commit ebe1d60

File tree

1 file changed

+50
-55
lines changed

1 file changed

+50
-55
lines changed

src/vws/vws.py

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import base64
66
import io
77
import json
8-
from enum import Enum
98
from time import sleep
109
from typing import Dict, List, Optional, Union
1110
from urllib.parse import urljoin
@@ -82,41 +81,32 @@ def _target_api_request(
8281
return response
8382

8483

85-
class _ResultCodes(Enum):
84+
def _raise_for_result_code(
85+
response: Response,
86+
expected_result_code: str,
87+
) -> None:
8688
"""
87-
Constants representing various VWS result codes.
89+
Raise an appropriate exception if the expected result code for a successful
90+
request is not returned.
8891
89-
See
90-
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
91-
92-
Some codes here are not documented in the above link.
92+
Args:
93+
response: A response from Vuforia.
94+
expected_result_code: See
95+
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
9396
"""
97+
result_code = response.json()['result_code']
98+
if result_code == expected_result_code:
99+
return
94100

95-
SUCCESS = 'Success'
96-
TARGET_CREATED = 'TargetCreated'
97-
AUTHENTICATION_FAILURE = 'AuthenticationFailure'
98-
REQUEST_TIME_TOO_SKEWED = 'RequestTimeTooSkewed'
99-
TARGET_NAME_EXIST = 'TargetNameExist'
100-
UNKNOWN_TARGET = 'UnknownTarget'
101-
BAD_IMAGE = 'BadImage'
102-
IMAGE_TOO_LARGE = 'ImageTooLarge'
103-
METADATA_TOO_LARGE = 'MetadataTooLarge'
104-
DATE_RANGE_ERROR = 'DateRangeError'
105-
FAIL = 'Fail'
106-
TARGET_STATUS_PROCESSING = 'TargetStatusProcessing'
107-
REQUEST_QUOTA_REACHED = 'RequestQuotaReached'
108-
TARGET_STATUS_NOT_SUCCESS = 'TargetStatusNotSuccess'
109-
PROJECT_INACTIVE = 'ProjectInactive'
110-
INACTIVE_PROJECT = 'InactiveProject'
111-
112-
113-
_EXCEPTIONS = {
114-
_ResultCodes.IMAGE_TOO_LARGE: ImageTooLarge,
115-
_ResultCodes.METADATA_TOO_LARGE: MetadataTooLarge,
116-
_ResultCodes.TARGET_NAME_EXIST: TargetNameExist,
117-
_ResultCodes.TARGET_STATUS_PROCESSING: TargetStatusProcessing,
118-
_ResultCodes.UNKNOWN_TARGET: UnknownTarget,
119-
}
101+
exception = {
102+
'ImageTooLarge': ImageTooLarge,
103+
'MetadataTooLarge': MetadataTooLarge,
104+
'TargetNameExist': TargetNameExist,
105+
'TargetStatusProcessing': TargetStatusProcessing,
106+
'UnknownTarget': UnknownTarget,
107+
}[result_code]
108+
109+
raise exception(response=response)
120110

121111

122112
class VWS:
@@ -192,12 +182,12 @@ def add_target(
192182
base_vws_url=self._base_vws_url,
193183
)
194184

195-
result_code = response.json()['result_code']
196-
if _ResultCodes(result_code) == _ResultCodes.TARGET_CREATED:
197-
return str(response.json()['target_id'])
185+
_raise_for_result_code(
186+
response=response,
187+
expected_result_code='TargetCreated',
188+
)
198189

199-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
200-
raise exception(response=response)
190+
return str(response.json()['target_id'])
201191

202192
def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
203193
"""
@@ -221,12 +211,11 @@ def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
221211
base_vws_url=self._base_vws_url,
222212
)
223213

224-
result_code = response.json()['result_code']
225-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
226-
return dict(response.json()['target_record'])
227-
228-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
229-
raise exception(response=response)
214+
_raise_for_result_code(
215+
response=response,
216+
expected_result_code='Success',
217+
)
218+
return dict(response.json()['target_record'])
230219

231220
@timeout_decorator.timeout(seconds=60 * 5)
232221
def wait_for_target_processed(self, target_id: str) -> None:
@@ -270,6 +259,10 @@ def list_targets(self) -> List[str]:
270259
base_vws_url=self._base_vws_url,
271260
)
272261

262+
_raise_for_result_code(
263+
response=response,
264+
expected_result_code='Success',
265+
)
273266
return list(response.json()['results'])
274267

275268
def get_target_summary_report(
@@ -297,12 +290,11 @@ def get_target_summary_report(
297290
base_vws_url=self._base_vws_url,
298291
)
299292

300-
result_code = response.json()['result_code']
301-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
302-
return dict(response.json())
303-
304-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
305-
raise exception(response=response)
293+
_raise_for_result_code(
294+
response=response,
295+
expected_result_code='Success',
296+
)
297+
return dict(response.json())
306298

307299
def get_database_summary_report(self) -> Dict[str, Union[str, int]]:
308300
"""
@@ -323,6 +315,11 @@ def get_database_summary_report(self) -> Dict[str, Union[str, int]]:
323315
base_vws_url=self._base_vws_url,
324316
)
325317

318+
_raise_for_result_code(
319+
response=response,
320+
expected_result_code='Success',
321+
)
322+
326323
return dict(response.json())
327324

328325
def delete_target(self, target_id: str) -> None:
@@ -344,9 +341,7 @@ def delete_target(self, target_id: str) -> None:
344341
base_vws_url=self._base_vws_url,
345342
)
346343

347-
result_code = response.json()['result_code']
348-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
349-
return
350-
351-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
352-
raise exception(response=response)
344+
_raise_for_result_code(
345+
response=response,
346+
expected_result_code='Success',
347+
)

0 commit comments

Comments
 (0)