22
33import httpx
44from multidict import CIMultiDict
5- from yarl import URL
65
76from extapi .http .abc import AbstractExecutor
87from extapi .http .types import (
@@ -28,6 +27,16 @@ async def read(self) -> bytes:
2827 return await self ._original .aread ()
2928
3029
30+ _httpx_extra_kwargs = [
31+ "content" ,
32+ "files" ,
33+ "cookies" ,
34+ "auth" ,
35+ "follow_redirects" ,
36+ "extensions" ,
37+ ]
38+
39+
3140class HttpxExecutor (AbstractExecutor [httpx .Response ], metaclass = abc .ABCMeta ):
3241 __slots__ = (
3342 "_client" ,
@@ -46,26 +55,35 @@ def __init__(
4655 verify = kwargs .pop ("verify" , None )
4756 if verify is None :
4857 verify = check_ssl
49- self ._client = httpx . AsyncClient (
58+ self ._client = self . _make_client (
5059 verify = verify , follow_redirects = follow_redirects , ** kwargs
5160 )
5261 self ._default_timeout = default_timeout
5362
63+ def _make_client (self , * args , ** kwargs ) -> httpx .AsyncClient :
64+ return httpx .AsyncClient (* args , ** kwargs )
65+
5466 async def close (self ):
5567 await self ._client .aclose ()
5668
5769 async def execute (self , request : RequestData ) -> Response [httpx .Response ]:
5870 timeout = request .timeout or self ._default_timeout
59- url = request .url
60-
61- if isinstance (url , URL ):
62- url = str (url )
71+ url = str (request .url )
6372
6473 if request .headers is None :
6574 httpx_headers = []
6675 else :
6776 httpx_headers = [(k , str (v )) for k , v in request .headers .items ()]
6877
78+ # httpx-specific kwargs
79+ # we need to pull them individually because
80+ # we may have our own custom kwargs
81+ httpx_kwargs = {
82+ key : request .kwargs [key ]
83+ for key in _httpx_extra_kwargs
84+ if key in request .kwargs
85+ }
86+
6987 response = await self ._client .stream (
7088 method = request .method ,
7189 url = url ,
@@ -74,11 +92,11 @@ async def execute(self, request: RequestData) -> Response[httpx.Response]:
7492 data = request .data ,
7593 headers = httpx_headers ,
7694 timeout = timeout ,
77- ** request . kwargs ,
95+ ** httpx_kwargs ,
7896 ).__aenter__ ()
7997
8098 return Response [httpx .Response ](
81- url = url ,
99+ url = request . url ,
82100 status = response .status_code ,
83101 headers = CIMultiDict (response .headers ),
84102 backend_response = HttpxResponseWrap (response ),
0 commit comments