Skip to content

Commit f0d188f

Browse files
Allow configurable timeouts for the scan methods that submit a large byte body.
1 parent 16642d2 commit f0d188f

File tree

1 file changed

+56
-12
lines changed
  • arachnid_shield_sdk/api

1 file changed

+56
-12
lines changed

arachnid_shield_sdk/api/v1.py

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ def __init__(self, username: typing.Union[str, bytes], password: typing.Union[st
3030
super().__init__(username=username, password=password)
3131
self.__client = super()._build_sync_http_client()
3232

33-
def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO], mime_type: str) -> ScannedMedia:
33+
def scan_media_from_bytes(
34+
self,
35+
contents: typing.Union[bytes, io.BytesIO],
36+
mime_type: str,
37+
timeout: typing.Optional[httpx.Timeout] = None,
38+
) -> ScannedMedia:
3439
"""Given the contents of some media, along with a mime type,
3540
scan the contents for matches against known child abuse media.
3641
3742
Args:
3843
contents: The raw bytes that represent the media.
3944
mime_type: The mimetype of the media.
45+
timeout:
46+
If provided, will set a timeout configuration for the underlying http client.
47+
Otherwise, will disable the timeout entirely.
4048
4149
Returns:
4250
The record of a successful media scan.
@@ -45,10 +53,13 @@ def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO], mime_
4553
`ArachnidShieldError` on a failed but complete interaction with
4654
the Arachnid Shield API, and `httpx.HTTPError` on any other connection failures.
4755
"""
48-
return self.scan_media_from_bytes_with_config(ScanMediaFromBytes(contents=contents, mime_type=mime_type))
56+
return self.scan_media_from_bytes_with_config(ScanMediaFromBytes(contents=contents, mime_type=mime_type), timeout=timeout)
4957

5058
def scan_media_from_file(
51-
self, filepath: pathlib.Path, mime_type_override: typing.Optional[str] = None
59+
self,
60+
filepath: pathlib.Path,
61+
mime_type_override: typing.Optional[str] = None,
62+
timeout: typing.Optional[httpx.Timeout] = None,
5263
) -> ScannedMedia:
5364
"""Given path to the media file to scan, and an optional
5465
value for mime_type that bypasses guessing it based of the filepath,
@@ -60,6 +71,9 @@ def scan_media_from_file(
6071
mime_type_override:
6172
If provided, will use this as the mime_type
6273
instead of guessing it from the filepath.
74+
timeout:
75+
If provided, will set a timeout configuration for the underlying http client.
76+
Otherwise, will disable the timeout entirely.
6377
6478
Returns:
6579
The record of a successful media scan.
@@ -78,7 +92,7 @@ def scan_media_from_file(
7892
detail=(
7993
f"Failed to identify mime_type for {filepath}. "
8094
f"You may specify it explicitly by providing "
81-
f"`force_mime_type`."
95+
f"`mime_type_override`."
8296
)
8397
)
8498
)
@@ -87,7 +101,7 @@ def scan_media_from_file(
87101
contents = f.read()
88102

89103
config = ScanMediaFromBytes(contents=contents, mime_type=mime_type)
90-
return self.scan_media_from_bytes_with_config(config)
104+
return self.scan_media_from_bytes_with_config(config, timeout=timeout)
91105

92106
def scan_media_from_url(self, url: str) -> ScannedMedia:
93107
"""Given the absolute url that hosts the media we wish to scan,
@@ -105,12 +119,19 @@ def scan_media_from_url(self, url: str) -> ScannedMedia:
105119
"""
106120
return self.scan_media_from_url_with_config(ScanMediaFromUrl(url=url))
107121

108-
def scan_media_from_bytes_with_config(self, config: ScanMediaFromBytes) -> ScannedMedia:
122+
def scan_media_from_bytes_with_config(
123+
self,
124+
config: ScanMediaFromBytes,
125+
timeout: typing.Optional[httpx.Timeout] = httpx.Timeout(5)
126+
) -> ScannedMedia:
109127
"""Given the contents of some media, along with a mime type,
110128
scan the contents for matches against known child abuse media.
111129
112130
Args:
113131
config: The context that will be used to build the request.
132+
timeout:
133+
If provided explicitly, a configuration passed to the underlying http client.
134+
It defaults to 5 seconds, and can be disabled by setting it to `None`.
114135
115136
Returns:
116137
ScannedMedia: A record of a successful scan of the media.
@@ -125,6 +146,7 @@ def scan_media_from_bytes_with_config(self, config: ScanMediaFromBytes) -> Scann
125146
url=url,
126147
headers={"Content-Type": config.mime_type},
127148
content=config.contents,
149+
timeout=timeout,
128150
)
129151

