1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 17:15:35 +00:00

Merge pull request #390 from kevin1024/fix-aiohttp-client

Fix vcr to support aiohttp client requests
This commit is contained in:
Luiz Menezes
2018-09-18 14:42:59 -03:00
committed by GitHub
5 changed files with 50 additions and 27 deletions

View File

@@ -1,7 +1,3 @@
import asyncio
@asyncio.coroutine
def handle_coroutine(vcr, fn):
async def handle_coroutine(vcr, fn): # noqa: E999
with vcr as cassette:
return (yield from fn(cassette)) # noqa: E999
return (await fn(cassette)) # noqa: E999

View File

@@ -25,28 +25,22 @@ class MockClientResponse(ClientResponse):
session=None,
)
# TODO: get encoding from header
@asyncio.coroutine
def json(self, *, encoding='utf-8', loads=json.loads, **kwargs): # NOQA: E999
async def json(self, *, encoding='utf-8', loads=json.loads, **kwargs): # NOQA: E999
return loads(self._body.decode(encoding))
@asyncio.coroutine
def text(self, encoding='utf-8'):
async def text(self, encoding='utf-8'):
return self._body.decode(encoding)
@asyncio.coroutine
def read(self):
async def read(self):
return self._body
@asyncio.coroutine
def release(self):
async def release(self):
pass
def vcr_request(cassette, real_request):
@functools.wraps(real_request)
@asyncio.coroutine
def new_request(self, method, url, **kwargs):
async def new_request(self, method, url, **kwargs):
headers = kwargs.get('headers')
headers = self._prepare_headers(headers)
data = kwargs.get('data')
@@ -82,7 +76,7 @@ def vcr_request(cassette, real_request):
response.close()
return response
response = yield from real_request(self, method, url, **kwargs) # NOQA: E999
response = await real_request(self, method, url, **kwargs) # NOQA: E999
vcr_response = {
'status': {
@@ -90,7 +84,7 @@ def vcr_request(cassette, real_request):
'message': response.reason,
},
'headers': dict(response.headers),
'body': {'string': (yield from response.read())}, # NOQA: E999
'body': {'string': (await response.read())}, # NOQA: E999
'url': response.url,
}
cassette.append(vcr_request, vcr_response)