Skip to content

Commit

Permalink
0.1.1 - Context Manager support and more. (#25)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dbrennand committed Feb 10, 2021
1 parent 05dfa8a commit 28e0750
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 140 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 dbrennand
Copyright (c) 2021 dbrennand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
271 changes: 136 additions & 135 deletions Pipfile.lock

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ vtotal = Virustotal(
API_VERSION="v3",
PROXIES={"http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080"},
TIMEOUT=5.0)

# As of version 0.1.1, the Virustotal class can be invoked as a Context Manager!
## v2 example
with Virustotal(API_KEY="Insert API key here.") as vtotal:
# Your code here

## v3 example
with Virustotal(API_KEY="Insert API key here.", API_VERSION="v3") as vtotal:
# Your code here
```

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

## Changelog

* 0.1.1 - Added Context Manager support and tests. Updated dependencies and license year.

* 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).

* 0.0.9 - Update dependencies for security vulnerability.
Expand Down
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

Binary file modified requirements.txt
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="virustotal-python",
version="0.1.0",
version="0.1.1",
author="dbrennand",
description="A Python library to interact with the public VirusTotal v2 and v3 APIs.",
long_description=long_description,
Expand Down
27 changes: 27 additions & 0 deletions virustotal_python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,30 @@ def test_metadata_v3(vtotal_v3):
engines_dict = resp.data["engines"]
assert engines_dict.keys()
assert "Microsoft" in engines_dict.keys()


def test_contextmanager_v2():
"""
Test basic Context Manager support.
"""
with virustotal_python.Virustotal() as vtotal:
# Retrieve information about an IP address
resp = vtotal.request("ip-address/report", params={"ip": IP})
assert resp.status_code == 200
data = resp.json()
assert data["as_owner"] == "Google LLC"
assert data["country"] == "US"


def test_contextmanager_v3():
"""
Test basic Context Manager support.
"""
with virustotal_python.Virustotal(API_VERSION="v3") as vtotal:
# Retrieve information about an IP address
resp = vtotal.request(f"ip_addresses/{IP}")
assert resp.status_code == 200
data = resp.data
assert data["id"] == IP
assert data["attributes"]["as_owner"] == "Google LLC"
assert data["attributes"]["country"] == "US"
17 changes: 15 additions & 2 deletions virustotal_python/virustotal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
MIT License
Copyright (c) 2020 dbrennand
Copyright (c) 2021 dbrennand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -243,7 +243,7 @@ def __init__(
:param TIMEOUT: A float for the amount of time to wait in seconds for the HTTP request before timing out.
:raises ValueError: Raises ValueError when no API_KEY is provided or the API_VERSION is invalid.
"""
self.VERSION = "0.1.0"
self.VERSION = "0.1.1"
if API_KEY is None:
raise ValueError(
"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'."
Expand Down Expand Up @@ -273,6 +273,19 @@ def __init__(
f"The API version '{API_VERSION}' is not a valid VirusTotal API version.\nValid API versions are 'v2' or 'v3'."
)

# Context Manager support
def __enter__(self):
"""
Context Manager enter function.
"""
return self

def __exit__(self, type, value, traceback):
"""
Context Manager exit function.
"""
return

def request(
self,
resource: str,
Expand Down

0 comments on commit 28e0750

Please sign in to comment.