@@ -399,18 +399,6 @@ def _build_headers(self, options: FinalRequestOptions) -> httpx.Headers:
399
399
400
400
return headers
401
401
402
- def _prepare_request (
403
- self ,
404
- request : httpx .Request , # noqa: ARG002
405
- ) -> None :
406
- """This method is used as a callback for mutating the `Request` object
407
- after it has been constructed.
408
-
409
- This is useful for cases where you want to add certain headers based off of
410
- the request properties, e.g. `url`, `method` etc.
411
- """
412
- return None
413
-
414
402
def _prepare_url (self , url : str ) -> URL :
415
403
"""
416
404
Merge a URL argument together with any 'base_url' on the client,
@@ -463,7 +451,7 @@ def _build_request(
463
451
kwargs ["data" ] = self ._serialize_multipartform (json_data )
464
452
465
453
# TODO: report this error to httpx
466
- request = self ._client .build_request ( # pyright: ignore[reportUnknownMemberType]
454
+ return self ._client .build_request ( # pyright: ignore[reportUnknownMemberType]
467
455
headers = headers ,
468
456
timeout = self .timeout if isinstance (options .timeout , NotGiven ) else options .timeout ,
469
457
method = options .method ,
@@ -477,8 +465,6 @@ def _build_request(
477
465
files = options .files ,
478
466
** kwargs ,
479
467
)
480
- self ._prepare_request (request )
481
- return request
482
468
483
469
def _serialize_multipartform (self , data : Mapping [object , object ]) -> dict [str , object ]:
484
470
items = self .qs .stringify_items (
@@ -781,6 +767,24 @@ def __exit__(
781
767
) -> None :
782
768
self .close ()
783
769
770
+ def _prepare_options (
771
+ self ,
772
+ options : FinalRequestOptions , # noqa: ARG002
773
+ ) -> None :
774
+ """Hook for mutating the given options"""
775
+ return None
776
+
777
+ def _prepare_request (
778
+ self ,
779
+ request : httpx .Request , # noqa: ARG002
780
+ ) -> None :
781
+ """This method is used as a callback for mutating the `Request` object
782
+ after it has been constructed.
783
+ This is useful for cases where you want to add certain headers based off of
784
+ the request properties, e.g. `url`, `method` etc.
785
+ """
786
+ return None
787
+
784
788
@overload
785
789
def request (
786
790
self ,
@@ -842,8 +846,11 @@ def _request(
842
846
stream : bool ,
843
847
stream_cls : type [_StreamT ] | None ,
844
848
) -> ResponseT | _StreamT :
849
+ self ._prepare_options (options )
850
+
845
851
retries = self ._remaining_retries (remaining_retries , options )
846
852
request = self ._build_request (options )
853
+ self ._prepare_request (request )
847
854
848
855
try :
849
856
response = self ._client .send (request , auth = self .custom_auth , stream = stream )
@@ -1201,6 +1208,24 @@ async def __aexit__(
1201
1208
) -> None :
1202
1209
await self .close ()
1203
1210
1211
+ async def _prepare_options (
1212
+ self ,
1213
+ options : FinalRequestOptions , # noqa: ARG002
1214
+ ) -> None :
1215
+ """Hook for mutating the given options"""
1216
+ return None
1217
+
1218
+ async def _prepare_request (
1219
+ self ,
1220
+ request : httpx .Request , # noqa: ARG002
1221
+ ) -> None :
1222
+ """This method is used as a callback for mutating the `Request` object
1223
+ after it has been constructed.
1224
+ This is useful for cases where you want to add certain headers based off of
1225
+ the request properties, e.g. `url`, `method` etc.
1226
+ """
1227
+ return None
1228
+
1204
1229
@overload
1205
1230
async def request (
1206
1231
self ,
@@ -1262,8 +1287,11 @@ async def _request(
1262
1287
stream_cls : type [_AsyncStreamT ] | None ,
1263
1288
remaining_retries : int | None ,
1264
1289
) -> ResponseT | _AsyncStreamT :
1290
+ await self ._prepare_options (options )
1291
+
1265
1292
retries = self ._remaining_retries (remaining_retries , options )
1266
1293
request = self ._build_request (options )
1294
+ await self ._prepare_request (request )
1267
1295
1268
1296
try :
1269
1297
response = await self ._client .send (request , auth = self .custom_auth , stream = stream )
0 commit comments