mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 01:25:34 +00:00
Drop support to asyncio.coroutine (py34 async/await syntax)
This commit is contained in:
@@ -2,23 +2,32 @@
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
from aiohttp.test_utils import TestClient
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
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)
|
||||
response_ctx = session.request(method, url, **kwargs)
|
||||
|
||||
response = yield from response_ctx.__aenter__()
|
||||
response = await response_ctx.__aenter__()
|
||||
if output == 'text':
|
||||
content = yield from response.text()
|
||||
content = await response.text()
|
||||
elif output == 'json':
|
||||
content_type = content_type or 'application/json'
|
||||
content = yield from response.json(encoding=encoding, content_type=content_type)
|
||||
content = await response.json(encoding=encoding, content_type=content_type)
|
||||
elif output == 'raw':
|
||||
content = yield from response.read()
|
||||
content = await response.read()
|
||||
|
||||
response_ctx._resp.close()
|
||||
yield from session.close()
|
||||
await session.close()
|
||||
|
||||
return response, content
|
||||
|
||||
|
||||
def aiohttp_app():
|
||||
async def hello(request):
|
||||
return aiohttp.web.Response(text='hello')
|
||||
|
||||
app = aiohttp.web.Application()
|
||||
app.router.add_get('/', hello)
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user