Skip to content
Myr

Refactor ASGITransport.request() (#1021)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent 838f417 · Browse files · 1 file+17 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+17 added−13 removed

  • other:+17 added−13 removed

1 file+17 added−13 removed

modified:httpx/_transports/asgi.py+17 added-13 removed
1import typing2from typing import Callable, Dict, List, Optional, Tuple1from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union23import httpcore4import sniffio56from .._content_streams import ByteStream7from .._utils import warn_deprecated810if typing.TYPE_CHECKING: # pragma: no cover9if TYPE_CHECKING: # pragma: no cover10 import asyncio11 import trio1214 Event = typing.Union[asyncio.Event, trio.Event]13 Event = Union[asyncio.Event, trio.Event]141516def create_event() -> "Event":17 if sniffio.current_async_library() == "trio":
58 unmodified lines
76 headers: List[Tuple[bytes, bytes]] = None,77 stream: httpcore.AsyncByteStream = None,78 timeout: Dict[str, Optional[float]] = None,79 ) -> Tuple[bytes, int, bytes, List[Tuple[bytes, bytes]], httpcore.AsyncByteStream]:80 headers = [] if headers is None else headers81 stream = ByteStream(b"") if stream is None else stream8283 # ASGI scope.84 scheme, host, port, full_path = url85 path, _, query = full_path.partition(b"?")86 scope = {87 "type": "http",
7 unmodified lines
95 "server": (host.decode("ascii"), port),96 "client": self.client,97 "root_path": self.root_path,98 }99100 # Request.101 request_body_chunks = stream.__aiter__()102 request_complete = False103104 # Response.105 status_code = None106 response_headers = None107 body_parts = []99 request_complete = False108 response_started = False109 response_complete = create_event()110103 headers = [] if headers is None else headers104 stream = ByteStream(b"") if stream is None else stream111 # ASGI callables.112106 request_body_chunks = stream.__aiter__()107113 async def receive() -> dict:109 nonlocal request_complete, response_complete114 nonlocal request_complete115116 if request_complete:117 await response_complete.wait()118 return {"type": "http.disconnect"}
5 unmodified lines
124 return {"type": "http.request", "body": b"", "more_body": False}125 return {"type": "http.request", "body": body, "more_body": True}126127 async def send(message: dict) -> None:123 nonlocal status_code, response_headers, body_parts124 nonlocal response_started, response_complete128 nonlocal status_code, response_headers, response_started129130 if message["type"] == "http.response.start":131 assert not response_started132
14 unmodified lines
147148 try:149 await self.app(scope, receive, send)150 except Exception:147 if self.raise_app_exceptions or not response_complete:151 if self.raise_app_exceptions or not response_complete.is_set():152 raise153154 assert response_complete.is_set()155 assert status_code is not None
22 unmodified lines