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
Provenance
- Commit
b481166- Tree
7502358- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
838f417
1 file changed+17 added−13 removed
modified:httpx/_transports/asgi.py+17 added-13 removed
1-import typing2-from typing import Callable, Dict, List, Optional, Tuple1+from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union2 3 import httpcore4 import sniffio5 6 from .._content_streams import ByteStream7 from .._utils import warn_deprecated8 10-if typing.TYPE_CHECKING: # pragma: no cover9+if TYPE_CHECKING: # pragma: no cover10 import asyncio11 import trio12 14- Event = typing.Union[asyncio.Event, trio.Event]13+ Event = Union[asyncio.Event, trio.Event]14 15 16 def create_event() -> "Event":17 if sniffio.current_async_library() == "trio":58 unmodified lines76 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 stream82+83+ # ASGI scope.84 scheme, host, port, full_path = url85 path, _, query = full_path.partition(b"?")86 scope = {87 "type": "http",7 unmodified lines95 "server": (host.decode("ascii"), port),96 "client": self.client,97 "root_path": self.root_path,98 }99+100+ # Request.101+ request_body_chunks = stream.__aiter__()102+ request_complete = False103+104+ # Response.105 status_code = None106 response_headers = None107 body_parts = []99- request_complete = False108 response_started = False109 response_complete = create_event()110 103- headers = [] if headers is None else headers104- stream = ByteStream(b"") if stream is None else stream111+ # ASGI callables.112 106- request_body_chunks = stream.__aiter__()107-113 async def receive() -> dict:109- nonlocal request_complete, response_complete114+ nonlocal request_complete115 116 if request_complete:117 await response_complete.wait()118 return {"type": "http.disconnect"}5 unmodified lines124 return {"type": "http.request", "body": b"", "more_body": False}125 return {"type": "http.request", "body": body, "more_body": True}126 127 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_started129 130 if message["type"] == "http.response.start":131 assert not response_started132 14 unmodified lines147 148 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 raise153 154 assert response_complete.is_set()155 assert status_code is not None22 unmodified lines