From: Tom Christie Date: Tue, 20 Aug 2019 12:30:54 +0000 (+0100) Subject: Add Client(http_versions=...) support X-Git-Tag: 0.7.2~19^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b343ee4a70f24937f21ee7a4667c7d9bc48b6753;p=thirdparty%2Fhttpx.git Add Client(http_versions=...) support --- diff --git a/httpx/__init__.py b/httpx/__init__.py index 7be5bc25..bc8917b3 100644 --- a/httpx/__init__.py +++ b/httpx/__init__.py @@ -5,9 +5,9 @@ from .concurrency.asyncio import AsyncioBackend from .config import ( USER_AGENT, CertTypes, - PoolLimits, HTTPVersionConfig, HTTPVersionTypes, + PoolLimits, SSLConfig, TimeoutConfig, TimeoutTypes, @@ -117,6 +117,8 @@ __all__ = [ "StatusCode", "codes", "TimeoutTypes", + "HTTPVersionTypes", + "HTTPVersionConfig", "AsyncRequest", "AsyncRequestData", "AsyncResponse", diff --git a/httpx/api.py b/httpx/api.py index 84892880..fe52e14e 100644 --- a/httpx/api.py +++ b/httpx/api.py @@ -24,7 +24,6 @@ def request( json: typing.Any = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - # files auth: AuthTypes = None, timeout: TimeoutTypes = None, allow_redirects: bool = True, diff --git a/httpx/client.py b/httpx/client.py index ae75f15d..9db68f75 100644 --- a/httpx/client.py +++ b/httpx/client.py @@ -11,6 +11,7 @@ from .config import ( DEFAULT_POOL_LIMITS, DEFAULT_TIMEOUT_CONFIG, CertTypes, + HTTPVersionTypes, PoolLimits, TimeoutTypes, VerifyTypes, @@ -57,6 +58,7 @@ class BaseClient: cookies: CookieTypes = None, verify: VerifyTypes = True, cert: CertTypes = None, + http_versions: HTTPVersionTypes = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, pool_limits: PoolLimits = DEFAULT_POOL_LIMITS, max_redirects: int = DEFAULT_MAX_REDIRECTS, @@ -84,6 +86,7 @@ class BaseClient: verify=verify, cert=cert, timeout=timeout, + http_versions=http_versions, pool_limits=pool_limits, backend=backend, ) diff --git a/httpx/concurrency/asyncio.py b/httpx/concurrency/asyncio.py index 613d48f9..6c5dc0c4 100644 --- a/httpx/concurrency/asyncio.py +++ b/httpx/concurrency/asyncio.py @@ -14,7 +14,7 @@ import ssl import typing from types import TracebackType -from ..config import PoolLimits, HTTPVersionConfig, TimeoutConfig +from ..config import PoolLimits, TimeoutConfig from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout from ..interfaces import ( BaseBackgroundManager, diff --git a/httpx/config.py b/httpx/config.py index 2b5ee9e3..08b99c0d 100644 --- a/httpx/config.py +++ b/httpx/config.py @@ -246,11 +246,11 @@ class HTTPVersionConfig: http_versions = ["HTTP/1.1", "HTTP/2"] if isinstance(http_versions, str): - self.http_versions = set([http_versions.upper()]) + self.http_versions = {http_versions.upper()} elif isinstance(http_versions, HTTPVersionConfig): self.http_versions = http_versions.http_versions else: - self.http_versions = set([version.upper() for version in http_versions]) + self.http_versions = {version.upper() for version in http_versions} for version in self.http_versions: if version not in ("HTTP/1.1", "HTTP/2"): diff --git a/httpx/dispatch/asgi.py b/httpx/dispatch/asgi.py index e3a38ad8..e3b4b0a0 100644 --- a/httpx/dispatch/asgi.py +++ b/httpx/dispatch/asgi.py @@ -1,7 +1,7 @@ import asyncio import typing -from ..config import CertTypes, HTTPVersionTypes, TimeoutTypes, VerifyTypes +from ..config import CertTypes, TimeoutTypes, VerifyTypes from ..interfaces import AsyncDispatcher from ..models import AsyncRequest, AsyncResponse @@ -59,7 +59,6 @@ class ASGIDispatch(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: scope = { diff --git a/httpx/dispatch/connection.py b/httpx/dispatch/connection.py index a0d124fc..b40ae340 100644 --- a/httpx/dispatch/connection.py +++ b/httpx/dispatch/connection.py @@ -1,13 +1,13 @@ import functools -import typing import ssl +import typing from ..concurrency.asyncio import AsyncioBackend from ..config import ( DEFAULT_TIMEOUT_CONFIG, CertTypes, - HTTPVersionTypes, HTTPVersionConfig, + HTTPVersionTypes, SSLConfig, TimeoutConfig, TimeoutTypes, @@ -48,12 +48,9 @@ class HTTPConnection(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: if self.h11_connection is None and self.h2_connection is None: - await self.connect( - verify=verify, cert=cert, timeout=timeout, http_versions=http_versions - ) + await self.connect(verify=verify, cert=cert, timeout=timeout) if self.h2_connection is not None: response = await self.h2_connection.send(request, timeout=timeout) @@ -68,19 +65,13 @@ class HTTPConnection(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> None: ssl = self.ssl.with_overrides(verify=verify, cert=cert) timeout = self.timeout if timeout is None else TimeoutConfig(timeout) - http_versions = ( - self.http_versions - if http_versions is None - else HTTPVersionConfig(http_versions) - ) host = self.origin.host port = self.origin.port - ssl_context = await self.get_ssl_context(ssl, http_versions) + ssl_context = await self.get_ssl_context(ssl) if self.release_func is None: on_release = None @@ -99,14 +90,14 @@ class HTTPConnection(AsyncDispatcher): reader, writer, self.backend, on_release=on_release ) - async def get_ssl_context( - self, ssl: SSLConfig, http_versions: HTTPVersionConfig - ) -> typing.Optional[ssl.SSLContext]: + async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]: if not self.origin.is_ssl: return None # Run the SSL loading in a threadpool, since it may makes disk accesses. - return await self.backend.run_in_threadpool(ssl.load_ssl_context, http_versions) + return await self.backend.run_in_threadpool( + ssl.load_ssl_context, self.http_versions + ) async def close(self) -> None: if self.h2_connection is not None: diff --git a/httpx/dispatch/connection_pool.py b/httpx/dispatch/connection_pool.py index 48a3433f..b88d142c 100644 --- a/httpx/dispatch/connection_pool.py +++ b/httpx/dispatch/connection_pool.py @@ -107,16 +107,11 @@ class ConnectionPool(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: connection = await self.acquire_connection(origin=request.url.origin) try: response = await connection.send( - request, - verify=verify, - cert=cert, - timeout=timeout, - http_versions=http_versions, + request, verify=verify, cert=cert, timeout=timeout ) except BaseException as exc: self.active_connections.remove(connection) diff --git a/httpx/dispatch/threaded.py b/httpx/dispatch/threaded.py index 902e0652..e4fdfd5e 100644 --- a/httpx/dispatch/threaded.py +++ b/httpx/dispatch/threaded.py @@ -1,4 +1,4 @@ -from ..config import CertTypes, HTTPVersionTypes, TimeoutTypes, VerifyTypes +from ..config import CertTypes, TimeoutTypes, VerifyTypes from ..interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher from ..models import ( AsyncRequest, @@ -29,7 +29,6 @@ class ThreadedDispatcher(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: concurrency_backend = self.backend @@ -49,7 +48,6 @@ class ThreadedDispatcher(AsyncDispatcher): "verify": verify, "cert": cert, "timeout": timeout, - "http_versions": http_versions, } sync_response = await self.backend.run_in_threadpool(func, **kwargs) assert isinstance(sync_response, Response) diff --git a/httpx/dispatch/wsgi.py b/httpx/dispatch/wsgi.py index d8f56d22..60e0a18c 100644 --- a/httpx/dispatch/wsgi.py +++ b/httpx/dispatch/wsgi.py @@ -1,7 +1,7 @@ import io import typing -from ..config import CertTypes, HTTPVersionTypes, TimeoutTypes, VerifyTypes +from ..config import CertTypes, TimeoutTypes, VerifyTypes from ..interfaces import Dispatcher from ..models import Request, Response @@ -60,7 +60,6 @@ class WSGIDispatch(Dispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> Response: environ = { "wsgi.version": (1, 0), diff --git a/httpx/interfaces.py b/httpx/interfaces.py index db6edaf4..2b4edf4d 100644 --- a/httpx/interfaces.py +++ b/httpx/interfaces.py @@ -3,15 +3,7 @@ import ssl import typing from types import TracebackType -from .config import ( - CertTypes, - PoolLimits, - HTTPVersionConfig, - HTTPVersionTypes, - TimeoutConfig, - TimeoutTypes, - VerifyTypes, -) +from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypes from .models import ( AsyncRequest, AsyncRequestData, @@ -50,16 +42,9 @@ class AsyncDispatcher: verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: request = AsyncRequest(method, url, data=data, params=params, headers=headers) - return await self.send( - request, - verify=verify, - cert=cert, - timeout=timeout, - http_versions=http_versions, - ) + return await self.send(request, verify=verify, cert=cert, timeout=timeout) async def send( self, @@ -67,7 +52,6 @@ class AsyncDispatcher: verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: raise NotImplementedError() # pragma: nocover @@ -106,16 +90,9 @@ class Dispatcher: verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> Response: request = Request(method, url, data=data, params=params, headers=headers) - return self.send( - request, - verify=verify, - cert=cert, - timeout=timeout, - http_versions=http_versions, - ) + return self.send(request, verify=verify, cert=cert, timeout=timeout) def send( self, @@ -123,7 +100,6 @@ class Dispatcher: verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> Response: raise NotImplementedError() # pragma: nocover diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index ff5b8e36..725ea56c 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -8,7 +8,6 @@ from httpx import ( AsyncResponse, CertTypes, Client, - HTTPVersionTypes, TimeoutTypes, VerifyTypes, ) @@ -21,7 +20,6 @@ class MockDispatch(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: body = json.dumps({"auth": request.headers.get("Authorization")}).encode() return AsyncResponse(200, content=body, request=request) diff --git a/tests/client/test_cookies.py b/tests/client/test_cookies.py index 9585d21a..280f44bc 100644 --- a/tests/client/test_cookies.py +++ b/tests/client/test_cookies.py @@ -8,7 +8,6 @@ from httpx import ( CertTypes, Client, Cookies, - HTTPVersionTypes, TimeoutTypes, VerifyTypes, ) @@ -21,7 +20,6 @@ class MockDispatch(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: if request.url.path.startswith("/echo_cookies"): body = json.dumps({"cookies": request.headers.get("Cookie")}).encode() diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py index 5410e397..2d17c125 100755 --- a/tests/client/test_headers.py +++ b/tests/client/test_headers.py @@ -8,7 +8,6 @@ from httpx import ( AsyncResponse, CertTypes, Client, - HTTPVersionTypes, TimeoutTypes, VerifyTypes, __version__, @@ -22,7 +21,6 @@ class MockDispatch(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: if request.url.path.startswith("/echo_headers"): request_headers = dict(request.headers.items()) diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index 3792ee85..3062733a 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -12,7 +12,6 @@ from httpx import ( CertTypes, RedirectBodyUnavailable, RedirectLoop, - HTTPVersionTypes, TimeoutTypes, TooManyRedirects, VerifyTypes, @@ -27,7 +26,6 @@ class MockDispatch(AsyncDispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> AsyncResponse: if request.url.path == "/redirect_301": status_code = codes.MOVED_PERMANENTLY diff --git a/tests/dispatch/test_threaded.py b/tests/dispatch/test_threaded.py index 488e13d9..ac90bdd2 100644 --- a/tests/dispatch/test_threaded.py +++ b/tests/dispatch/test_threaded.py @@ -4,7 +4,6 @@ from httpx import ( CertTypes, Client, Dispatcher, - HTTPVersionTypes, Request, Response, TimeoutTypes, @@ -24,7 +23,6 @@ class MockDispatch(Dispatcher): verify: VerifyTypes = None, cert: CertTypes = None, timeout: TimeoutTypes = None, - http_versions: HTTPVersionTypes = None, ) -> Response: if request.url.path == "/streaming_response": return Response(200, content=streaming_body(), request=request) diff --git a/tests/dispatch/utils.py b/tests/dispatch/utils.py index de44f711..c92fa7a3 100644 --- a/tests/dispatch/utils.py +++ b/tests/dispatch/utils.py @@ -13,7 +13,6 @@ from httpx import ( Protocol, Request, TimeoutConfig, - HTTPVersionConfig, ) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index ba346c8a..052cb90d 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -10,9 +10,9 @@ from httpx import ( CertTypes, Client, Dispatcher, + HTTPVersionTypes, Request, Response, - HTTPVersionTypes, TimeoutTypes, VerifyTypes, multipart,