77import json
88from enum import Enum
99from time import sleep
10- from typing import Dict , List , Union
10+ from typing import Dict , List , Optional , Union
1111from urllib .parse import urljoin
1212
1313import requests
1414import timeout_decorator
1515from requests import Response
1616
1717from vws ._authorization import authorization_header , rfc_1123_date
18- from vws .exceptions import TargetStatusProcessing , UnknownTarget
18+ from vws .exceptions import (
19+ MetadataTooLarge ,
20+ TargetStatusProcessing ,
21+ UnknownTarget ,
22+ )
1923
2024
2125def _target_api_request (
@@ -107,6 +111,7 @@ class _ResultCodes(Enum):
107111_EXCEPTIONS = {
108112 _ResultCodes .UNKNOWN_TARGET : UnknownTarget ,
109113 _ResultCodes .TARGET_STATUS_PROCESSING : TargetStatusProcessing ,
114+ _ResultCodes .METADATA_TOO_LARGE : MetadataTooLarge ,
110115}
111116
112117
@@ -137,6 +142,7 @@ def add_target(
137142 width : Union [int , float ],
138143 image : io .BytesIO ,
139144 active_flag : bool = True ,
145+ application_metadata : Optional [bytes ] = None ,
140146 ) -> str :
141147 """
142148 Add a target to a Vuforia Web Services database.
@@ -149,18 +155,26 @@ def add_target(
149155 width: The width of the target.
150156 image: The image of the target.
151157 active_flag: Whether or not the target is active for query.
158+ application_metadata: The application metadata of the target.
159+ This will be base64 encoded.
152160
153161 Returns:
154162 The target ID of the new target.
155163 """
156164 image_data = image .getvalue ()
157165 image_data_encoded = base64 .b64encode (image_data ).decode ('ascii' )
166+ if application_metadata is None :
167+ metadata_encoded = None
168+ else :
169+ metadata_encoded_str = base64 .b64encode (application_metadata )
170+ metadata_encoded = metadata_encoded_str .decode ('ascii' )
158171
159172 data = {
160173 'name' : name ,
161174 'width' : width ,
162175 'image' : image_data_encoded ,
163176 'active_flag' : active_flag ,
177+ 'application_metadata' : metadata_encoded ,
164178 }
165179
166180 content = bytes (json .dumps (data ), encoding = 'utf-8' )
@@ -174,7 +188,12 @@ def add_target(
174188 base_vws_url = self ._base_vws_url ,
175189 )
176190
177- return str (response .json ()['target_id' ])
191+ result_code = response .json ()['result_code' ]
192+ if _ResultCodes (result_code ) == _ResultCodes .TARGET_CREATED :
193+ return str (response .json ()['target_id' ])
194+
195+ exception = _EXCEPTIONS [_ResultCodes (result_code )]
196+ raise exception (response = response )
178197
179198 def get_target_record (self , target_id : str ) -> Dict [str , Union [str , int ]]:
180199 """
0 commit comments