Skip to content

Commit 28e0750

Browse files
authored
0.1.1 - Context Manager support and more. (#25)
* Bump LICENSE year. * Update dependencies. * Bump license text year. Bump version. Add support to Virustotal class to be used as a Context Manager. * Bump version to 0.1.1 * Add tests for Context Manager. * Add example usage to README for using Virustotal class as a Context Manager. Add 0.1.1 changelog notes. * Remove file. * Addressed PR #26
1 parent 05dfa8a commit 28e0750

8 files changed

Lines changed: 191 additions & 140 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 dbrennand
3+
Copyright (c) 2021 dbrennand
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Pipfile.lock

Lines changed: 136 additions & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ vtotal = Virustotal(
6262
API_VERSION="v3",
6363
PROXIES={"http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080"},
6464
TIMEOUT=5.0)
65+
66+
# As of version 0.1.1, the Virustotal class can be invoked as a Context Manager!
67+
## v2 example
68+
with Virustotal(API_KEY="Insert API key here.") as vtotal:
69+
# Your code here
70+
71+
## v3 example
72+
with Virustotal(API_KEY="Insert API key here.", API_VERSION="v3") as vtotal:
73+
# Your code here
6574
```
6675

6776
Additionally, it is possible to provide an API key via the environment variable `VIRUSTOTAL_API_KEY`.
@@ -211,6 +220,8 @@ To run the tests, perform the following steps:
211220

212221
## Changelog
213222

223+
* 0.1.1 - Added Context Manager support and tests. Updated dependencies and license year.
224+
214225
* 0.1.0 - Added support for the VirusTotal v3 API. Library redesign (new usage, examples, tests and more.) See [#24](https://github.com/dbrennand/virustotal-python/pull/24).
215226

216227
* 0.0.9 - Update dependencies for security vulnerability.

_config.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

requirements.txt

-2 Bytes
Binary file not shown.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="virustotal-python",
8-
version="0.1.0",
8+
version="0.1.1",
99
author="dbrennand",
1010
description="A Python library to interact with the public VirusTotal v2 and v3 APIs.",
1111
long_description=long_description,

virustotal_python/tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,30 @@ def test_metadata_v3(vtotal_v3):
295295
engines_dict = resp.data["engines"]
296296
assert engines_dict.keys()
297297
assert "Microsoft" in engines_dict.keys()
298+
299+
300+
def test_contextmanager_v2():
301+
"""
302+
Test basic Context Manager support.
303+
"""
304+
with virustotal_python.Virustotal() as vtotal:
305+
# Retrieve information about an IP address
306+
resp = vtotal.request("ip-address/report", params={"ip": IP})
307+
assert resp.status_code == 200
308+
data = resp.json()
309+
assert data["as_owner"] == "Google LLC"
310+
assert data["country"] == "US"
311+
312+
313+
def test_contextmanager_v3():
314+
"""
315+
Test basic Context Manager support.
316+
"""
317+
with virustotal_python.Virustotal(API_VERSION="v3") as vtotal:
318+
# Retrieve information about an IP address
319+
resp = vtotal.request(f"ip_addresses/{IP}")
320+
assert resp.status_code == 200
321+
data = resp.data
322+
assert data["id"] == IP
323+
assert data["attributes"]["as_owner"] == "Google LLC"
324+
assert data["attributes"]["country"] == "US"

virustotal_python/virustotal.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
MIT License
33
4-
Copyright (c) 2020 dbrennand
4+
Copyright (c) 2021 dbrennand
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -243,7 +243,7 @@ def __init__(
243243
:param TIMEOUT: A float for the amount of time to wait in seconds for the HTTP request before timing out.
244244
:raises ValueError: Raises ValueError when no API_KEY is provided or the API_VERSION is invalid.
245245
"""
246-
self.VERSION = "0.1.0"
246+
self.VERSION = "0.1.1"
247247
if API_KEY is None:
248248
raise ValueError(
249249
"An API key is required to interact with the VirusTotal API.\nProvide one to the API_KEY parameter or by setting the environment variable 'VIRUSTOTAL_API_KEY'."
@@ -273,6 +273,19 @@ def __init__(
273273
f"The API version '{API_VERSION}' is not a valid VirusTotal API version.\nValid API versions are 'v2' or 'v3'."
274274
)
275275

276+
# Context Manager support
277+
def __enter__(self):
278+
"""
279+
Context Manager enter function.
280+
"""
281+
return self
282+
283+
def __exit__(self, type, value, traceback):
284+
"""
285+
Context Manager exit function.
286+
"""
287+
return
288+
276289
def request(
277290
self,
278291
resource: str,

0 commit comments

Comments
 (0)