Skip to content

Commit

Permalink
Merge pull request #25 from GDATASoftwareAG/httpx
Browse files Browse the repository at this point in the history
Switch to httpx
  • Loading branch information
pstadermann authored May 23, 2022
2 parents f8a21e8 + fcd8e3b commit 2e407ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ xmlrunner==1.7.7
unittest-xml-reporting==3.2.0
websockets~=10.3
python-dotenv==0.20.0
requests==2.27.1
httpx[http2]==0.22.0
build==0.7.0
jwt==1.3.1
2 changes: 1 addition & 1 deletion python/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers =
python_requires = >=3.8
install_requires =
websockets == 10.3
requests == 2.27.1
httpx[http2] == 0.22.0
jwt == 1.3.1

[options.packages.find]
Expand Down
12 changes: 7 additions & 5 deletions python/src/vaas/vaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import asyncio
from asyncio import Future
from jwt import JWT
import requests
import httpx
import websockets.client

URL = "wss://gateway-vaas.gdatasecurity.de"
Expand All @@ -32,7 +32,7 @@ def __init__(self, tracing=VaasTracing()):
self.websocket = None
self.session_id = None
self.results = {}
self.session = requests.Session()
self.httpx_client = httpx.AsyncClient(http2=True)

async def connect(self, token, url=URL):
"""Connect to VaaS
Expand All @@ -59,6 +59,8 @@ async def close(self):
await self.websocket.close()
if self.loop_result is not None:
await self.loop_result
if self.httpx_client is not None:
await self.httpx_client.aclose()

async def __aenter__(self):
return self
Expand Down Expand Up @@ -113,7 +115,7 @@ async def for_buffer(self, buffer):
token = response.get("upload_token")
url = response.get("url")
response_message = self.__response_message_for_guid(guid)
self.__upload(token, url, buffer)
await self.__upload(token, url, buffer)
verdict = (await response_message).get("verdict")
self.tracing.trace_upload_request(time.time() - start, len(buffer))

Expand All @@ -124,11 +126,11 @@ async def for_file(self, path):
with open(path, "rb") as open_file:
return await self.for_buffer(open_file.read())

def __upload(self, token, upload_uri, buffer):
async def __upload(self, token, upload_uri, buffer):
jwt = JWT()
decoded_token = jwt.decode(token, do_verify=False)
trace_id = decoded_token.get("traceId")
self.session.put(
await self.httpx_client.put(
url=upload_uri,
data=buffer,
headers={"Authorization": token, "traceParent": trace_id},
Expand Down

0 comments on commit 2e407ea

Please sign in to comment.