Drop backend parameter on AsyncClient (#791)
* Drop backend parameter on AsyncClient * Fix master merge Co-authored-by: Tom Christie <tom@tomchristie.com>
This commit is contained in:
parent
2714f32238
commit
53804173bb
@ -95,18 +95,9 @@ await client.post(url, data=upload_bytes())
|
||||
|
||||
HTTPX supports either `asyncio` or `trio` as an async environment.
|
||||
|
||||
By default it will auto-detect which of those two to use as the backend
|
||||
It will auto-detect which of those two to use as the backend
|
||||
for socket operations and concurrency primitives.
|
||||
|
||||
You can also explicitly select a backend by instantiating a client with the
|
||||
`backend` argument...
|
||||
|
||||
```python
|
||||
client = httpx.AsyncClient(backend='auto') # Autodetection. The default case.
|
||||
client = httpx.AsyncClient(backend='asyncio') # Use asyncio as the backend.
|
||||
client = httpx.AsyncClient(backend='trio') # Use trio as the backend.
|
||||
```
|
||||
|
||||
### [AsyncIO](https://docs.python.org/3/library/asyncio.html)
|
||||
|
||||
AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)
|
||||
|
||||
@ -5,7 +5,6 @@ from types import TracebackType
|
||||
import hstspreload
|
||||
|
||||
from ._auth import Auth, AuthTypes, BasicAuth, FunctionAuth
|
||||
from ._backends.base import ConcurrencyBackend
|
||||
from ._config import (
|
||||
DEFAULT_MAX_REDIRECTS,
|
||||
DEFAULT_POOL_LIMITS,
|
||||
@ -922,9 +921,6 @@ class AsyncClient(BaseClient):
|
||||
over the network.
|
||||
* **app** - *(optional)* An ASGI application to send requests to,
|
||||
rather than sending actual network requests.
|
||||
* **backend** - *(optional)* A concurrency backend to use when issuing
|
||||
async requests. Either 'auto', 'asyncio', 'trio', or a `ConcurrencyBackend`
|
||||
instance. Defaults to 'auto', for autodetection.
|
||||
* **trust_env** - *(optional)* Enables or disables usage of environment
|
||||
variables for configuration.
|
||||
* **uds** - *(optional)* A path to a Unix domain socket to connect through.
|
||||
@ -947,7 +943,6 @@ class AsyncClient(BaseClient):
|
||||
base_url: URLTypes = None,
|
||||
dispatch: AsyncDispatcher = None,
|
||||
app: typing.Callable = None,
|
||||
backend: typing.Union[str, ConcurrencyBackend] = "auto",
|
||||
trust_env: bool = True,
|
||||
uds: str = None,
|
||||
):
|
||||
@ -971,7 +966,6 @@ class AsyncClient(BaseClient):
|
||||
pool_limits=pool_limits,
|
||||
dispatch=dispatch,
|
||||
app=app,
|
||||
backend=backend,
|
||||
trust_env=trust_env,
|
||||
uds=uds,
|
||||
)
|
||||
@ -982,7 +976,6 @@ class AsyncClient(BaseClient):
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
backend=backend,
|
||||
trust_env=trust_env,
|
||||
)
|
||||
for key, proxy in proxy_map.items()
|
||||
@ -996,7 +989,6 @@ class AsyncClient(BaseClient):
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
dispatch: AsyncDispatcher = None,
|
||||
app: typing.Callable = None,
|
||||
backend: typing.Union[str, ConcurrencyBackend] = "auto",
|
||||
trust_env: bool = True,
|
||||
uds: str = None,
|
||||
) -> AsyncDispatcher:
|
||||
@ -1011,7 +1003,6 @@ class AsyncClient(BaseClient):
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
backend=backend,
|
||||
trust_env=trust_env,
|
||||
uds=uds,
|
||||
)
|
||||
@ -1023,7 +1014,6 @@ class AsyncClient(BaseClient):
|
||||
cert: CertTypes = None,
|
||||
http2: bool = False,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
backend: typing.Union[str, ConcurrencyBackend] = "auto",
|
||||
trust_env: bool = True,
|
||||
) -> AsyncDispatcher:
|
||||
return HTTPProxy(
|
||||
@ -1034,7 +1024,6 @@ class AsyncClient(BaseClient):
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
backend=backend,
|
||||
trust_env=trust_env,
|
||||
)
|
||||
|
||||
|
||||
@ -160,10 +160,3 @@ async def test_uds(uds_server):
|
||||
assert response.status_code == 200
|
||||
assert response.text == "Hello, world!"
|
||||
assert response.encoding == "iso-8859-1"
|
||||
|
||||
|
||||
async def test_explicit_backend(server, async_environment):
|
||||
async with httpx.AsyncClient(backend=async_environment) as client:
|
||||
response = await client.get(server.url)
|
||||
assert response.status_code == 200
|
||||
assert response.text == "Hello, world!"
|
||||
|
||||
@ -7,6 +7,7 @@ import pytest
|
||||
from h2.settings import SettingCodes
|
||||
|
||||
from httpx import AsyncClient, Response, TimeoutException
|
||||
from httpx._dispatch.connection_pool import ConnectionPool
|
||||
|
||||
from .utils import MockHTTP2Backend
|
||||
|
||||
@ -25,8 +26,9 @@ async def app(request):
|
||||
@pytest.mark.asyncio
|
||||
async def test_http2_get_request():
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
response = await client.get("http://example.org")
|
||||
|
||||
assert response.status_code == 200
|
||||
@ -36,8 +38,9 @@ async def test_http2_get_request():
|
||||
@pytest.mark.asyncio
|
||||
async def test_http2_post_request():
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
response = await client.post("http://example.org", data=b"<data>")
|
||||
|
||||
assert response.status_code == 200
|
||||
@ -51,9 +54,10 @@ async def test_http2_post_request():
|
||||
@pytest.mark.asyncio
|
||||
async def test_http2_large_post_request():
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
data = b"a" * 100000
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
response = await client.post("http://example.org", data=data)
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.content) == {
|
||||
@ -66,8 +70,9 @@ async def test_http2_large_post_request():
|
||||
@pytest.mark.asyncio
|
||||
async def test_http2_multiple_requests():
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
response_1 = await client.get("http://example.org/1")
|
||||
response_2 = await client.get("http://example.org/2")
|
||||
response_3 = await client.get("http://example.org/3")
|
||||
@ -89,8 +94,9 @@ async def test_http2_reconnect():
|
||||
be seemlessly reconnected.
|
||||
"""
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
response_1 = await client.get("http://example.org/1")
|
||||
backend.server.close_connection = True
|
||||
response_2 = await client.get("http://example.org/2")
|
||||
@ -105,8 +111,9 @@ async def test_http2_reconnect():
|
||||
@pytest.mark.asyncio
|
||||
async def test_http2_settings_in_handshake():
|
||||
backend = MockHTTP2Backend(app=app)
|
||||
dispatch = ConnectionPool(backend=backend, http2=True)
|
||||
|
||||
async with AsyncClient(backend=backend, http2=True) as client:
|
||||
async with AsyncClient(dispatch=dispatch) as client:
|
||||
await client.get("http://example.org")
|
||||
|
||||
h2_conn = backend.server.conn
|
||||
|
||||
@ -10,9 +10,9 @@ from httpx._backends.base import BaseSocketStream, lookup_backend
|
||||
|
||||
|
||||
class MockHTTP2Backend:
|
||||
def __init__(self, app, backend="auto"):
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
self.backend = lookup_backend(backend)
|
||||
self.backend = lookup_backend()
|
||||
self.server = None
|
||||
|
||||
async def open_tcp_stream(
|
||||
@ -31,7 +31,7 @@ class MockHTTP2Backend:
|
||||
|
||||
|
||||
class MockHTTP2Server(BaseSocketStream):
|
||||
def __init__(self, app, backend):
|
||||
def __init__(self, app, backend: MockHTTP2Backend):
|
||||
config = h2.config.H2Configuration(client_side=False)
|
||||
self.conn = h2.connection.H2Connection(config=config)
|
||||
self.app = app
|
||||
@ -151,8 +151,8 @@ class MockHTTP2Server(BaseSocketStream):
|
||||
|
||||
|
||||
class MockRawSocketBackend:
|
||||
def __init__(self, data_to_send=b"", backend="auto"):
|
||||
self.backend = lookup_backend(backend)
|
||||
def __init__(self, data_to_send=b""):
|
||||
self.backend = lookup_backend()
|
||||
self.data_to_send = data_to_send
|
||||
self.received_data = []
|
||||
self.stream = MockRawSocketStream(self)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user