@@ -66,7 +66,26 @@ def _normalize_request(self, request: httpcore.Request) -> HookEvent:
66
66
data = request_data .model_dump ()
67
67
)
68
68
69
- def _normalize_response (self , response : httpcore .Response ) -> HookEvent :
69
+ async def _normalize_response (self , response : httpcore .Response ) -> HookEvent :
70
+ httpx_response = httpx .Response (
71
+ status_code = response .status ,
72
+ headers = response .headers ,
73
+ content = await response .aread (),
74
+ extensions = response .extensions ,
75
+ )
76
+
77
+ response_data = HTTPResponseData (
78
+ status_code = httpx_response .status_code ,
79
+ headers = dict (httpx_response .headers ),
80
+ body = httpx_response .text
81
+ )
82
+
83
+ return HookEvent (
84
+ event_type = HookEventType .HTTP_RESPONSE ,
85
+ data = response_data .model_dump ()
86
+ )
87
+
88
+ def _normalize_response_sync (self , response : httpcore .Response ) -> HookEvent :
70
89
httpx_response = httpx .Response (
71
90
status_code = response .status ,
72
91
headers = response .headers ,
@@ -90,15 +109,15 @@ def _request_callback_sync(self, request: httpcore.Request) -> None:
90
109
self ._callback_handler .on_hook_callback_sync (self , normalized )
91
110
92
111
def _response_callback_sync (self , response : httpcore .Response ) -> None :
93
- normalized = self ._normalize_response (response )
112
+ normalized = self ._normalize_response_sync (response )
94
113
self ._callback_handler .on_hook_callback_sync (self , normalized )
95
114
96
115
async def _request_callback (self , request : httpcore .Request ) -> None :
97
116
normalized = self ._normalize_request (request )
98
117
await self ._callback_handler .on_hook_callback (self , normalized )
99
118
100
119
async def _response_callback (self , response : httpcore .Response ) -> None :
101
- normalized = self ._normalize_response (response )
120
+ normalized = await self ._normalize_response (response )
102
121
await self ._callback_handler .on_hook_callback (self , normalized )
103
122
104
123
def _intercepted_handle_request (self , conn_self : httpcore .HTTPConnection , request : httpcore .Request ) -> httpcore .Response :
@@ -118,14 +137,14 @@ def _intercepted_handle_request(self, conn_self: httpcore.HTTPConnection, reques
118
137
119
138
async def _intercepted_handle_async_request (self , conn_self : httpcore .AsyncHTTPConnection , request : httpcore .Request ) -> httpcore .Response :
120
139
await self ._request_callback (request )
121
- response : httpcore .Response = self ._original_handle_async_request (conn_self , request ) # type: ignore
140
+ response : httpcore .Response = await self ._original_handle_async_request (conn_self , request ) # type: ignore
122
141
await self ._response_callback (response )
123
142
124
143
# Since we messed up the response, we'll need to create a new one
125
144
new_response = httpcore .Response (
126
145
status = response .status ,
127
146
headers = response .headers ,
128
- content = response .read (),
147
+ content = await response .aread (),
129
148
extensions = response .extensions .copy () if response .extensions else {}, # type: ignore
130
149
)
131
150
0 commit comments