Skip to content

Commit 1179f25

Browse files
committed
Patch httpcore instead of httpx
1 parent ac70eaa commit 1179f25

File tree

5 files changed

+220
-213
lines changed

5 files changed

+220
-213
lines changed

docs/installation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following HTTP libraries are supported:
2222
- ``urllib2``
2323
- ``urllib3``
2424
- ``httpx``
25+
- ``httpcore``
2526

2627
Speed
2728
-----

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def find_version(*file_paths):
4848
"tests": [
4949
"aiohttp",
5050
"boto3",
51+
"httpcore",
5152
"httplib2",
5253
"httpx",
5354
"pytest-aiohttp",

vcr/patch.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@
9292

9393

9494
try:
95-
import httpx
95+
import httpcore
9696
except ImportError: # pragma: no cover
9797
pass
9898
else:
99-
_HttpxSyncClient_send_single_request = httpx.Client._send_single_request
100-
_HttpxAsyncClient_send_single_request = httpx.AsyncClient._send_single_request
99+
_HttpcoreConnectionPool_handle_request = httpcore.ConnectionPool.handle_request
100+
_HttpcoreAsyncConnectionPool_handle_async_request = httpcore.AsyncConnectionPool.handle_async_request
101101

102102

103103
class CassettePatcherBuilder:
@@ -121,7 +121,7 @@ def build(self):
121121
self._httplib2(),
122122
self._tornado(),
123123
self._aiohttp(),
124-
self._httpx(),
124+
self._httpcore(),
125125
self._build_patchers_from_mock_triples(self._cassette.custom_patches),
126126
)
127127

@@ -304,19 +304,22 @@ def _aiohttp(self):
304304
yield client.ClientSession, "_request", new_request
305305

306306
@_build_patchers_from_mock_triples_decorator
307-
def _httpx(self):
307+
def _httpcore(self):
308308
try:
309-
import httpx
309+
import httpcore
310310
except ImportError: # pragma: no cover
311311
return
312312
else:
313-
from .stubs.httpx_stubs import async_vcr_send, sync_vcr_send
313+
from .stubs.httpcore_stubs import vcr_handle_async_request, vcr_handle_request
314314

315-
new_async_client_send = async_vcr_send(self._cassette, _HttpxAsyncClient_send_single_request)
316-
yield httpx.AsyncClient, "_send_single_request", new_async_client_send
315+
new_handle_async_request = vcr_handle_async_request(
316+
self._cassette,
317+
_HttpcoreAsyncConnectionPool_handle_async_request,
318+
)
319+
yield httpcore.AsyncConnectionPool, "handle_async_request", new_handle_async_request
317320

318-
new_sync_client_send = sync_vcr_send(self._cassette, _HttpxSyncClient_send_single_request)
319-
yield httpx.Client, "_send_single_request", new_sync_client_send
321+
new_handle_request = vcr_handle_request(self._cassette, _HttpcoreConnectionPool_handle_request)
322+
yield httpcore.ConnectionPool, "handle_request", new_handle_request
320323

