1414from vws .exceptions .base_exceptions import VWSError
1515
1616
17+ def _target_id_from_url (* , url : str ) -> str :
18+ """Return the target ID from a VWS response URL.
19+
20+ Paths may include a custom base URL prefix. The target ID is the
21+ path segment after ``targets``, ``summary``, or ``duplicates``.
22+ """
23+ path = urlparse (url = url ).path
24+ parts = [part for part in path .split (sep = "/" ) if part ]
25+ for marker in ("targets" , "summary" , "duplicates" ):
26+ try :
27+ marker_index = parts .index (marker )
28+ except ValueError :
29+ continue
30+ return parts [marker_index + 1 ]
31+ message = f"Could not find a target ID in URL path { path !r} "
32+ raise ValueError (message )
33+
34+
1735@beartype
1836class UnknownTargetError (VWSError ):
1937 """Exception raised when Vuforia returns a response with a result code
@@ -23,11 +41,7 @@ class UnknownTargetError(VWSError):
2341 @property
2442 def target_id (self ) -> str :
2543 """The unknown target ID."""
26- path = urlparse (url = self .response .url ).path
27- # Every HTTP path which can raise this error has the target ID as the
28- # second path segment, e.g. `/something/{target_id}` or
29- # `/something/{target_id}/more`.
30- return path .split (sep = "/" )[2 ]
44+ return _target_id_from_url (url = self .response .url )
3145
3246
3347@beartype
@@ -68,11 +82,7 @@ class TargetStatusProcessingError(VWSError):
6882 @property
6983 def target_id (self ) -> str :
7084 """The processing target ID."""
71- path = urlparse (url = self .response .url ).path
72- # Every HTTP path which can raise this error has the target ID as the
73- # second path segment, e.g. `/something/{target_id}` or
74- # `/something/{target_id}/more`.
75- return path .split (sep = "/" )[2 ]
85+ return _target_id_from_url (url = self .response .url )
7686
7787
7888# This is not simulated by the mock.
@@ -158,11 +168,7 @@ class TargetStatusNotSuccessError(VWSError):
158168 @property
159169 def target_id (self ) -> str :
160170 """The unknown target ID."""
161- path = urlparse (url = self .response .url ).path
162- # Every HTTP path which can raise this error has the target ID as the
163- # second path segment, e.g. `/something/{target_id}` or
164- # `/something/{target_id}/more`.
165- return path .split (sep = "/" )[2 ]
171+ return _target_id_from_url (url = self .response .url )
166172
167173
168174@beartype
0 commit comments