Skip to content

Commit

Permalink
Added logic to handle case when chunk transfer encoding is used (#56)
Browse files Browse the repository at this point in the history
closes #52
  • Loading branch information
ytreister authored Jul 20, 2021
1 parent 246ea4f commit 47a2f04
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions vt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,37 @@ def __getattr__(self, attr):
def content(self):
return StreamReader(self._aiohttp_resp.content)

async def _get_chunked_response(self):
buffer = b""
async for data, _ in self.content.iter_chunks():
buffer += data
return buffer

async def read_async(self):
return await self._aiohttp_resp.read()
if self.headers.get('Transfer-encoding') == 'chunked':
return await self._get_chunked_response()
else:
return await self._aiohttp_resp.read()

def read(self):
return _make_sync(self.read_async())

async def json_async(self):
return await self._aiohttp_resp.json()
if self.headers.get('Transfer-encoding') == 'chunked':
response_content = await self._get_chunked_response()
return json.loads(response_content)
else:
return await self._aiohttp_resp.json()

def json(self):
return _make_sync(self.json_async())

async def text_async(self):
return await self._aiohttp_resp.text()
if self.headers.get('Transfer-encoding') == 'chunked':
response_content = await self._get_chunked_response()
return response_content.decode(self._aiohttp_resp.get_encoding())
else:
return await self._aiohttp_resp.text()

def text(self):
return _make_sync(self.text_async())
Expand Down

0 comments on commit 47a2f04

Please sign in to comment.