Skip to content

Commit 5f03bef

Browse files
committed
Add request_timeout param to get_detector and add tests
1 parent ea19514 commit 5f03bef

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/groundlight/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ def _user_is_privileged(self) -> bool:
270270
obj = self.user_api.who_am_i()
271271
return obj["is_superuser"]
272272

273-
def get_detector(self, id: Union[str, Detector]) -> Detector: # pylint: disable=redefined-builtin
273+
def get_detector(
274+
self,
275+
id: Union[str, Detector],
276+
request_timeout: Optional[float] = None,
277+
) -> Detector: # pylint: disable=redefined-builtin
274278
"""
275279
Get a Detector by id.
276280
@@ -281,6 +285,8 @@ def get_detector(self, id: Union[str, Detector]) -> Detector: # pylint: disable
281285
print(detector)
282286
283287
:param id: the detector id
288+
:param request_timeout: The request timeout for the image query submission API request. Most users will not need
289+
to modify this. If not set, the default value will be used.
284290
285291
:return: Detector
286292
"""
@@ -289,7 +295,8 @@ def get_detector(self, id: Union[str, Detector]) -> Detector: # pylint: disable
289295
# Short-circuit
290296
return id
291297
try:
292-
obj = self.detectors_api.get_detector(id=id, _request_timeout=DEFAULT_REQUEST_TIMEOUT)
298+
request_timeout = request_timeout if request_timeout is not None else DEFAULT_REQUEST_TIMEOUT
299+
obj = self.detectors_api.get_detector(id=id, _request_timeout=request_timeout)
293300
except NotFoundException as e:
294301
raise NotFoundError(f"Detector with id '{id}' not found") from e
295302
return Detector.parse_obj(obj.to_dict())
@@ -750,8 +757,8 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
750757
:param image_query_id: The ID for the image query. This is to enable specific functionality
751758
and is not intended for general external use. If not set, a random ID
752759
will be generated.
753-
:param request_timeout: The total request timeout for the image query submission API request. Most users will
754-
not need to modify this. If not set, the default value will be used.
760+
:param request_timeout: The request timeout for the image query submission API request. Most users will not need
761+
to modify this. If not set, the default value will be used.
755762
756763
:return: ImageQuery with query details and result (if wait > 0)
757764
:raises ValueError: If wait > 0 when want_async=True

test/integration/test_groundlight.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ def test_get_detector(gl: Groundlight, detector: Detector):
222222
assert isinstance(_detector, Detector)
223223

224224

225+
def test_get_detector_with_low_request_timeout(gl: Groundlight, detector: Detector):
226+
"""
227+
Verifies that get_detector respects the request_timeout parameter and raises a ReadTimeoutError when timeout is
228+
exceeded. Verifies that request_timeout parameter can be a float or a tuple.
229+
"""
230+
with pytest.raises(ReadTimeoutError):
231+
# Setting a very low request_timeout value should result in a timeout.
232+
# NOTE: request_timeout=0 seems to have special behavior that does not result in a timeout.
233+
gl.get_detector(id=detector.id, request_timeout=1e-8)
234+
235+
with pytest.raises(ReadTimeoutError):
236+
# Ensure a tuple can be passed.
237+
gl.get_detector(id=detector.id, request_timeout=(1e-8, 1e-8))
238+
239+
225240
def test_get_detector_by_name(gl: Groundlight, detector: Detector):
226241
_detector = gl.get_detector_by_name(name=detector.name)
227242
assert str(_detector)
@@ -352,14 +367,18 @@ def test_submit_image_query_with_human_review_param(gl: Groundlight, detector: D
352367

353368
def test_submit_image_query_with_low_request_timeout(gl: Groundlight, detector: Detector, image: str):
354369
"""
355-
Test that submit_image_query respects the request_timeout parameter and raises a ReadTimeoutError when timeout is
356-
exceeded.
370+
Verifies that submit_image_query respects the request_timeout parameter and raises a ReadTimeoutError when timeout is
371+
exceeded. Verifies that request_timeout parameter can be a float or a tuple.
357372
"""
358373
with pytest.raises(ReadTimeoutError):
359374
# Setting a very low request_timeout value should result in a timeout.
360375
# NOTE: request_timeout=0 seems to have special behavior that does not result in a timeout.
361376
gl.submit_image_query(detector=detector, image=image, human_review="NEVER", request_timeout=1e-8)
362377

378+
with pytest.raises(ReadTimeoutError):
379+
# Ensure a tuple can be passed.
380+
gl.submit_image_query(detector=detector, image=image, human_review="NEVER", request_timeout=(1e-8, 1e-8))
381+
363382

364383
@pytest.mark.skip_for_edge_endpoint(reason="The edge-endpoint does not support passing detector metadata.")
365384
def test_create_detector_with_metadata(gl: Groundlight):

0 commit comments

Comments
 (0)