130152
if response.is_client_error or response.is_server_error:
@@ -203,13 +225,21 @@ def __init__(self, username: typing.Union[str, bytes], password: typing.Union[st
203225
super().__init__(username=username, password=password)
204226
self.__client = super()._build_async_http_client()
205227

206-
async def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO], mime_type: str) -> ScannedMedia:
228+
async def scan_media_from_bytes(
229+
self,
230+
contents: typing.Union[bytes, io.BytesIO],
231+
mime_type: str,
232+
timeout: typing.Optional[httpx.Timeout] = None,
233+
) -> ScannedMedia:
207234
"""Given the contents of some media, along with a mime type,
208235
scan the contents for matches against known child abuse media.
209236
210237
Args:
211238
contents: The raw bytes that represent the media.
212239
mime_type: The mimetype of the media.
240+
timeout:
241+
If provided, will set a timeout configuration for the underlying http client.
242+
Otherwise, will disable the timeout entirely.
213243
214244
Returns:
215245
The record of a successful media scan.
@@ -219,7 +249,7 @@ async def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO],
219249
the Arachnid Shield API, and `httpx.HTTPError` on any other connection failures.
220250
"""
221251

222-
return await self.scan_media_from_bytes_with_config(ScanMediaFromBytes(contents=contents, mime_type=mime_type))
252+
return await self.scan_media_from_bytes_with_config(ScanMediaFromBytes(contents=contents, mime_type=mime_type), timeout=timeout)
223253

224254
async def scan_media_from_url(self, url: str) -> ScannedMedia:
225255
"""Given the absolute url that hosts the media we wish to scan,
@@ -238,7 +268,10 @@ async def scan_media_from_url(self, url: str) -> ScannedMedia:
238268
return await self.scan_media_from_url_with_config(ScanMediaFromUrl(url=url))
239269

240270
async def scan_media_from_file(
241-
self, filepath: pathlib.Path, mime_type_override: typing.Optional[str] = None
271+
self,
272+
filepath: pathlib.Path,
273+
mime_type_override: typing.Optional[str] = None,
274+
timeout: typing.Optional[httpx.Timeout] = None,
242275
) -> ScannedMedia:
243276
"""Given path to the media file to scan, and an optional
244277
value for mime_type that bypasses guessing it based of the filepath,
@@ -250,6 +283,9 @@ async def scan_media_from_file(
250283
mime_type_override:
251284
If provided, will use this as the mime_type
252285
instead of guessing it from the filepath.
286+
timeout:
287+
If provided, will set a timeout configuration for the underlying http client.
288+
Otherwise, will disable the timeout entirely.
253289
254290
Returns:
255291
The record of a successful media scan.
@@ -268,7 +304,7 @@ async def scan_media_from_file(
268304
detail=(
269305
f"Failed to identify mime_type for {filepath}. "
270306
f"You may specify it explicitly by providing "
271-
f"`force_mime_type`."
307+
f"`mime_type_override`."
272308
)
273309
)
274310
)
@@ -277,14 +313,21 @@ async def scan_media_from_file(
277313
contents = f.read()
278314

279315
config = ScanMediaFromBytes(contents=contents, mime_type=mime_type)
280-
return await self.scan_media_from_bytes_with_config(config)
316+
return await self.scan_media_from_bytes_with_config(config, timeout=timeout)
281317

282-
async def scan_media_from_bytes_with_config(self, config: ScanMediaFromBytes) -> ScannedMedia:
318+
async def scan_media_from_bytes_with_config(
319+
self,
320+
config: ScanMediaFromBytes,
321+
timeout: typing.Optional[httpx.Timeout] = httpx.Timeout(5)
322+
) -> ScannedMedia:
283323
"""Given the contents of some media, along with a mime type,
284324
scan the contents for matches against known child abuse media.
285325
286326
Args:
287327
config: The context that will be used to build the request.
328+
timeout:
329+
If provided explicitly, a configuration passed to the underlying http client.
330+
It defaults to 5 seconds, and can be disabled by setting it to `None`.
288331
289332
Returns:
290333
ScannedMedia: A record of a successful scan of the media.
@@ -300,6 +343,7 @@ async def scan_media_from_bytes_with_config(self, config: ScanMediaFromBytes) ->
300343
url=url,
301344
headers={"Content-Type": config.mime_type},
302345
content=config.contents,
346+
timeout=timeout,
303347
)
304348

305349
if response.is_client_error or response.is_server_error:

0 commit comments

Comments
 (0)