From e045b86d7f9bb201ac79ea0e660957a1df1aa7a4 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Nov 2019 11:21:40 +0000 Subject: [PATCH] Clean up '_dispatcher_for_request' into 'dispatcher_for_url' (#561) --- httpx/client.py | 18 +++++++++--------- tests/client/test_proxies.py | 3 +-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/httpx/client.py b/httpx/client.py index 30e2181e..4c594f2b 100644 --- a/httpx/client.py +++ b/httpx/client.py @@ -535,7 +535,7 @@ class Client: Sends a single request, without handling any redirections. """ - dispatcher = self._dispatcher_for_request(request, self.proxies) + dispatcher = self.dispatcher_for_url(request.url) try: with ElapsedTimer() as timer: @@ -560,12 +560,12 @@ class Client: return response - def _dispatcher_for_request( - self, request: Request, proxies: typing.Dict[str, Dispatcher] - ) -> Dispatcher: - """Gets the Dispatcher instance that should be used for a given Request""" - if proxies: - url = request.url + def dispatcher_for_url(self, url: URL) -> Dispatcher: + """ + Returns the Dispatcher instance that should be used for a given URL. + This will either be the standard connection pool, or a proxy. + """ + if self.proxies: is_default_port = (url.scheme == "http" and url.port == 80) or ( url.scheme == "https" and url.port == 443 ) @@ -579,8 +579,8 @@ class Client: "all", ) for proxy_key in proxy_keys: - if proxy_key and proxy_key in proxies: - dispatcher = proxies[proxy_key] + if proxy_key and proxy_key in self.proxies: + dispatcher = self.proxies[proxy_key] return dispatcher return self.dispatch diff --git a/tests/client/test_proxies.py b/tests/client/test_proxies.py index 1c02ce8c..fc1e57a5 100644 --- a/tests/client/test_proxies.py +++ b/tests/client/test_proxies.py @@ -102,8 +102,7 @@ PROXY_URL = "http://[::1]" ) def test_dispatcher_for_request(url, proxies, expected): client = httpx.Client(proxies=proxies) - request = httpx.Request("GET", url) - dispatcher = client._dispatcher_for_request(request, client.proxies) + dispatcher = client.dispatcher_for_url(httpx.URL(url)) if expected is None: assert isinstance(dispatcher, httpx.ConnectionPool) -- 2.47.3