1+ from contextlib import contextmanager
12from httpcore ._sync .http_proxy import HTTPProxy , TunnelHTTPConnection , merge_headers , logger
23from httpcore ._sync .http11 import HTTP11Connection
34from httpcore ._async .http_proxy import AsyncHTTPProxy , AsyncTunnelHTTPConnection
67from httpcore ._exceptions import ProxyError
78from httpcore ._ssl import default_ssl_context
89from httpcore ._trace import Trace
9- from httpx import AsyncHTTPTransport , HTTPTransport
10- from httpx ._config import DEFAULT_LIMITS , Proxy , create_ssl_context
10+ from httpx import AsyncHTTPTransport , HTTPTransport , Client
11+ from httpx ._config import DEFAULT_LIMITS , DEFAULT_TIMEOUT_CONFIG , Proxy , create_ssl_context
1112
1213class ProxyTunnelHTTPConnection (TunnelHTTPConnection ):
1314 # Unfortunately the only way to get connect_response.headers into the Response
@@ -311,4 +312,65 @@ def __init__(
311312 socket_options = socket_options ,
312313 )
313314 else :
314- super ().__init__ (verify , cert , trust_env , http1 , http2 , limits , proxy , uds , local_address , retries , socket_options )
315+ super ().__init__ (verify , cert , trust_env , http1 , http2 , limits , proxy , uds , local_address , retries , socket_options )
316+
317+ def request (method : str ,
318+ url : URL | str ,
319+ * ,
320+ cookies = None ,
321+ proxy = None ,
322+ timeout = DEFAULT_TIMEOUT_CONFIG ,
323+ verify = True ,
324+ trust_env : bool = True ,
325+ ** kwargs ):
326+ transport = HTTPProxyTransport (proxy = proxy )
327+ with Client (
328+ cookies = cookies ,
329+ verify = verify ,
330+ timeout = timeout ,
331+ trust_env = trust_env ,
332+ mounts = {'http://' : transport , 'https://' : transport }
333+ ) as client :
334+ return client .request (method = method , url = url , ** kwargs )
335+
336+ def get (* args , ** kwargs ):
337+ return request ('GET' , * args , ** kwargs )
338+
339+ def options (* args , ** kwargs ):
340+ return request ('OPTIONS' , * args , ** kwargs )
341+
342+ def head (* args , ** kwargs ):
343+ return request ('HEAD' , * args , ** kwargs )
344+
345+ def post (* args , ** kwargs ):
346+ return request ('POST' , * args , ** kwargs )
347+
348+ def put (* args , ** kwargs ):
349+ return request ('PUT' , * args , ** kwargs )
350+
351+ def patch (* args , ** kwargs ):
352+ return request ('PATCH' , * args , ** kwargs )
353+
354+ def delete (* args , ** kwargs ):
355+ return request ('DELETE' , * args , ** kwargs )
356+
357+ @contextmanager
358+ def stream (method : str ,
359+ url : URL | str ,
360+ * ,
361+ cookies = None ,
362+ proxy = None ,
363+ timeout = DEFAULT_TIMEOUT_CONFIG ,
364+ verify = True ,
365+ trust_env : bool = True ,
366+ ** kwargs ):
367+ transport = HTTPProxyTransport (proxy = proxy )
368+ with Client (
369+ cookies = cookies ,
370+ verify = verify ,
371+ timeout = timeout ,
372+ trust_env = trust_env ,
373+ mounts = {'http://' : transport , 'https://' : transport }
374+ ) as client :
375+ with client .stream (method = method , url = url , ** kwargs ) as response :
376+ yield response
0 commit comments