Use public API only from transports (#1096)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 16893e4 · Browse files · 2 files·+28 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Diff stats
2 files+28 added−9 removed
- other:+28 added−9 removed
Provenance
- Commit
e1a7d5a- Tree
b5eef95- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
16893e4
2 files changed+28 added−9 removed
modified:httpx/_transports/asgi.py+22 added-5 removed
1-from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union1+from typing import (2+ TYPE_CHECKING,3+ AsyncIterator,4+ Callable,5+ Dict,6+ List,7+ Optional,8+ Tuple,9+ Union,10+)11 12 import httpcore13 import sniffio14 6-from .._content_streams import ByteStream7-15 if TYPE_CHECKING: # pragma: no cover16 import asyncio17 18 import trio11 unmodified lines30 31 return asyncio.Event()32 33 34+async def async_byte_iterator(bytestring: bytes) -> AsyncIterator[bytes]:35+ yield bytestring36+37+38 class ASGITransport(httpcore.AsyncHTTPTransport):39 """40 A custom AsyncTransport that handles sending requests directly to an ASGI app.41 The simplest way to use this functionality is to use the `app` argument.46 unmodified lines88 stream: httpcore.AsyncByteStream = None,89 timeout: Dict[str, Optional[float]] = None,90 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:91 headers = [] if headers is None else headers81- stream = ByteStream(b"") if stream is None else stream92+ stream = (93+ httpcore.AsyncByteStream(async_byte_iterator(b""))94+ if stream is None95+ else stream96+ )97 98 # ASGI scope.99 scheme, host, port, full_path = url100 path, _, query = full_path.partition(b"?")68 unmodified lines169 assert response_complete.is_set()170 assert status_code is not None171 assert response_headers is not None172 158- stream = ByteStream(b"".join(body_parts))173+ response_body = b"".join(body_parts)174 175+ stream = httpcore.AsyncByteStream(async_byte_iterator(response_body))176+177 return (b"HTTP/1.1", status_code, b"", response_headers, stream)modified:httpx/_transports/wsgi.py+6 added-4 removed
2 unmodified lines3 import typing4 5 import httpcore6 7-from .._content_streams import ByteStream, IteratorStream7 9-8 def _skip_leading_empty_chunks(body: typing.Iterable) -> typing.Iterable:9 body = iter(body)10 for chunk in body:11 if chunk:60 unmodified lines72 typing.List[typing.Tuple[bytes, bytes]],73 httpcore.SyncByteStream,74 ]:75 headers = [] if headers is None else headers78- stream = ByteStream(b"") if stream is None else stream76+ stream = (77+ httpcore.SyncByteStream(chunk for chunk in [b""])78+ if stream is None79+ else stream80+ )81 82 scheme, host, port, full_path = url83 path, _, query = full_path.partition(b"?")84 environ = {44 unmodified lines129 headers = [130 (key.encode("ascii"), value.encode("ascii"))131 for key, value in seen_response_headers132 ]131- stream = IteratorStream(chunk for chunk in result)133+ stream = httpcore.SyncByteStream(chunk for chunk in result)134 135 return (b"HTTP/1.1", status_code, b"", headers, stream)