Skip to content

Commit 25118c5

Browse files
Merge pull request #793 from adamtheturtle/wait-for-target-processed
Add initial function to wait for target processed
2 parents 5b8e7af + c217ecd commit 25118c5

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests==2.19.1
2+
timeout-decorator==0.4.0

src/vws/vws.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import base64
66
import io
77
import json
8+
from time import sleep
89
from typing import Any, Dict, Union
910
from urllib.parse import urljoin
1011

1112
import requests
13+
import timeout_decorator
1214
from requests import Response
1315

1416
from vws._authorization import authorization_header, rfc_1123_date
@@ -154,3 +156,26 @@ def get_target(self, target_id: str) -> Dict[str, Any]:
154156
)
155157

156158
return dict(response.json())
159+
160+
@timeout_decorator.timeout(seconds=60 * 5)
161+
def wait_for_target_processed(self, target_id: str) -> None:
162+
"""
163+
Wait up to five minutes (arbitrary) for a target to get past the
164+
processing stage.
165+
166+
Args:
167+
target_id: The ID of the target to wait for.
168+
169+
Raises:
170+
TimeoutError: The target remained in the processing stage for more
171+
than five minutes.
172+
"""
173+
while True:
174+
target_details = self.get_target(target_id=target_id)
175+
if target_details['status'] != 'processing':
176+
return
177+
178+
# We wait 0.2 seconds rather than less than that to decrease the
179+
# number of calls made to the API, to decrease the likelihood of
180+
# hitting the request quota.
181+
sleep(0.2)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Tests for helper function for waiting for a target to be processed.
3+
"""
4+
5+
import io
6+
7+
from vws import VWS
8+
9+
10+
class TestWaitForTargetProcessed:
11+
"""
12+
Test for successfully adding a target.
13+
"""
14+
15+
def test_wait_for_target_processed(
16+
self,
17+
client: VWS,
18+
high_quality_image: io.BytesIO,
19+
) -> None:
20+
"""
21+
It is possible to wait until a target is processed.
22+
"""
23+
target_id = client.add_target(
24+
name='x',
25+
width=1,
26+
image=high_quality_image,
27+
)
28+
target_details = client.get_target(target_id=target_id)
29+
assert target_details['status'] == 'processing'
30+
client.wait_for_target_processed(target_id=target_id)
31+
target_details = client.get_target(target_id=target_id)
32+
assert target_details['status'] != 'processing'

0 commit comments

Comments
 (0)