1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

close aiohttp session on errors

This commit is contained in:
Thomas Grainger
2023-12-15 11:39:16 +00:00
parent 3919cb2573
commit f075c8b0b4

View File

@@ -5,24 +5,24 @@ import aiohttp
async def aiohttp_request(loop, method, url, output="text", encoding="utf-8", content_type=None, **kwargs): async def aiohttp_request(loop, method, url, output="text", encoding="utf-8", content_type=None, **kwargs):
session = aiohttp.ClientSession(loop=loop) async with aiohttp.ClientSession(loop=loop) as session:
response_ctx = session.request(method, url, **kwargs) response_ctx = session.request(method, url, **kwargs)
response = await response_ctx.__aenter__() response = await response_ctx.__aenter__()
if output == "text": if output == "text":
content = await response.text() content = await response.text()
elif output == "json": elif output == "json":
content_type = content_type or "application/json" content_type = content_type or "application/json"
content = await response.json(encoding=encoding, content_type=content_type) content = await response.json(encoding=encoding, content_type=content_type)
elif output == "raw": elif output == "raw":
content = await response.read() content = await response.read()
elif output == "stream": elif output == "stream":
content = await response.content.read() content = await response.content.read()
response_ctx._resp.close() response_ctx._resp.close()
await session.close() await session.close()
return response, content return response, content
def aiohttp_app(): def aiohttp_app():