From: Tom Christie Date: Mon, 6 Jan 2020 12:08:14 +0000 (+0000) Subject: Dispatcher -> AsyncDispatcher (#725) X-Git-Tag: 0.11.0~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e19bd9bc4bf5849650a3847232a4b1353600635e;p=thirdparty%2Fhttpx.git Dispatcher -> AsyncDispatcher (#725) * Dispatcher -> AsyncDispatcher * Fix invalid renamings * Fix invalid renamings --- diff --git a/httpx/client.py b/httpx/client.py index 75f29ac3..c16912f0 100644 --- a/httpx/client.py +++ b/httpx/client.py @@ -21,7 +21,7 @@ from .config import ( ) from .content_streams import ContentStream from .dispatch.asgi import ASGIDispatch -from .dispatch.base import Dispatcher +from .dispatch.base import AsyncDispatcher from .dispatch.connection_pool import ConnectionPool from .dispatch.proxy_http import HTTPProxy from .exceptions import ( @@ -120,7 +120,7 @@ class AsyncClient: pool_limits: PoolLimits = DEFAULT_POOL_LIMITS, max_redirects: int = DEFAULT_MAX_REDIRECTS, base_url: URLTypes = None, - dispatch: Dispatcher = None, + dispatch: AsyncDispatcher = None, app: typing.Callable = None, backend: typing.Union[str, ConcurrencyBackend] = "auto", trust_env: bool = True, @@ -161,7 +161,7 @@ class AsyncClient: if proxies is None and trust_env: proxies = typing.cast(ProxiesTypes, get_environment_proxies()) - self.proxies: typing.Dict[str, Dispatcher] = _proxies_to_dispatchers( + self.proxies: typing.Dict[str, AsyncDispatcher] = _proxies_to_dispatchers( proxies, verify=verify, cert=cert, @@ -621,9 +621,9 @@ class AsyncClient: return response - def dispatcher_for_url(self, url: URL) -> Dispatcher: + def dispatcher_for_url(self, url: URL) -> AsyncDispatcher: """ - Returns the Dispatcher instance that should be used for a given URL. + Returns the AsyncDispatcher instance that should be used for a given URL. This will either be the standard connection pool, or a proxy. """ if self.proxies: @@ -641,8 +641,7 @@ class AsyncClient: ) for proxy_key in proxy_keys: if proxy_key and proxy_key in self.proxies: - dispatcher = self.proxies[proxy_key] - return dispatcher + return self.proxies[proxy_key] return self.dispatch @@ -897,8 +896,8 @@ def _proxies_to_dispatchers( pool_limits: PoolLimits, backend: typing.Union[str, ConcurrencyBackend], trust_env: bool, -) -> typing.Dict[str, Dispatcher]: - def _proxy_from_url(url: URLTypes) -> Dispatcher: +) -> typing.Dict[str, AsyncDispatcher]: + def _proxy_from_url(url: URLTypes) -> AsyncDispatcher: nonlocal verify, cert, http2, pool_limits, backend, trust_env url = URL(url) if url.scheme in ("http", "https"): @@ -917,7 +916,7 @@ def _proxies_to_dispatchers( return {} elif isinstance(proxies, (str, URL)): return {"all": _proxy_from_url(proxies)} - elif isinstance(proxies, Dispatcher): + elif isinstance(proxies, AsyncDispatcher): return {"all": proxies} else: new_proxies = {} diff --git a/httpx/dispatch/asgi.py b/httpx/dispatch/asgi.py index 2f74019e..335540b6 100644 --- a/httpx/dispatch/asgi.py +++ b/httpx/dispatch/asgi.py @@ -3,12 +3,12 @@ import typing from ..config import TimeoutTypes from ..content_streams import ByteStream from ..models import Request, Response -from .base import Dispatcher +from .base import AsyncDispatcher -class ASGIDispatch(Dispatcher): +class ASGIDispatch(AsyncDispatcher): """ - A custom dispatcher that handles sending requests directly to an ASGI app. + A custom AsyncDispatcher that handles sending requests directly to an ASGI app. The simplest way to use this functionality is to use the `app` argument. This will automatically infer if 'app' is a WSGI or an ASGI application, diff --git a/httpx/dispatch/base.py b/httpx/dispatch/base.py index f24eb68a..4879ea82 100644 --- a/httpx/dispatch/base.py +++ b/httpx/dispatch/base.py @@ -12,12 +12,12 @@ from ..models import ( ) -class Dispatcher: +class AsyncDispatcher: """ - Base class for dispatcher classes, that handle sending the request. + Base class for AsyncDispatcher classes, that handle sending the request. Stubs out the interface, as well as providing a `.request()` convenience - implementation, to make it easy to use or test stand-alone dispatchers, + implementation, to make it easy to use or test stand-alone AsyncDispatchers, without requiring a complete `AsyncClient` instance. """ @@ -40,7 +40,7 @@ class Dispatcher: async def close(self) -> None: pass # pragma: nocover - async def __aenter__(self) -> "Dispatcher": + async def __aenter__(self) -> "AsyncDispatcher": return self async def __aexit__( diff --git a/httpx/dispatch/connection.py b/httpx/dispatch/connection.py index d9e104b0..9b6b28b7 100644 --- a/httpx/dispatch/connection.py +++ b/httpx/dispatch/connection.py @@ -7,7 +7,7 @@ from ..backends.base import ConcurrencyBackend, lookup_backend from ..config import SSLConfig, Timeout from ..models import URL, Origin, Request, Response from ..utils import get_logger -from .base import Dispatcher +from .base import AsyncDispatcher from .http2 import HTTP2Connection from .http11 import HTTP11Connection @@ -18,7 +18,7 @@ ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]] logger = get_logger(__name__) -class HTTPConnection(Dispatcher): +class HTTPConnection(AsyncDispatcher): def __init__( self, origin: typing.Union[str, Origin], diff --git a/httpx/dispatch/connection_pool.py b/httpx/dispatch/connection_pool.py index 3e09b110..4ca2e5fd 100644 --- a/httpx/dispatch/connection_pool.py +++ b/httpx/dispatch/connection_pool.py @@ -12,7 +12,7 @@ from ..config import ( from ..exceptions import PoolTimeout from ..models import Origin, Request, Response from ..utils import get_logger -from .base import Dispatcher +from .base import AsyncDispatcher from .connection import HTTPConnection CONNECTIONS_DICT = typing.Dict[Origin, typing.List[HTTPConnection]] @@ -85,7 +85,7 @@ class ConnectionStore: return len(self.all) -class ConnectionPool(Dispatcher): +class ConnectionPool(AsyncDispatcher): KEEP_ALIVE_EXPIRY = 5.0 def __init__( diff --git a/httpx/models.py b/httpx/models.py index dc98b302..7f085d24 100644 --- a/httpx/models.py +++ b/httpx/models.py @@ -52,7 +52,7 @@ from .utils import ( ) if typing.TYPE_CHECKING: # pragma: no cover - from .dispatch.base import Dispatcher # noqa: F401 + from .dispatch.base import AsyncDispatcher # noqa: F401 PrimitiveData = typing.Optional[typing.Union[str, int, float, bool]] @@ -74,7 +74,9 @@ HeaderTypes = typing.Union[ CookieTypes = typing.Union["Cookies", CookieJar, typing.Dict[str, str]] ProxiesTypes = typing.Union[ - URLTypes, "Dispatcher", typing.Dict[URLTypes, typing.Union[URLTypes, "Dispatcher"]] + URLTypes, + "AsyncDispatcher", + typing.Dict[URLTypes, typing.Union[URLTypes, "AsyncDispatcher"]], ] diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index 34ec77eb..af9a17be 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -16,10 +16,10 @@ from httpx import ( ) from httpx.auth import Auth, AuthFlow from httpx.config import CertTypes, TimeoutTypes, VerifyTypes -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): def __init__(self, auth_header: str = "", status_code: int = 200) -> None: self.auth_header = auth_header self.status_code = status_code @@ -38,7 +38,7 @@ class MockDispatch(Dispatcher): ) -class MockDigestAuthDispatch(Dispatcher): +class MockDigestAuthDispatch(AsyncDispatcher): def __init__( self, algorithm: str = "SHA-256", diff --git a/tests/client/test_cookies.py b/tests/client/test_cookies.py index 3e8d0d3b..2932232d 100644 --- a/tests/client/test_cookies.py +++ b/tests/client/test_cookies.py @@ -5,10 +5,10 @@ import pytest from httpx import AsyncClient, Cookies, Request, Response from httpx.config import CertTypes, TimeoutTypes, VerifyTypes -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): async def send( self, request: Request, diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py index 672092d8..56cf24c7 100755 --- a/tests/client/test_headers.py +++ b/tests/client/test_headers.py @@ -6,10 +6,10 @@ import pytest from httpx import AsyncClient, Headers, Request, Response, __version__ from httpx.config import CertTypes, TimeoutTypes, VerifyTypes -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): async def send( self, request: Request, diff --git a/tests/client/test_queryparams.py b/tests/client/test_queryparams.py index a887f1c2..e0ce45b2 100644 --- a/tests/client/test_queryparams.py +++ b/tests/client/test_queryparams.py @@ -4,10 +4,10 @@ import pytest from httpx import URL, AsyncClient, QueryParams, Request, Response from httpx.config import CertTypes, TimeoutTypes, VerifyTypes -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): async def send( self, request: Request, diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index 4c027455..a48e478e 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -15,10 +15,10 @@ from httpx import ( codes, ) from httpx.config import CertTypes, TimeoutTypes, VerifyTypes -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): async def send( self, request: Request, @@ -305,7 +305,7 @@ async def test_cross_subdomain_redirect(): assert response.url == URL("https://www.example.org/cross_subdomain") -class MockCookieDispatch(Dispatcher): +class MockCookieDispatch(AsyncDispatcher): async def send( self, request: Request, diff --git a/tests/test_multipart.py b/tests/test_multipart.py index eb1eaac4..e0bd750c 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -9,11 +9,11 @@ import pytest import httpx from httpx.config import CertTypes, TimeoutTypes, VerifyTypes from httpx.content_streams import encode -from httpx.dispatch.base import Dispatcher +from httpx.dispatch.base import AsyncDispatcher from httpx.utils import format_form_param -class MockDispatch(Dispatcher): +class MockDispatch(AsyncDispatcher): async def send( self, request: httpx.Request,