]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Use public API only from transports (#1096)
authorTom Christie <tom@tomchristie.com>
Fri, 31 Jul 2020 10:30:55 +0000 (11:30 +0100)
committerGitHub <noreply@github.com>
Fri, 31 Jul 2020 10:30:55 +0000 (11:30 +0100)
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
httpx/_transports/asgi.py
httpx/_transports/wsgi.py

index 465f49cef1165db5c31e74ef7c164ab6595c126d..051662946757b3f22bf56b5ff14d8c53f99c8b80 100644 (file)
@@ -1,10 +1,17 @@
-from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union
+from typing import (
+    TYPE_CHECKING,
+    AsyncIterator,
+    Callable,
+    Dict,
+    List,
+    Optional,
+    Tuple,
+    Union,
+)
 
 import httpcore
 import sniffio
 
-from .._content_streams import ByteStream
-
 if TYPE_CHECKING:  # pragma: no cover
     import asyncio
 
@@ -24,6 +31,10 @@ def create_event() -> "Event":
         return asyncio.Event()
 
 
+async def async_byte_iterator(bytestring: bytes) -> AsyncIterator[bytes]:
+    yield bytestring
+
+
 class ASGITransport(httpcore.AsyncHTTPTransport):
     """
     A custom AsyncTransport that handles sending requests directly to an ASGI app.
@@ -78,7 +89,11 @@ class ASGITransport(httpcore.AsyncHTTPTransport):
         timeout: Dict[str, Optional[float]] = None,
     ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:
         headers = [] if headers is None else headers
-        stream = ByteStream(b"") if stream is None else stream
+        stream = (
+            httpcore.AsyncByteStream(async_byte_iterator(b""))
+            if stream is None
+            else stream
+        )
 
         # ASGI scope.
         scheme, host, port, full_path = url
@@ -155,6 +170,8 @@ class ASGITransport(httpcore.AsyncHTTPTransport):
         assert status_code is not None
         assert response_headers is not None
 
-        stream = ByteStream(b"".join(body_parts))
+        response_body = b"".join(body_parts)
+
+        stream = httpcore.AsyncByteStream(async_byte_iterator(response_body))
 
         return (b"HTTP/1.1", status_code, b"", response_headers, stream)
index 3203caf4717c083fa4c616c95f5e4e9d1241875d..81d801d002955fdc29eac736ccd39a364668a720 100644 (file)
@@ -4,8 +4,6 @@ import typing
 
 import httpcore
 
-from .._content_streams import ByteStream, IteratorStream
-
 
 def _skip_leading_empty_chunks(body: typing.Iterable) -> typing.Iterable:
     body = iter(body)
@@ -75,7 +73,11 @@ class WSGITransport(httpcore.SyncHTTPTransport):
         httpcore.SyncByteStream,
     ]:
         headers = [] if headers is None else headers
-        stream = ByteStream(b"") if stream is None else stream
+        stream = (
+            httpcore.SyncByteStream(chunk for chunk in [b""])
+            if stream is None
+            else stream
+        )
 
         scheme, host, port, full_path = url
         path, _, query = full_path.partition(b"?")
@@ -128,6 +130,6 @@ class WSGITransport(httpcore.SyncHTTPTransport):
             (key.encode("ascii"), value.encode("ascii"))
             for key, value in seen_response_headers
         ]
-        stream = IteratorStream(chunk for chunk in result)
+        stream = httpcore.SyncByteStream(chunk for chunk in result)
 
         return (b"HTTP/1.1", status_code, b"", headers, stream)