Skip to content

Commit

Permalink
Add error handling to 'download_file' (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostefano authored Jul 26, 2021
1 parent f3e458e commit 9bd3d27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pytest

from vt import APIError
from vt import Client
from vt import FeedType
from vt import Object
Expand Down Expand Up @@ -279,6 +280,31 @@ def test_download_file(httpserver):
assert f.read() == b'filecontent'


def test_download_file_with_error(httpserver):

httpserver.expect_request(
'/api/v3/files/01020304050607080900a0b0c0ddead/download',
method='GET',
headers={'X-Apikey': 'dummy_api_key'}
).respond_with_data(
status=404,
content_type="application/json",
response_data=json.dumps({
"error": {
"code": "NotFoundError",
"message": "Resource not found."
}
})
)

with pytest.raises(APIError) as e_info:
with new_client(httpserver) as client:
with io.BytesIO() as f:
client.download_file('01020304050607080900a0b0c0ddead', f)
assert e_info.value.args[0] == "NotFoundError"
assert e_info.value.args[1] == "Resource not found."


def test_scan_file(httpserver):

upload_url = (
Expand Down
3 changes: 3 additions & 0 deletions vt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def download_file(self, hash, file):
async def download_file_async(self, hash, file):
"""Like :func:`download_file` but returns a coroutine."""
response = await self.get_async(f'/files/{hash}/download')
error = await self.get_error_async(response)
if error:
raise error
while True:
chunk = await response.content.read_async(1024*1024)
if not chunk:
Expand Down
2 changes: 1 addition & 1 deletion vt/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.0'
__version__ = '0.7.1'

0 comments on commit 9bd3d27

Please sign in to comment.