Skip to content

Commit 3901e03

Browse files
committed
httpx helper methods
1 parent 9900423 commit 3901e03

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,17 @@ with httpx.Client(mounts=mounts) as client:
9191
r.headers['X-ProxyMesh-IP']
9292
```
9393

94-
async
94+
helper methods
95+
``` python
96+
import httpx
97+
from python_proxy_headers import httpx_proxy
98+
proxy = httpx.Proxy('http://de.proxymesh.com:31280', headers={'X-ProxyMesh-IP': '134.209.244.192'})
99+
r = httpx_proxy.get('https://proxymesh.com/api/headers/', proxy=proxy)
100+
r.headers['X-ProxyMesh-IP']
101+
```
102+
103+
9. Figure out if httpx async is worth extending
104+
95105
``` python
96106
import httpx
97107
from python_proxy_headers.httpx_proxy import AsyncHTTPProxyTransport
@@ -103,6 +113,7 @@ async with httpx.AsyncClient(mounts=mounts) as client:
103113
r.headers['X-ProxyMesh-IP']
104114
```
105115

106-
9. Figure out if httpx async is worth extending
107116
10. Is there a requests async library worth extending? aiohttp
108-
11. Update proxy-examples repository
117+
11. Update proxy-examples repository
118+
119+
**TODO: rename modules to be more clear**

python_proxy_headers/httpx_proxy.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
from httpcore._sync.http_proxy import HTTPProxy, TunnelHTTPConnection, merge_headers, logger
23
from httpcore._sync.http11 import HTTP11Connection
34
from httpcore._async.http_proxy import AsyncHTTPProxy, AsyncTunnelHTTPConnection
@@ -6,8 +7,8 @@
67
from httpcore._exceptions import ProxyError
78
from httpcore._ssl import default_ssl_context
89
from 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

1213
class 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

Comments
 (0)