|
6 | 6 | import io |
7 | 7 | import json |
8 | 8 | from datetime import date |
| 9 | +from time import sleep |
9 | 10 | from typing import Dict, List, Optional, Union |
10 | 11 | from urllib.parse import urljoin |
11 | 12 |
|
12 | 13 | import requests |
13 | 14 | from requests import Response |
| 15 | +from timeout_decorator import timeout |
14 | 16 | from vws_auth_tools import authorization_header, rfc_1123_date |
15 | 17 |
|
16 | | -from func_timeout.exceptions import FunctionTimedOut |
17 | | -from vws.exceptions import TargetProcessingTimeout |
18 | 18 | from vws._result_codes import raise_for_result_code |
19 | | -from vws._wait_for_target_processed import foobar |
| 19 | +from vws.exceptions import TargetProcessingTimeout |
20 | 20 | from vws.reports import ( |
21 | 21 | DatabaseSummaryReport, |
22 | 22 | TargetRecord, |
@@ -255,6 +255,38 @@ def get_target_record(self, target_id: str) -> TargetRecord: |
255 | 255 | ) |
256 | 256 | return target_record |
257 | 257 |
|
| 258 | + def _wait_for_target_processed( |
| 259 | + self, |
| 260 | + target_id: str, |
| 261 | + seconds_between_requests: float, |
| 262 | + ) -> None: |
| 263 | + """ |
| 264 | + Wait indefinitely for a target to get past the processing stage. |
| 265 | +
|
| 266 | + Args: |
| 267 | + target_id: The ID of the target to wait for. |
| 268 | + seconds_between_requests: The number of seconds to wait between |
| 269 | + requests made while polling the target status. |
| 270 | +
|
| 271 | + Raises: |
| 272 | + ~vws.exceptions.AuthenticationFailure: The secret key is not |
| 273 | + correct. |
| 274 | + ~vws.exceptions.Fail: There was an error with the request. For |
| 275 | + example, the given access key does not match a known database. |
| 276 | + TimeoutError: The target remained in the processing stage for more |
| 277 | + than five minutes. |
| 278 | + ~vws.exceptions.UnknownTarget: The given target ID does not match a |
| 279 | + target in the database. |
| 280 | + ~vws.exceptions.RequestTimeTooSkewed: There is an error with the |
| 281 | + time sent to Vuforia. |
| 282 | + """ |
| 283 | + while True: |
| 284 | + report = self.get_target_summary_report(target_id=target_id) |
| 285 | + if report.status != TargetStatuses.PROCESSING: |
| 286 | + return |
| 287 | + |
| 288 | + sleep(seconds_between_requests) |
| 289 | + |
258 | 290 | def wait_for_target_processed( |
259 | 291 | self, |
260 | 292 | target_id: str, |
@@ -288,15 +320,18 @@ def wait_for_target_processed( |
288 | 320 | ~vws.exceptions.RequestTimeTooSkewed: There is an error with the |
289 | 321 | time sent to Vuforia. |
290 | 322 | """ |
291 | | - try: |
292 | | - foobar( |
293 | | - vws_client=self, |
| 323 | + |
| 324 | + @timeout( |
| 325 | + seconds=timeout_seconds, |
| 326 | + timeout_exception=TargetProcessingTimeout, |
| 327 | + ) |
| 328 | + def decorated() -> None: |
| 329 | + self._wait_for_target_processed( |
294 | 330 | target_id=target_id, |
295 | | - timeout_seconds=timeout_seconds, |
296 | 331 | seconds_between_requests=seconds_between_requests, |
297 | 332 | ) |
298 | | - except FunctionTimedOut: |
299 | | - raise TargetProcessingTimeout |
| 333 | + |
| 334 | + decorated() |
300 | 335 |
|
301 | 336 | def list_targets(self) -> List[str]: |
302 | 337 | """ |
|
0 commit comments