Skip to content

Commit 9e451a0

Browse files
committed
Merge branch 'simpler-exceptions' into client
2 parents 8489d59 + 0cfa9a4 commit 9e451a0

1 file changed

Lines changed: 60 additions & 64 deletions

File tree

src/vws/vws.py

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -91,50 +91,41 @@ def _target_api_request(
9191
return response
9292

9393

94-
class _ResultCodes(Enum):
94+
def _raise_for_result_code(
95+
response: Response,
96+
expected_result_code: str,
97+
) -> None:
9598
"""
96-
Constants representing various VWS result codes.
99+
Raise an appropriate exception if the expected result code for a successful
100+
request is not returned.
97101
98-
See
99-
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
100-
101-
Some codes here are not documented in the above link.
102+
Args:
103+
response: A response from Vuforia.
104+
expected_result_code: See
105+
https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
102106
"""
103-
104-
SUCCESS = 'Success'
105-
TARGET_CREATED = 'TargetCreated'
106-
AUTHENTICATION_FAILURE = 'AuthenticationFailure'
107-
REQUEST_TIME_TOO_SKEWED = 'RequestTimeTooSkewed'
108-
TARGET_NAME_EXIST = 'TargetNameExist'
109-
UNKNOWN_TARGET = 'UnknownTarget'
110-
BAD_IMAGE = 'BadImage'
111-
IMAGE_TOO_LARGE = 'ImageTooLarge'
112-
METADATA_TOO_LARGE = 'MetadataTooLarge'
113-
DATE_RANGE_ERROR = 'DateRangeError'
114-
FAIL = 'Fail'
115-
TARGET_STATUS_PROCESSING = 'TargetStatusProcessing'
116-
REQUEST_QUOTA_REACHED = 'RequestQuotaReached'
117-
TARGET_STATUS_NOT_SUCCESS = 'TargetStatusNotSuccess'
118-
PROJECT_INACTIVE = 'ProjectInactive'
119-
INACTIVE_PROJECT = 'InactiveProject'
120-
121-
122-
_EXCEPTIONS = {
123-
_ResultCodes.AUTHENTICATION_FAILURE: AuthenticationFailure,
124-
_ResultCodes.BAD_IMAGE: BadImage,
125-
_ResultCodes.DATE_RANGE_ERROR: DateRangeError,
126-
_ResultCodes.FAIL: Fail,
127-
_ResultCodes.IMAGE_TOO_LARGE: ImageTooLarge,
128-
_ResultCodes.INACTIVE_PROJECT: InactiveProject,
129-
_ResultCodes.METADATA_TOO_LARGE: MetadataTooLarge,
130-
_ResultCodes.PROJECT_INACTIVE: ProjectInactive,
131-
_ResultCodes.REQUEST_QUOTA_REACHED: RequestQuotaReached,
132-
_ResultCodes.REQUEST_TIME_TOO_SKEWED: RequestTimeTooSkewed,
133-
_ResultCodes.TARGET_NAME_EXIST: TargetNameExist,
134-
_ResultCodes.TARGET_STATUS_NOT_SUCCESS: TargetStatusNotSuccess,
135-
_ResultCodes.TARGET_STATUS_PROCESSING: TargetStatusProcessing,
136-
_ResultCodes.UNKNOWN_TARGET: UnknownTarget,
137-
}
107+
result_code = response.json()['result_code']
108+
if result_code == expected_result_code:
109+
return
110+
111+
exception = {
112+
'AuthenticationFailure': AuthenticationFailure,
113+
'BadImage': BadImage,
114+
'DateRangeError': DateRangeError,
115+
'Fail': Fail,
116+
'ImageTooLarge': ImageTooLarge,
117+
'InactiveProject': InactiveProject,
118+
'MetadataTooLarge': MetadataTooLarge,
119+
'ProjectInactive': ProjectInactive,
120+
'RequestTimeTooSkewed': RequestTimeTooSkewed,
121+
'TargetStatusNotSuccess': TargetStatusNotSuccess,
122+
'RequestQuotaReached': RequestQuotaReached,
123+
'TargetNameExist': TargetNameExist,
124+
'TargetStatusProcessing': TargetStatusProcessing,
125+
'UnknownTarget': UnknownTarget,
126+
}[result_code]
127+
128+
raise exception(response=response)
138129

139130

140131
class VWS:
@@ -211,12 +202,12 @@ def add_target(
211202
base_vws_url=self._base_vws_url,
212203
)
213204

214-
result_code = response.json()['result_code']
215-
if _ResultCodes(result_code) == _ResultCodes.TARGET_CREATED:
216-
return str(response.json()['target_id'])
205+
_raise_for_result_code(
206+
response=response,
207+
expected_result_code='TargetCreated',
208+
)
217209

218-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
219-
raise exception(response=response)
210+
return str(response.json()['target_id'])
220211

221212
def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
222213
"""
@@ -240,12 +231,11 @@ def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
240231
base_vws_url=self._base_vws_url,
241232
)
242233

243-
result_code = response.json()['result_code']
244-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
245-
return dict(response.json()['target_record'])
246-
247-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
248-
raise exception(response=response)
234+
_raise_for_result_code(
235+
response=response,
236+
expected_result_code='Success',
237+
)
238+
return dict(response.json()['target_record'])
249239

250240
@timeout_decorator.timeout(seconds=60 * 5)
251241
def wait_for_target_processed(self, target_id: str) -> None:
@@ -289,6 +279,10 @@ def list_targets(self) -> List[str]:
289279
base_vws_url=self._base_vws_url,
290280
)
291281

282+
_raise_for_result_code(
283+
response=response,
284+
expected_result_code='Success',
285+
)
292286
return list(response.json()['results'])
293287

294288
def get_target_summary_report(
@@ -316,12 +310,11 @@ def get_target_summary_report(
316310
base_vws_url=self._base_vws_url,
317311
)
318312

319-
result_code = response.json()['result_code']
320-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
321-
return dict(response.json())
322-
323-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
324-
raise exception(response=response)
313+
_raise_for_result_code(
314+
response=response,
315+
expected_result_code='Success',
316+
)
317+
return dict(response.json())
325318

326319
def get_database_summary_report(self) -> Dict[str, Union[str, int]]:
327320
"""
@@ -342,6 +335,11 @@ def get_database_summary_report(self) -> Dict[str, Union[str, int]]:
342335
base_vws_url=self._base_vws_url,
343336
)
344337

338+
_raise_for_result_code(
339+
response=response,
340+
expected_result_code='Success',
341+
)
342+
345343
return dict(response.json())
346344

347345
def delete_target(self, target_id: str) -> None:
@@ -363,9 +361,7 @@ def delete_target(self, target_id: str) -> None:
363361
base_vws_url=self._base_vws_url,
364362
)
365363

366-
result_code = response.json()['result_code']
367-
if _ResultCodes(result_code) == _ResultCodes.SUCCESS:
368-
return
369-
370-
exception = _EXCEPTIONS[_ResultCodes(result_code)]
371-
raise exception(response=response)
364+
_raise_for_result_code(
365+
response=response,
366+
expected_result_code='Success',
367+
)

0 commit comments

Comments
 (0)