Skip to content

Commit 1337650

Browse files
adamtheturtleclaude
andcommitted
Add custom request timeout support for VWS and CloudRecoService
Allow users to configure request timeouts instead of using a hardcoded 30-second default. Both VWS and CloudRecoService now accept an optional request_timeout_seconds parameter during initialization, with proper defaults and documentation. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent b20b4bd commit 1337650

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

src/vws/query.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ def __init__(
4949
client_access_key: str,
5050
client_secret_key: str,
5151
base_vwq_url: str = "https://cloudreco.vuforia.com",
52+
request_timeout_seconds: float = 30.0,
5253
) -> None:
5354
"""
5455
Args:
5556
client_access_key: A VWS client access key.
5657
client_secret_key: A VWS client secret key.
5758
base_vwq_url: The base URL for the VWQ API.
59+
request_timeout_seconds: The timeout in seconds for each HTTP
60+
request made to the Cloud Reco API.
5861
"""
5962
self._client_access_key = client_access_key
6063
self._client_secret_key = client_secret_key
6164
self._base_vwq_url = base_vwq_url
65+
self.request_timeout_seconds = request_timeout_seconds
6266

6367
def query(
6468
self,
@@ -141,8 +145,7 @@ def query(
141145
url=urljoin(base=self._base_vwq_url, url=request_path),
142146
headers=headers,
143147
data=content,
144-
# We should make the timeout customizable.
145-
timeout=30,
148+
timeout=self.request_timeout_seconds,
146149
)
147150
response = Response(
148151
text=requests_response.text,

src/vws/vws.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _target_api_request(
6868
data: bytes,
6969
request_path: str,
7070
base_vws_url: str,
71+
request_timeout_seconds: float,
7172
) -> Response:
7273
"""Make a request to the Vuforia Target API.
7374
@@ -82,6 +83,7 @@ def _target_api_request(
8283
request_path: The path to the endpoint which will be used in the
8384
request.
8485
base_vws_url: The base URL for the VWS API.
86+
request_timeout_seconds: The timeout in seconds for the request.
8587
8688
Returns:
8789
The response to the request made by `requests`.
@@ -111,8 +113,7 @@ def _target_api_request(
111113
url=url,
112114
headers=headers,
113115
data=data,
114-
# We should make the timeout customizable.
115-
timeout=30,
116+
timeout=request_timeout_seconds,
116117
)
117118

118119
return Response(
@@ -134,16 +135,20 @@ def __init__(
134135
server_access_key: str,
135136
server_secret_key: str,
136137
base_vws_url: str = "https://vws.vuforia.com",
138+
request_timeout_seconds: float = 30.0,
137139
) -> None:
138140
"""
139141
Args:
140142
server_access_key: A VWS server access key.
141143
server_secret_key: A VWS server secret key.
142144
base_vws_url: The base URL for the VWS API.
145+
request_timeout_seconds: The timeout in seconds for each HTTP
146+
request made to the VWS API.
143147
"""
144148
self._server_access_key = server_access_key
145149
self._server_secret_key = server_secret_key
146150
self._base_vws_url = base_vws_url
151+
self.request_timeout_seconds = request_timeout_seconds
147152

148153
def make_request(
149154
self,
@@ -187,6 +192,7 @@ def make_request(
187192
data=data,
188193
request_path=request_path,
189194
base_vws_url=self._base_vws_url,
195+
request_timeout_seconds=self.request_timeout_seconds,
190196
)
191197

192198
if (

tests/test_query.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,54 @@ def test_match(
4242
assert matching_target.target_id == target_id
4343

4444

45+
class TestCustomRequestTimeout:
46+
"""Tests for using a custom request timeout."""
47+
48+
@staticmethod
49+
def test_default_timeout() -> None:
50+
"""By default, the request timeout is 30 seconds."""
51+
default_timeout_seconds = 30.0
52+
with MockVWS() as mock:
53+
database = VuforiaDatabase()
54+
mock.add_database(database=database)
55+
cloud_reco_client = CloudRecoService(
56+
client_access_key=database.client_access_key,
57+
client_secret_key=database.client_secret_key,
58+
)
59+
expected = default_timeout_seconds
60+
assert cloud_reco_client.request_timeout_seconds == expected
61+
62+
@staticmethod
63+
def test_custom_timeout(image: io.BytesIO | BinaryIO) -> None:
64+
"""It is possible to set a custom request timeout."""
65+
with MockVWS() as mock:
66+
database = VuforiaDatabase()
67+
mock.add_database(database=database)
68+
vws_client = VWS(
69+
server_access_key=database.server_access_key,
70+
server_secret_key=database.server_secret_key,
71+
)
72+
custom_timeout = 60.5
73+
cloud_reco_client = CloudRecoService(
74+
client_access_key=database.client_access_key,
75+
client_secret_key=database.client_secret_key,
76+
request_timeout_seconds=custom_timeout,
77+
)
78+
assert cloud_reco_client.request_timeout_seconds == custom_timeout
79+
80+
# Verify requests work with the custom timeout
81+
target_id = vws_client.add_target(
82+
name="x",
83+
width=1,
84+
image=image,
85+
active_flag=True,
86+
application_metadata=None,
87+
)
88+
vws_client.wait_for_target_processed(target_id=target_id)
89+
matches = cloud_reco_client.query(image=image)
90+
assert len(matches) == 1
91+
92+
4593
class TestCustomBaseVWQURL:
4694
"""Tests for using a custom base VWQ URL."""
4795

tests/test_vws.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,47 @@ def test_add_two_targets(
9292
)
9393

9494

95+
class TestCustomRequestTimeout:
96+
"""Tests for using a custom request timeout."""
97+
98+
@staticmethod
99+
def test_default_timeout() -> None:
100+
"""By default, the request timeout is 30 seconds."""
101+
default_timeout_seconds = 30.0
102+
with MockVWS() as mock:
103+
database = VuforiaDatabase()
104+
mock.add_database(database=database)
105+
vws_client = VWS(
106+
server_access_key=database.server_access_key,
107+
server_secret_key=database.server_secret_key,
108+
)
109+
expected = default_timeout_seconds
110+
assert vws_client.request_timeout_seconds == expected
111+
112+
@staticmethod
113+
def test_custom_timeout(image: io.BytesIO | BinaryIO) -> None:
114+
"""It is possible to set a custom request timeout."""
115+
with MockVWS() as mock:
116+
database = VuforiaDatabase()
117+
mock.add_database(database=database)
118+
custom_timeout = 60.5
119+
vws_client = VWS(
120+
server_access_key=database.server_access_key,
121+
server_secret_key=database.server_secret_key,
122+
request_timeout_seconds=custom_timeout,
123+
)
124+
assert vws_client.request_timeout_seconds == custom_timeout
125+
126+
# Verify requests work with the custom timeout
127+
vws_client.add_target(
128+
name="x",
129+
width=1,
130+
image=image,
131+
active_flag=True,
132+
application_metadata=None,
133+
)
134+
135+
95136
class TestCustomBaseVWSURL:
96137
"""Tests for using a custom base VWS URL."""
97138

0 commit comments

Comments
 (0)