Skip to content

Commit

Permalink
feat(object): Add an error attribute (VirusTotal#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 authored Mar 15, 2022
1 parent be0c2fe commit 5b4e0b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
22 changes: 15 additions & 7 deletions tests/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def iterator_response(httpserver):
'id': 'dummy_id_4',
'type': 'dummy_type',
'attributes': {'order': 0}
}]
}, {
'id': 'dummy_id_5',
'type': 'dummy_type',
'error': {'code': 'NotFoundError', 'message': 'item not found.'}}]
})


Expand All @@ -79,11 +82,16 @@ def test_next(httpserver, iterator_response):
last = None
for i, obj in enumerate(it):
assert obj.id == f'dummy_id_{i+3}'
if obj.id == 'dummy_id_5':
assert obj.error['code'] == 'NotFoundError'
else:
assert obj.order == 0
assert obj.error is None
last = obj

assert last.id == 'dummy_id_4'
assert it._count == 4
assert it._batch_cursor == 1
assert last.id == 'dummy_id_5'
assert it._count == 5
assert it._batch_cursor == 2

with pytest.raises(StopIteration):
# there shouldn't be more available elements after the for loop
Expand Down Expand Up @@ -141,9 +149,9 @@ async def test_anext(httpserver, iterator_response):
last = obj
i += 1

assert last.id == 'dummy_id_4'
assert it._count == 4
assert it._batch_cursor == 1
assert last.id == 'dummy_id_5'
assert it._count == 5
assert it._batch_cursor == 2

with pytest.raises(StopAsyncIteration):
# there shouldn't be more available elements after the for loop
Expand Down
8 changes: 8 additions & 0 deletions vt/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def from_dict(cls, obj_dict):
if 'relationships' in obj_dict:
obj._relationships = obj_dict['relationships']

if 'error' in obj_dict:
obj._error = obj_dict['error']

return obj

def __init__(self, obj_type, obj_id=None, obj_attributes=None):
Expand All @@ -125,6 +128,7 @@ def __init__(self, obj_type, obj_id=None, obj_attributes=None):

self._modified_attrs = []
self._modified_data = {}
self._error = None

def __on_attr_change(self, attr):
if hasattr(self, '_modified_attrs'):
Expand Down Expand Up @@ -178,6 +182,10 @@ def relationships(self):
return self._relationships
return {}

@property
def error(self):
return self._error

def get(self, attr_name, default=None):
"""Returns an attribute by name.
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.13.2'
__version__ = '0.14.0'

0 comments on commit 5b4e0b7

Please sign in to comment.