Skip to content

Commit 9bd3d27

Browse files
authored
Add error handling to 'download_file' (#64)
1 parent f3e458e commit 9bd3d27

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

tests/test_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import pytest
2222

23+
from vt import APIError
2324
from vt import Client
2425
from vt import FeedType
2526
from vt import Object
@@ -279,6 +280,31 @@ def test_download_file(httpserver):
279280
assert f.read() == b'filecontent'
280281

281282

283+
def test_download_file_with_error(httpserver):
284+
285+
httpserver.expect_request(
286+
'/api/v3/files/01020304050607080900a0b0c0ddead/download',
287+
method='GET',
288+
headers={'X-Apikey': 'dummy_api_key'}
289+
).respond_with_data(
290+
status=404,
291+
content_type="application/json",
292+
response_data=json.dumps({
293+
"error": {
294+
"code": "NotFoundError",
295+
"message": "Resource not found."
296+
}
297+
})
298+
)
299+
300+
with pytest.raises(APIError) as e_info:
301+
with new_client(httpserver) as client:
302+
with io.BytesIO() as f:
303+
client.download_file('01020304050607080900a0b0c0ddead', f)
304+
assert e_info.value.args[0] == "NotFoundError"
305+
assert e_info.value.args[1] == "Resource not found."
306+
307+
282308
def test_scan_file(httpserver):
283309

284310
upload_url = (

vt/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ def download_file(self, hash, file):
308308
async def download_file_async(self, hash, file):
309309
"""Like :func:`download_file` but returns a coroutine."""
310310
response = await self.get_async(f'/files/{hash}/download')
311+
error = await self.get_error_async(response)
312+
if error:
313+
raise error
311314
while True:
312315
chunk = await response.content.read_async(1024*1024)
313316
if not chunk:

vt/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.7.0'
1+
__version__ = '0.7.1'

0 commit comments

Comments
 (0)