321324
def _urllib3_patchers(self, cpool, conn, stubs):
322325
http_connection_remover = ConnectionRemover(

vcr/stubs/httpcore_stubs.py

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import asyncio
2+
import functools
3+
import logging
4+
from collections import defaultdict
5+
from collections.abc import AsyncIterable, Iterable
6+
7+
from httpcore import Response
8+
from httpcore._models import ByteStream
9+
10+
from vcr.errors import CannotOverwriteExistingCassetteException
11+
from vcr.filters import decode_response
12+
from vcr.request import Request as VcrRequest
13+
from vcr.serializers.compat import convert_body_to_bytes
14+
15+
_logger = logging.getLogger(__name__)
16+
17+
18+
async def _convert_byte_stream(stream):
19+
if isinstance(stream, Iterable):
20+
return list(stream)
21+
22+
if isinstance(stream, AsyncIterable):
23+
return [part async for part in stream]
24+
25+
26+
def _serialize_headers(real_response):
27+
"""
28+
Some headers can appear multiple times, like "Set-Cookie".
29+
Therefore serialize every header key to a list of values.
30+
"""
31+
32+
headers = defaultdict(list)
33+
34+
for name, value in real_response.headers:
35+
headers[name.decode("ascii")].append(value.decode("ascii"))
36+
37+
return dict(headers)
38+
39+
40+
async def _serialize_response(real_response):
41+
# The reason_phrase may not exist
42+
try:
43+
reason_phrase = real_response.extensions["reason_phrase"].decode("ascii")
44+
except KeyError:
45+
reason_phrase = None
46+
47+
# Reading the response stream consumes the iterator, so we need to restore it afterwards
48+
content = b"".join(await _convert_byte_stream(real_response.stream))
49+
real_response.stream = ByteStream(content)
50+
51+
return {
52+
"status": {"code": real_response.status, "message": reason_phrase},
53+
"headers": _serialize_headers(real_response),
54+
"body": {"string": content},
55+
}
56+
57+
58+
def _deserialize_headers(headers):
59+
"""
60+
httpcore accepts headers as list of tuples of header key and value.
61+
"""
62+
63+
return [
64+
(name.encode("ascii"), value.encode("ascii")) for name, values in headers.items() for value in values
65+
]
66+
67+
68+
def _deserialize_response(vcr_response):
69+
# Cassette format generated for HTTPX requests by older versions of
70+
# vcrpy. We restructure the content to resemble what a regular
71+
# cassette looks like.
72+
if "status_code" in vcr_response:
73+
vcr_response = decode_response(
74+
convert_body_to_bytes(
75+
{
76+
"headers": vcr_response["headers"],
77+
"body": {"string": vcr_response["content"]},
78+
"status": {"code": vcr_response["status_code"]},
79+
},
80+
),
81+
)
82+
extensions = None
83+
else:
84+
extensions = (
85+
{"reason_phrase": vcr_response["status"]["message"].encode("ascii")}
86+
if vcr_response["status"]["message"]
87+
else None
88+
)
89+
90+
return Response(
91+
vcr_response["status"]["code"],
92+
headers=_deserialize_headers(vcr_response["headers"]),
93+
content=vcr_response["body"]["string"],
94+
extensions=extensions,
95+
)
96+
97+
98+
async def _make_vcr_request(real_request):
99+
# Reading the request stream consumes the iterator, so we need to restore it afterwards
100+
body = b"".join(await _convert_byte_stream(real_request.stream))
101+
real_request.stream = ByteStream(body)
102+
103+
uri = bytes(real_request.url).decode("ascii")
104+
headers = {name.decode("ascii"): value.decode("ascii") for name, value in real_request.headers}
105+
106+
return VcrRequest(real_request.method.decode("ascii"), uri, body, headers)
107+
108+
109+
async def _vcr_request(cassette, real_request):
110+
vcr_request = await _make_vcr_request(real_request)
111+
112+
if cassette.can_play_response_for(vcr_request):
113+
return vcr_request, _play_responses(cassette, vcr_request)
114+
115+
if cassette.write_protected and cassette.filter_request(vcr_request):
116+
raise CannotOverwriteExistingCassetteException(
117+
cassette=cassette,
118+
failed_request=vcr_request,
119+
)
120+
121+
_logger.info("%s not in cassette, sending to real server", vcr_request)
122+
123+
return vcr_request, None
124+
125+
126+
async def _record_responses(cassette, vcr_request, real_response):
127+
cassette.append(vcr_request, await _serialize_response(real_response))
128+
129+
130+
def _play_responses(cassette, vcr_request):
131+
vcr_response = cassette.play_response(vcr_request)
132+
real_response = _deserialize_response(vcr_response)
133+
134+
return real_response
135+
136+
137+
async def _vcr_handle_async_request(
138+
cassette,
139+
real_handle_async_request,
140+
self,
141+
real_request,
142+
):
143+
vcr_request, vcr_response = await _vcr_request(cassette, real_request)
144+
145+
if vcr_response:
146+
return vcr_response
147+
148+
real_response = await real_handle_async_request(self, real_request)
149+
await _record_responses(cassette, vcr_request, real_response)
150+
151+
return real_response
152+
153+
154+
def vcr_handle_async_request(cassette, real_handle_async_request):
155+
@functools.wraps(real_handle_async_request)
156+
def _inner_handle_async_request(self, real_request):
157+
return _vcr_handle_async_request(
158+
cassette,
159+
real_handle_async_request,
160+
self,
161+
real_request,
162+
)
163+
164+
return _inner_handle_async_request
165+
166+
167+
def _run_async_function(sync_func, *args, **kwargs):
168+
"""
169+
Safely run an asynchronous function from a synchronous context.
170+
Handles both cases:
171+
- An event loop is already running.
172+
- No event loop exists yet.
173+
"""
174+
try:
175+
asyncio.get_running_loop()
176+
except RuntimeError:
177+
return asyncio.run(sync_func(*args, **kwargs))
178+
else:
179+
# If inside a running loop, create a task and wait for it
180+
return asyncio.ensure_future(sync_func(*args, **kwargs))
181+
182+
183+
def _vcr_handle_request(cassette, real_handle_request, self, real_request):
184+
vcr_request, vcr_response = _run_async_function(
185+
_vcr_request,
186+
cassette,
187+
real_request,
188+
)
189+
190+
if vcr_response:
191+
return vcr_response
192+
193+
real_response = real_handle_request(self, real_request)
194+
_run_async_function(_record_responses, cassette, vcr_request, real_response)
195+
196+
return real_response
197+
198+
199+
def vcr_handle_request(cassette, real_handle_request):
200+
@functools.wraps(real_handle_request)
201+
def _inner_handle_request(self, real_request):
202+
return _vcr_handle_request(cassette, real_handle_request, self, real_request)
203+
204+
return _inner_handle_request

0 commit comments

Comments
 (0)