@@ -30,13 +30,21 @@ def __init__(self, username: typing.Union[str, bytes], password: typing.Union[st
30
30
super ().__init__ (username = username , password = password )
31
31
self .__client = super ()._build_sync_http_client ()
32
32
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 :
34
39
"""Given the contents of some media, along with a mime type,
35
40
scan the contents for matches against known child abuse media.
36
41
37
42
Args:
38
43
contents: The raw bytes that represent the media.
39
44
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.
40
48
41
49
Returns:
42
50
The record of a successful media scan.
@@ -45,10 +53,13 @@ def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO], mime_
45
53
`ArachnidShieldError` on a failed but complete interaction with
46
54
the Arachnid Shield API, and `httpx.HTTPError` on any other connection failures.
47
55
"""
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 )
49
57
50
58
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 ,
52
63
) -> ScannedMedia :
53
64
"""Given path to the media file to scan, and an optional
54
65
value for mime_type that bypasses guessing it based of the filepath,
@@ -60,6 +71,9 @@ def scan_media_from_file(
60
71
mime_type_override:
61
72
If provided, will use this as the mime_type
62
73
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.
63
77
64
78
Returns:
65
79
The record of a successful media scan.
@@ -78,7 +92,7 @@ def scan_media_from_file(
78
92
detail = (
79
93
f"Failed to identify mime_type for { filepath } . "
80
94
f"You may specify it explicitly by providing "
81
- f"`force_mime_type `."
95
+ f"`mime_type_override `."
82
96
)
83
97
)
84
98
)
@@ -87,7 +101,7 @@ def scan_media_from_file(
87
101
contents = f .read ()
88
102
89
103
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 )
91
105
92
106
def scan_media_from_url (self , url : str ) -> ScannedMedia :
93
107
"""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:
105
119
"""
106
120
return self .scan_media_from_url_with_config (ScanMediaFromUrl (url = url ))
107
121
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 :
109
127
"""Given the contents of some media, along with a mime type,
110
128
scan the contents for matches against known child abuse media.
111
129
112
130
Args:
113
131
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`.
114
135
115
136
Returns:
116
137
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
125
146
url = url ,
126
147
headers = {"Content-Type" : config .mime_type },
127
148
content = config .contents ,
149
+ timeout = timeout ,
128
150
)
129
151
130
152
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
203
225
super ().__init__ (username = username , password = password )
204
226
self .__client = super ()._build_async_http_client ()
205
227
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 :
207
234
"""Given the contents of some media, along with a mime type,
208
235
scan the contents for matches against known child abuse media.
209
236
210
237
Args:
211
238
contents: The raw bytes that represent the media.
212
239
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.
213
243
214
244
Returns:
215
245
The record of a successful media scan.
@@ -219,7 +249,7 @@ async def scan_media_from_bytes(self, contents: typing.Union[bytes, io.BytesIO],
219
249
the Arachnid Shield API, and `httpx.HTTPError` on any other connection failures.
220
250
"""
221
251
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 )
223
253
224
254
async def scan_media_from_url (self , url : str ) -> ScannedMedia :
225
255
"""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:
238
268
return await self .scan_media_from_url_with_config (ScanMediaFromUrl (url = url ))
239
269
240
270
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 ,
242
275
) -> ScannedMedia :
243
276
"""Given path to the media file to scan, and an optional
244
277
value for mime_type that bypasses guessing it based of the filepath,
@@ -250,6 +283,9 @@ async def scan_media_from_file(
250
283
mime_type_override:
251
284
If provided, will use this as the mime_type
252
285
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.
253
289
254
290
Returns:
255
291
The record of a successful media scan.
@@ -268,7 +304,7 @@ async def scan_media_from_file(
268
304
detail = (
269
305
f"Failed to identify mime_type for { filepath } . "
270
306
f"You may specify it explicitly by providing "
271
- f"`force_mime_type `."
307
+ f"`mime_type_override `."
272
308
)
273
309
)
274
310
)
@@ -277,14 +313,21 @@ async def scan_media_from_file(
277
313
contents = f .read ()
278
314
279
315
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 )
281
317
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 :
283
323
"""Given the contents of some media, along with a mime type,
284
324
scan the contents for matches against known child abuse media.
285
325
286
326
Args:
287
327
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`.
288
331
289
332
Returns:
290
333
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) ->
300
343
url = url ,
301
344
headers = {"Content-Type" : config .mime_type },
302
345
content = config .contents ,
346
+ timeout = timeout ,
303
347
)
304
348
305
349
if response .is_client_error or response .is_server_error :
0 commit comments