]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Refactor interfaces (#252)
authorTom Christie <tom@tomchristie.com>
Wed, 21 Aug 2019 06:35:01 +0000 (07:35 +0100)
committerFlorimond Manca <florimond.manca@gmail.com>
Wed, 21 Aug 2019 06:35:01 +0000 (08:35 +0200)
* Move Dispatcher, AsyncDispatcher into dispatch/base.py

* Move concurrency interfaces into concurrency/base.py

12 files changed:
httpx/__init__.py
httpx/client.py
httpx/concurrency/asyncio.py
httpx/concurrency/base.py [moved from httpx/interfaces.py with 56% similarity]
httpx/dispatch/asgi.py
httpx/dispatch/base.py [new file with mode: 0644]
httpx/dispatch/connection.py
httpx/dispatch/connection_pool.py
httpx/dispatch/http11.py
httpx/dispatch/http2.py
httpx/dispatch/threaded.py
httpx/dispatch/wsgi.py

index 69759f4d75f669e17c13c8a498942f95888a5cbb..ebacaad87ebe4fc2f1c98b36e22d3d0e246bdd82 100644 (file)
@@ -2,6 +2,13 @@ from .__version__ import __description__, __title__, __version__
 from .api import delete, get, head, options, patch, post, put, request
 from .client import AsyncClient, Client
 from .concurrency.asyncio import AsyncioBackend
+from .concurrency.base import (
+    BaseBackgroundManager,
+    BasePoolSemaphore,
+    BaseReader,
+    BaseWriter,
+    ConcurrencyBackend,
+)
 from .config import (
     USER_AGENT,
     CertTypes,
@@ -13,6 +20,7 @@ from .config import (
     TimeoutTypes,
     VerifyTypes,
 )
+from .dispatch.base import AsyncDispatcher, Dispatcher
 from .dispatch.connection import HTTPConnection
 from .dispatch.connection_pool import ConnectionPool
 from .exceptions import (
@@ -32,15 +40,6 @@ from .exceptions import (
     TooManyRedirects,
     WriteTimeout,
 )
-from .interfaces import (
-    AsyncDispatcher,
-    BaseBackgroundManager,
-    BasePoolSemaphore,
-    BaseReader,
-    BaseWriter,
-    ConcurrencyBackend,
-    Dispatcher,
-)
 from .models import (
     URL,
     AsyncRequest,
index 04dbfea538709be30e547807fa20d179497f0dd0..f7a3c5db6264e3e941950e052e7ab9e995cc57e6 100644 (file)
@@ -6,6 +6,7 @@ import hstspreload
 
 from .auth import HTTPBasicAuth
 from .concurrency.asyncio import AsyncioBackend
+from .concurrency.base import ConcurrencyBackend
 from .config import (
     DEFAULT_MAX_REDIRECTS,
     DEFAULT_POOL_LIMITS,
@@ -17,6 +18,7 @@ from .config import (
     VerifyTypes,
 )
 from .dispatch.asgi import ASGIDispatch
+from .dispatch.base import AsyncDispatcher, Dispatcher
 from .dispatch.connection_pool import ConnectionPool
 from .dispatch.threaded import ThreadedDispatcher
 from .dispatch.wsgi import WSGIDispatch
@@ -27,7 +29,6 @@ from .exceptions import (
     RedirectLoop,
     TooManyRedirects,
 )
-from .interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher
 from .models import (
     URL,
     AsyncRequest,
index 08e39839ec9d6cef045968dea510a5add33686cf..f378e4d940a42d1458c8e2e218bfe551246abc33 100644 (file)
@@ -14,16 +14,17 @@ import ssl
 import typing
 from types import TracebackType
 
-from ..config import PoolLimits, TimeoutConfig
-from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout
-from ..interfaces import (
+from .base import (
     BaseBackgroundManager,
     BasePoolSemaphore,
     BaseQueue,
     BaseReader,
     BaseWriter,
     ConcurrencyBackend,
+    TimeoutFlag,
 )
+from ..config import PoolLimits, TimeoutConfig
+from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout
 
 SSL_MONKEY_PATCH_APPLIED = False
 
@@ -49,38 +50,6 @@ def ssl_monkey_patch() -> None:
     MonkeyPatch.write = _fixed_write
 
 
-class TimeoutFlag:
-    """
-    A timeout flag holds a state of either read-timeout or write-timeout mode.
-
-    We use this so that we can attempt both reads and writes concurrently, while
-    only enforcing timeouts in one direction.
-
-    During a request/response cycle we start in write-timeout mode.
-
-    Once we've sent a request fully, or once we start seeing a response,
-    then we switch to read-timeout mode instead.
-    """
-
-    def __init__(self) -> None:
-        self.raise_on_read_timeout = False
-        self.raise_on_write_timeout = True
-
-    def set_read_timeouts(self) -> None:
-        """
-        Set the flag to read-timeout mode.
-        """
-        self.raise_on_read_timeout = True
-        self.raise_on_write_timeout = False
-
-    def set_write_timeouts(self) -> None:
-        """
-        Set the flag to write-timeout mode.
-        """
-        self.raise_on_read_timeout = False
-        self.raise_on_write_timeout = True
-
-
 class Reader(BaseReader):
     def __init__(
         self, stream_reader: asyncio.StreamReader, timeout: TimeoutConfig
similarity index 56%
rename from httpx/interfaces.py
rename to httpx/concurrency/base.py
index cdfc0726f3d5de79b9955189da541487e60b68c4..1a6842b85267221a348a513e4e5080d5730a1506 100644 (file)
@@ -2,114 +2,39 @@ import ssl
 import typing
 from types import TracebackType
 
-from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypes
-from .models import (
-    AsyncRequest,
-    AsyncRequestData,
-    AsyncResponse,
-    HeaderTypes,
-    QueryParamTypes,
-    Request,
-    RequestData,
-    Response,
-    URLTypes,
-)
-
-
-class AsyncDispatcher:
-    """
-    Base class for async dispatcher 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,
-    without requiring a complete `Client` instance.
-    """
-
-    async def request(
-        self,
-        method: str,
-        url: URLTypes,
-        *,
-        data: AsyncRequestData = b"",
-        params: QueryParamTypes = None,
-        headers: HeaderTypes = None,
-        verify: VerifyTypes = None,
-        cert: CertTypes = None,
-        timeout: TimeoutTypes = None,
-    ) -> AsyncResponse:
-        request = AsyncRequest(method, url, data=data, params=params, headers=headers)
-        return await self.send(request, verify=verify, cert=cert, timeout=timeout)
-
-    async def send(
-        self,
-        request: AsyncRequest,
-        verify: VerifyTypes = None,
-        cert: CertTypes = None,
-        timeout: TimeoutTypes = None,
-    ) -> AsyncResponse:
-        raise NotImplementedError()  # pragma: nocover
-
-    async def close(self) -> None:
-        pass  # pragma: nocover
-
-    async def __aenter__(self) -> "AsyncDispatcher":
-        return self
-
-    async def __aexit__(
-        self,
-        exc_type: typing.Type[BaseException] = None,
-        exc_value: BaseException = None,
-        traceback: TracebackType = None,
-    ) -> None:
-        await self.close()
+from ..config import PoolLimits, TimeoutConfig
 
 
-class Dispatcher:
+class TimeoutFlag:
     """
-    Base class for synchronous dispatcher classes, that handle sending the request.
+    A timeout flag holds a state of either read-timeout or write-timeout mode.
 
-    Stubs out the interface, as well as providing a `.request()` convenience
-    implementation, to make it easy to use or test stand-alone dispatchers,
-    without requiring a complete `Client` instance.
-    """
-
-    def request(
-        self,
-        method: str,
-        url: URLTypes,
-        *,
-        data: RequestData = b"",
-        params: QueryParamTypes = None,
-        headers: HeaderTypes = None,
-        verify: VerifyTypes = None,
-        cert: CertTypes = None,
-        timeout: TimeoutTypes = None,
-    ) -> Response:
-        request = Request(method, url, data=data, params=params, headers=headers)
-        return self.send(request, verify=verify, cert=cert, timeout=timeout)
-
-    def send(
-        self,
-        request: Request,
-        verify: VerifyTypes = None,
-        cert: CertTypes = None,
-        timeout: TimeoutTypes = None,
-    ) -> Response:
-        raise NotImplementedError()  # pragma: nocover
+    We use this so that we can attempt both reads and writes concurrently, while
+    only enforcing timeouts in one direction.
 
-    def close(self) -> None:
-        pass  # pragma: nocover
+    During a request/response cycle we start in write-timeout mode.
 
-    def __enter__(self) -> "Dispatcher":
-        return self
+    Once we've sent a request fully, or once we start seeing a response,
+    then we switch to read-timeout mode instead.
+    """
 
-    def __exit__(
-        self,
-        exc_type: typing.Type[BaseException] = None,
-        exc_value: BaseException = None,
-        traceback: TracebackType = None,
-    ) -> None:
-        self.close()
+    def __init__(self) -> None:
+        self.raise_on_read_timeout = False
+        self.raise_on_write_timeout = True
+
+    def set_read_timeouts(self) -> None:
+        """
+        Set the flag to read-timeout mode.
+        """
+        self.raise_on_read_timeout = True
+        self.raise_on_write_timeout = False
+
+    def set_write_timeouts(self) -> None:
+        """
+        Set the flag to write-timeout mode.
+        """
+        self.raise_on_read_timeout = False
+        self.raise_on_write_timeout = True
 
 
 class BaseReader:
index 5b6bcad6d493e31270846bbe5dd2fb40bc5ac81b..bf926f482cf4bf06e133ace20a0e0971cbdb4a79 100644 (file)
@@ -1,9 +1,10 @@
 import asyncio
 import typing
 
+from .base import AsyncDispatcher
+from ..concurrency.base import ConcurrencyBackend
 from ..concurrency.asyncio import AsyncioBackend
 from ..config import CertTypes, TimeoutTypes, VerifyTypes
-from ..interfaces import AsyncDispatcher, ConcurrencyBackend
 from ..models import AsyncRequest, AsyncResponse
 
 
diff --git a/httpx/dispatch/base.py b/httpx/dispatch/base.py
new file mode 100644 (file)
index 0000000..d0baa51
--- /dev/null
@@ -0,0 +1,111 @@
+import typing
+from types import TracebackType
+
+from ..config import CertTypes, TimeoutTypes, VerifyTypes
+from ..models import (
+    AsyncRequest,
+    AsyncRequestData,
+    AsyncResponse,
+    HeaderTypes,
+    QueryParamTypes,
+    Request,
+    RequestData,
+    Response,
+    URLTypes,
+)
+
+
+class AsyncDispatcher:
+    """
+    Base class for async dispatcher 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,
+    without requiring a complete `Client` instance.
+    """
+
+    async def request(
+        self,
+        method: str,
+        url: URLTypes,
+        *,
+        data: AsyncRequestData = b"",
+        params: QueryParamTypes = None,
+        headers: HeaderTypes = None,
+        verify: VerifyTypes = None,
+        cert: CertTypes = None,
+        timeout: TimeoutTypes = None,
+    ) -> AsyncResponse:
+        request = AsyncRequest(method, url, data=data, params=params, headers=headers)
+        return await self.send(request, verify=verify, cert=cert, timeout=timeout)
+
+    async def send(
+        self,
+        request: AsyncRequest,
+        verify: VerifyTypes = None,
+        cert: CertTypes = None,
+        timeout: TimeoutTypes = None,
+    ) -> AsyncResponse:
+        raise NotImplementedError()  # pragma: nocover
+
+    async def close(self) -> None:
+        pass  # pragma: nocover
+
+    async def __aenter__(self) -> "AsyncDispatcher":
+        return self
+
+    async def __aexit__(
+        self,
+        exc_type: typing.Type[BaseException] = None,
+        exc_value: BaseException = None,
+        traceback: TracebackType = None,
+    ) -> None:
+        await self.close()
+
+
+class Dispatcher:
+    """
+    Base class for synchronous dispatcher 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,
+    without requiring a complete `Client` instance.
+    """
+
+    def request(
+        self,
+        method: str,
+        url: URLTypes,
+        *,
+        data: RequestData = b"",
+        params: QueryParamTypes = None,
+        headers: HeaderTypes = None,
+        verify: VerifyTypes = None,
+        cert: CertTypes = None,
+        timeout: TimeoutTypes = None,
+    ) -> Response:
+        request = Request(method, url, data=data, params=params, headers=headers)
+        return self.send(request, verify=verify, cert=cert, timeout=timeout)
+
+    def send(
+        self,
+        request: Request,
+        verify: VerifyTypes = None,
+        cert: CertTypes = None,
+        timeout: TimeoutTypes = None,
+    ) -> Response:
+        raise NotImplementedError()  # pragma: nocover
+
+    def close(self) -> None:
+        pass  # pragma: nocover
+
+    def __enter__(self) -> "Dispatcher":
+        return self
+
+    def __exit__(
+        self,
+        exc_type: typing.Type[BaseException] = None,
+        exc_value: BaseException = None,
+        traceback: TracebackType = None,
+    ) -> None:
+        self.close()
index d82ea5db214a69075051a73f81fb3f90a49ef842..36f96f8065ee979b4043dc85a923f8c858a352b3 100644 (file)
@@ -2,7 +2,9 @@ import functools
 import ssl
 import typing
 
+from .base import AsyncDispatcher
 from ..concurrency.asyncio import AsyncioBackend
+from ..concurrency.base import ConcurrencyBackend
 from ..config import (
     DEFAULT_TIMEOUT_CONFIG,
     CertTypes,
@@ -13,7 +15,6 @@ from ..config import (
     TimeoutTypes,
     VerifyTypes,
 )
-from ..interfaces import AsyncDispatcher, ConcurrencyBackend
 from ..models import AsyncRequest, AsyncResponse, Origin
 from .http2 import HTTP2Connection
 from .http11 import HTTP11Connection
index b88d142c4c7a0bf7e30d93933b20cbec892235da..0e14bf8354a3db119243d24d2d4a07be862731d8 100644 (file)
@@ -1,6 +1,8 @@
 import typing
 
+from .base import AsyncDispatcher
 from ..concurrency.asyncio import AsyncioBackend
+from ..concurrency.base import ConcurrencyBackend
 from ..config import (
     DEFAULT_POOL_LIMITS,
     DEFAULT_TIMEOUT_CONFIG,
@@ -10,7 +12,6 @@ from ..config import (
     TimeoutTypes,
     VerifyTypes,
 )
-from ..interfaces import AsyncDispatcher, ConcurrencyBackend
 from ..models import AsyncRequest, AsyncResponse, Origin
 from .connection import HTTPConnection
 
index 46d7309cc393fa8c6a7437e56634765e60204a18..554591f8040b8f16fa91af1d2ad90000bb38eefc 100644 (file)
@@ -2,9 +2,8 @@ import typing
 
 import h11
 
-from ..concurrency.asyncio import TimeoutFlag
+from ..concurrency.base import BaseReader, BaseWriter, ConcurrencyBackend, TimeoutFlag
 from ..config import TimeoutConfig, TimeoutTypes
-from ..interfaces import BaseReader, BaseWriter, ConcurrencyBackend
 from ..models import AsyncRequest, AsyncResponse
 
 H11Event = typing.Union[
index 199ee8fa198d666a9dacef1aa6c6c452ebd4b8b4..bf258e3a7a4e54c52dd4ec0fbdf81eb2cf50c609 100644 (file)
@@ -4,9 +4,8 @@ import typing
 import h2.connection
 import h2.events
 
-from ..concurrency.asyncio import TimeoutFlag
+from ..concurrency.base import BaseReader, BaseWriter, ConcurrencyBackend, TimeoutFlag
 from ..config import TimeoutConfig, TimeoutTypes
-from ..interfaces import BaseReader, BaseWriter, ConcurrencyBackend
 from ..models import AsyncRequest, AsyncResponse
 
 
index 602001092d8ea90af52d21a15bdd1a864abfd751..8176608729493cb0d8fe7c1f77696bf8b0cda72e 100644 (file)
@@ -1,5 +1,6 @@
+from .base import AsyncDispatcher, Dispatcher
+from ..concurrency.base import ConcurrencyBackend
 from ..config import CertTypes, TimeoutTypes, VerifyTypes
-from ..interfaces import AsyncDispatcher, ConcurrencyBackend, Dispatcher
 from ..models import (
     AsyncRequest,
     AsyncRequestData,
index e1109c05a39fcfed2a8cc39fb6340e777ece9c4f..0cbe1095e2c510bcabb64c456ee0c649710097cd 100644 (file)
@@ -1,8 +1,8 @@
 import io
 import typing
 
+from .base import Dispatcher
 from ..config import CertTypes, TimeoutTypes, VerifyTypes
-from ..interfaces import Dispatcher
 from ..models import Request, Response