@@ -38,25 +38,34 @@ def _transform_headers(httpx_response):
38
38
39
39
async def _to_serialized_response (resp , aread ):
40
40
# The content shouldn't already have been read in by HTTPX.
41
- assert not hasattr (resp , "_decoder" )
42
-
43
- # Retrieve the content, but without decoding it.
44
- with patch .dict (resp .headers , {"Content-Encoding" : "" }):
45
- if aread :
46
- await resp .aread ()
47
- else :
48
- resp .read ()
49
-
50
- result = {
51
- "status" : {"code" : resp .status_code , "message" : resp .reason_phrase },
52
- "headers" : _transform_headers (resp ),
53
- "body" : {"string" : resp .content },
54
- }
55
-
56
- # As the content wasn't decoded, we restore the response to a state which
57
- # will be capable of decoding the content for the consumer.
58
- del resp ._decoder
59
- resp ._content = resp ._get_content_decoder ().decode (resp .content )
41
+
42
+ if not hasattr (resp , "_decoder" ):
43
+ # Retrieve the content, but without decoding it.
44
+ with patch .dict (resp .headers , {"Content-Encoding" : "" }):
45
+ if aread :
46
+ await resp .aread ()
47
+ else :
48
+ resp .read ()
49
+
50
+ result = {
51
+ "status" : {"code" : resp .status_code , "message" : resp .reason_phrase },
52
+ "headers" : _transform_headers (resp ),
53
+ "body" : {"string" : resp .content },
54
+ }
55
+
56
+ # As the content wasn't decoded, we restore the response to a state which
57
+ # will be capable of decoding the content for the consumer.
58
+ del resp ._decoder
59
+ resp ._content = resp ._get_content_decoder ().decode (resp .content )
60
+ else :
61
+ # The content has already been read in by HTTPX. Record it without an Encoding header.
62
+ with patch .dict (resp .headers , {"Content-Encoding" : "" }):
63
+ result = {
64
+ "status" : {"code" : resp .status_code , "message" : resp .reason_phrase },
65
+ "headers" : _transform_headers (resp ),
66
+ "body" : {"string" : resp .content },
67
+ }
68
+
60
69
return result
61
70
62
71
@@ -174,7 +183,17 @@ def _sync_vcr_send(cassette, real_send, *args, **kwargs):
174
183
return response
175
184
176
185
real_response = real_send (* args , ** kwargs )
177
- asyncio .run (_record_responses (cassette , vcr_request , real_response , aread = False ))
186
+
187
+ try :
188
+ loop = asyncio .get_running_loop ()
189
+ except RuntimeError :
190
+ loop = None
191
+
192
+ if loop is not None :
193
+ loop .create_task (_record_responses (cassette , vcr_request , real_response , aread = False ))
194
+ else :
195
+ asyncio .run (_record_responses (cassette , vcr_request , real_response , aread = False ))
196
+
178
197
return real_response
179
198
180
199
0 commit comments