55import base64
66import io
77import json
8+ from enum import Enum
89from time import sleep
910from typing import Dict , List , Union
1011from urllib .parse import urljoin
1415from requests import Response
1516
1617from vws ._authorization import authorization_header , rfc_1123_date
18+ from vws .exceptions import UnknownTarget
1719
1820
1921def _target_api_request (
@@ -74,6 +76,39 @@ def _target_api_request(
7476 return response
7577
7678
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+
77112class VWS :
78113 """
79114 An interface to Vuforia Web Services APIs.
@@ -162,7 +197,12 @@ def get_target_record(self, target_id: str) -> Dict[str, Union[str, int]]:
162197 base_vws_url = self ._base_vws_url ,
163198 )
164199
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 )
166206
167207 @timeout_decorator .timeout (seconds = 60 * 5 )
168208 def wait_for_target_processed (self , target_id : str ) -> None :
@@ -187,11 +227,6 @@ def wait_for_target_processed(self, target_id: str) -> None:
187227 # hitting the request quota.
188228 sleep (0.2 )
189229
190- # We wait 0.2 seconds rather than less than that to decrease the
191- # number of calls made to the API, to decrease the likelihood of
192- # hitting the request quota.
193- sleep (0.2 )
194-
195230 def list_targets (self ) -> List [str ]:
196231 """
197232 List target IDs.
@@ -232,7 +267,12 @@ def get_target_summary_report(
232267 base_vws_url = self ._base_vws_url ,
233268 )
234269
235- return dict (response .json ())
270+ result_code = response .json ()['result_code' ]
271+ if _ResultCodes (result_code ) == _ResultCodes .SUCCESS :
272+ return dict (response .json ())
273+
274+ exception = _EXCEPTIONS [_ResultCodes (result_code )]
275+ raise exception (response = response )
236276
237277 def get_database_summary_report (self ) -> Dict [str , Union [str , int ]]:
238278 """
@@ -259,3 +299,18 @@ def delete_target(self, target_id: str) -> None:
259299 Args:
260300 target_id: The ID of the target to delete.
261301 """
302+ response = _target_api_request (
303+ server_access_key = self ._server_access_key ,
304+ server_secret_key = self ._server_secret_key ,
305+ method = 'GET' ,
306+ content = b'' ,
307+ request_path = f'/summary/{ target_id } ' ,
308+ base_vws_url = self ._base_vws_url ,
309+ )
310+
311+ result_code = response .json ()['result_code' ]
312+ if _ResultCodes (result_code ) == _ResultCodes .SUCCESS :
313+ return
314+
315+ exception = _EXCEPTIONS [_ResultCodes (result_code )]
316+ raise exception (response = response )
0 commit comments