]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Drop backend parameter on AsyncClient (#791)
authorFlorimond Manca <florimond.manca@gmail.com>
Thu, 27 Feb 2020 20:42:18 +0000 (21:42 +0100)
committerGitHub <noreply@github.com>
Thu, 27 Feb 2020 20:42:18 +0000 (20:42 +0000)
* Drop backend parameter on AsyncClient

* Fix master merge

Co-authored-by: Tom Christie <tom@tomchristie.com>
docs/async.md
httpx/_client.py
tests/client/test_async_client.py
tests/dispatch/test_http2.py
tests/dispatch/utils.py

index 81885eef29b17e8a7dd90b37bf4c832e5f20aee2..0e83ebf6a17ebb5815c992730b9d16e5dc87f17c 100644 (file)
@@ -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)
index 7401a36fff63a48074f4cb7a003ea41233219b01..e9d33a94a37d19ec54ac2fd1253033b473d3e1c3 100644 (file)
@@ -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,
         )
 
index 59438399669860542b98a07a13c09d5a23a6847c..269dc267de059916b983ac49c6afbd38b6e08e7f 100644 (file)
@@ -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!"
index 07b0229bfa6999de0f1876c1e7375292dff84d82..b2efcfad5d2d959ff7539644ec463ea01d1a3df3 100644 (file)
@@ -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
index 9939d6d297f33cbb76f5d652ebc61cb6779a6ed5..d2690c973ee402fd572e8cd38680f00105416495 100644 (file)
@@ -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)