Skip to content

Commit

Permalink
fix(client): Fix Runtime error when running non async apps (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 authored Jun 10, 2024
1 parent d33c67f commit 03fd2f1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pylint aiohttp pytest_httpserver pytest_asyncio
pip install pytest pylint aiohttp pytest_httpserver pytest_asyncio flask
- name: Lint
run: |
pylint --rcfile pylintrc vt/ tests examples/
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"aiohttp ; python_version>'3.7'"
],
setup_requires=["pytest-runner"],
extras_require={"test": ["pytest", "pytest_httpserver", "pytest_asyncio"]},
extras_require={
"test": ["pytest", "pytest_httpserver", "pytest_asyncio", "flask"]
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
Expand Down
28 changes: 27 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import datetime
import io
import functools
import json
import pickle

Expand All @@ -23,8 +24,10 @@
from vt import Client
from vt import Object

from tests import wsgi_app

def new_client(httpserver):

def new_client(httpserver, unused_apikey=""):
return Client(
"dummy_api_key",
host="http://" + httpserver.host + ":" + str(httpserver.port),
Expand Down Expand Up @@ -480,3 +483,26 @@ def test_user_headers(httpserver):
assert "Accept-Encoding" in headers
assert "User-Agent" in headers
assert "foo" in headers


def test_wsgi_app(httpserver, monkeypatch):
app = wsgi_app.app
app.config.update({"TESTING": True})
client = app.test_client()
expected_response = {"data": {
"id": "google.com",
"type": "domain",
"attributes": {"foo": "foo"},
}}


httpserver.expect_request(
"/api/v3/domains/google.com", method="GET",
headers={"X-Apikey": "dummy_api_key"}
).respond_with_json(expected_response)
monkeypatch.setattr(
"tests.wsgi_app.vt.Client", functools.partial(new_client, httpserver)
)
response = client.get("/")
assert response.status_code == 200
assert response.json == expected_response
14 changes: 14 additions & 0 deletions tests/wsgi_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Simulates a Flask app using vt-py to test non-async environments."""

import flask
import os
import vt

app = flask.Flask(__name__)


@app.get('/')
def home():
with vt.Client(os.getenv('VT_APIKEY')) as c:
g = c.get('/domains/google.com')
return g.json()
12 changes: 11 additions & 1 deletion vt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,17 @@ def __init__(
if connector is not None:
self._connector = connector
else:
self._connector = aiohttp.TCPConnector(ssl=self._verify_ssl)
# the TCPConnector class expects to be instantiated inside a event loop.
# If there is none, create one.
try:
event_loop = asyncio.get_event_loop()
except RuntimeError:
event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)

self._connector = aiohttp.TCPConnector(
ssl=self._verify_ssl, loop=event_loop
)

def _full_url(self, path:str, *args: typing.Any) -> str:
try:
Expand Down

0 comments on commit 03fd2f1

Please sign in to comment.