From: Tom Christie Date: Tue, 23 Apr 2019 13:25:47 +0000 (+0100) Subject: Module renaming X-Git-Tag: 0.2.1~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F15%2Fhead;p=thirdparty%2Fhttpx.git Module renaming --- diff --git a/httpcore/__init__.py b/httpcore/__init__.py index 2480785b..c6851003 100644 --- a/httpcore/__init__.py +++ b/httpcore/__init__.py @@ -1,5 +1,5 @@ from .config import PoolLimits, SSLConfig, TimeoutConfig -from .connections import Connection +from .connectionpool import ConnectionPool from .datastructures import URL, Origin, Request, Response from .exceptions import ( BadResponse, @@ -10,7 +10,7 @@ from .exceptions import ( StreamConsumed, Timeout, ) -from .pool import ConnectionPool +from .http11 import HTTP11Connection from .sync import SyncClient, SyncConnectionPool __version__ = "0.2.0" diff --git a/httpcore/compat.py b/httpcore/compat.py index a16ebcc0..35c31d64 100644 --- a/httpcore/compat.py +++ b/httpcore/compat.py @@ -2,6 +2,7 @@ import asyncio if hasattr(asyncio, "run"): asyncio_run = asyncio.run + else: # pragma: nocover def asyncio_run(main, *, debug=False): # type: ignore diff --git a/httpcore/pool.py b/httpcore/connectionpool.py similarity index 93% rename from httpcore/pool.py rename to httpcore/connectionpool.py index a61e67c1..d815919b 100644 --- a/httpcore/pool.py +++ b/httpcore/connectionpool.py @@ -10,7 +10,7 @@ from .config import ( SSLConfig, TimeoutConfig, ) -from .connections import Connection +from .http11 import HTTP11Connection from .datastructures import Client, Origin, Request, Response from .exceptions import PoolTimeout @@ -31,7 +31,7 @@ class ConnectionPool(Client): self.num_keepalive_connections = 0 self._keepalive_connections = ( {} - ) # type: typing.Dict[Origin, typing.List[Connection]] + ) # type: typing.Dict[Origin, typing.List[HTTP11Connection]] self._max_connections = ConnectionSemaphore( max_connections=self.limits.hard_limit ) @@ -53,7 +53,7 @@ class ConnectionPool(Client): async def acquire_connection( self, origin: Origin, timeout: typing.Optional[TimeoutConfig] = None - ) -> Connection: + ) -> HTTP11Connection: try: connection = self._keepalive_connections[origin].pop() if not self._keepalive_connections[origin]: @@ -71,7 +71,7 @@ class ConnectionPool(Client): await asyncio.wait_for(self._max_connections.acquire(), pool_timeout) except asyncio.TimeoutError: raise PoolTimeout() - connection = Connection( + connection = HTTP11Connection( origin, ssl=self.ssl, timeout=self.timeout, @@ -81,7 +81,7 @@ class ConnectionPool(Client): return connection - async def release_connection(self, connection: Connection) -> None: + async def release_connection(self, connection: HTTP11Connection) -> None: if connection.is_closed: self._max_connections.release() self.num_active_connections -= 1 diff --git a/httpcore/datastructures.py b/httpcore/datastructures.py index 016ae8a9..de5ee2f5 100644 --- a/httpcore/datastructures.py +++ b/httpcore/datastructures.py @@ -72,24 +72,20 @@ class Origin: def __init__(self, url: typing.Union[str, URL]) -> None: if isinstance(url, str): url = URL(url) - self.scheme = url.scheme - self.hostname = url.hostname + self.is_ssl = url.scheme == "https" + self.hostname = url.hostname.lower() self.port = url.port - @property - def is_secure(self) -> bool: - return self.scheme == "https" - def __eq__(self, other: typing.Any) -> bool: return ( isinstance(other, self.__class__) - and self.scheme == other.scheme + and self.is_ssl == other.is_ssl and self.hostname == other.hostname and self.port == other.port ) def __hash__(self) -> int: - return hash((self.scheme, self.hostname, self.port)) + return hash((self.is_ssl, self.hostname, self.port)) class Request: diff --git a/httpcore/connections.py b/httpcore/http11.py similarity index 96% rename from httpcore/connections.py rename to httpcore/http11.py index 6ddea6da..23cc27ce 100644 --- a/httpcore/connections.py +++ b/httpcore/http11.py @@ -17,7 +17,7 @@ H11Event = typing.Union[ ] -class Connection(Client): +class HTTP11Connection(Client): def __init__( self, origin: typing.Union[str, Origin], @@ -93,13 +93,13 @@ class Connection(Client): ) async def _connect(self, ssl: SSLConfig, timeout: TimeoutConfig) -> None: - ssl_context = await ssl.load_ssl_context() if self.origin.is_secure else None + hostname = self.origin.hostname + port = self.origin.port + ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None try: self._reader, self._writer = await asyncio.wait_for( # type: ignore - asyncio.open_connection( - self.origin.hostname, self.origin.port, ssl=ssl_context - ), + asyncio.open_connection(hostname, port, ssl=ssl_context), timeout.connect_timeout, ) except asyncio.TimeoutError: diff --git a/httpcore/sync.py b/httpcore/sync.py index 0cc502e1..8ca8b189 100644 --- a/httpcore/sync.py +++ b/httpcore/sync.py @@ -3,8 +3,8 @@ from types import TracebackType from .compat import asyncio_run from .config import SSLConfig, TimeoutConfig +from .connectionpool import ConnectionPool from .datastructures import URL, Client, Response -from .pool import ConnectionPool class SyncResponse: diff --git a/tests/test_pool.py b/tests/test_connection_pools.py similarity index 100% rename from tests/test_pool.py rename to tests/test_connection_pools.py diff --git a/tests/test_connections.py b/tests/test_connections.py index 5cfca611..f1590140 100644 --- a/tests/test_connections.py +++ b/tests/test_connections.py @@ -5,7 +5,7 @@ import httpcore @pytest.mark.asyncio async def test_get(server): - http = httpcore.Connection(origin="http://127.0.0.1:8000/") + http = httpcore.HTTP11Connection(origin="http://127.0.0.1:8000/") response = await http.request("GET", "http://127.0.0.1:8000/") assert response.status_code == 200 assert response.body == b"Hello, world!" @@ -13,7 +13,7 @@ async def test_get(server): @pytest.mark.asyncio async def test_post(server): - http = httpcore.Connection(origin="http://127.0.0.1:8000/") + http = httpcore.HTTP11Connection(origin="http://127.0.0.1:8000/") response = await http.request( "POST", "http://127.0.0.1:8000/", body=b"Hello, world!" ) diff --git a/tests/test_sync.py b/tests/test_sync.py index 372bb920..5879d650 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -53,7 +53,7 @@ def test_stream_iterator(server): with httpcore.SyncConnectionPool() as http: response = http.request("GET", "http://127.0.0.1:8000/", stream=True) assert response.status_code == 200 - body = b'' + body = b"" for chunk in response.stream(): body += chunk assert body == b"Hello, world!"