|
5 | 5 | import base64 |
6 | 6 | import io |
7 | 7 | import json |
| 8 | +from enum import Enum |
8 | 9 | from time import sleep |
9 | 10 | from typing import Dict, List, Union |
10 | 11 | from urllib.parse import urljoin |
|
14 | 15 | from requests import Response |
15 | 16 |
|
16 | 17 | from vws._authorization import authorization_header, rfc_1123_date |
| 18 | +from vws.exceptions import UnknownTarget |
17 | 19 |
|
18 | 20 |
|
19 | 21 | def _target_api_request( |
@@ -74,6 +76,39 @@ def _target_api_request( |
74 | 76 | return response |
75 | 77 |
|
76 | 78 |
|
| 79 | +class _ResultCodes(Enum): |
| 80 | + """ |
| 81 | + Constants representing various VWS result codes. |
| 82 | +
|
| 83 | + See |
| 84 | + https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes |
| 85 | +
|
| 86 | + Some codes here are not documented in the above link. |
| 87 | + """ |
| 88 | + |
| 89 | + SUCCESS = 'Success' |
| 90 | + TARGET_CREATED = 'TargetCreated' |
| 91 | + AUTHENTICATION_FAILURE = 'AuthenticationFailure' |
| 92 | + REQUEST_TIME_TOO_SKEWED = 'RequestTimeTooSkewed' |
| 93 | + TARGET_NAME_EXIST = 'TargetNameExist' |
| 94 | + UNKNOWN_TARGET = 'UnknownTarget' |
| 95 | + BAD_IMAGE = 'BadImage' |
| 96 | + IMAGE_TOO_LARGE = 'ImageTooLarge' |
| 97 | + METADATA_TOO_LARGE = 'MetadataTooLarge' |
| 98 | + DATE_RANGE_ERROR = 'DateRangeError' |
| 99 | + FAIL = 'Fail' |
| 100 | + TARGET_STATUS_PROCESSING = 'TargetStatusProcessing' |
| 101 | + REQUEST_QUOTA_REACHED = 'RequestQuotaReached' |
| 102 | + TARGET_STATUS_NOT_SUCCESS = 'TargetStatusNotSuccess' |
| 103 | + PROJECT_INACTIVE = 'ProjectInactive' |
| 104 | + INACTIVE_PROJECT = 'InactiveProject' |
| 105 | + |
| 106 | + |
| 107 | +_EXCEPTIONS = { |
| 108 | + _ResultCodes.UNKNOWN_TARGET: UnknownTarget, |
| 109 | +} |
| 110 | + |
| 111 | + |
77 | 112 | class VWS: |
78 | 113 | """ |
79 | 114 | An interface to Vuforia Web Services APIs. |
@@ -162,7 +197,12 @@ def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]: |
162 | 197 | base_vws_url=self._base_vws_url, |
163 | 198 | ) |
164 | 199 |
|
165 | | - return dict(response.json()['target_record']) |
| 200 | + result_code = response.json()['result_code'] |
| 201 | + if _ResultCodes(result_code) == _ResultCodes.SUCCESS: |
| 202 | + return dict(response.json()['target_record']) |
| 203 | + |
| 204 | + exception = _EXCEPTIONS[_ResultCodes(result_code)] |
| 205 | + raise exception(response=response) |
166 | 206 |
|
167 | 207 | @timeout_decorator.timeout(seconds=60 * 5) |
168 | 208 | def wait_for_target_processed(self, target_id: str) -> None: |
|
0 commit comments