MCPcopy
hub / github.com/encode/httpx / handle_async_request

Method handle_async_request

httpx/_transports/asgi.py:99–187  ·  view source on GitHub ↗
(
        self,
        request: Request,
    )

Source from the content-addressed store, hash-verified

97 self.client = client
98
99 async def handle_async_request(
100 self,
101 request: Request,
102 ) -> Response:
103 assert isinstance(request.stream, AsyncByteStream)
104
105 # ASGI scope.
106 scope = {
107 "type": "http",
108 "asgi": {"version": "3.0"},
109 "http_version": "1.1",
110 "method": request.method,
111 "headers": [(k.lower(), v) for (k, v) in request.headers.raw],
112 "scheme": request.url.scheme,
113 "path": request.url.path,
114 "raw_path": request.url.raw_path.split(b"?")[0],
115 "query_string": request.url.query,
116 "server": (request.url.host, request.url.port),
117 "client": self.client,
118 "root_path": self.root_path,
119 }
120
121 # Request.
122 request_body_chunks = request.stream.__aiter__()
123 request_complete = False
124
125 # Response.
126 status_code = None
127 response_headers = None
128 body_parts = []
129 response_started = False
130 response_complete = create_event()
131
132 # ASGI callables.
133
134 async def receive() -> dict[str, typing.Any]:
135 nonlocal request_complete
136
137 if request_complete:
138 await response_complete.wait()
139 return {"type": "http.disconnect"}
140
141 try:
142 body = await request_body_chunks.__anext__()
143 except StopAsyncIteration:
144 request_complete = True
145 return {"type": "http.request", "body": b"", "more_body": False}
146 return {"type": "http.request", "body": body, "more_body": True}
147
148 async def send(message: typing.MutableMapping[str, typing.Any]) -> None:
149 nonlocal status_code, response_headers, response_started
150
151 if message["type"] == "http.response.start":
152 assert not response_started
153
154 status_code = message["status"]
155 response_headers = message.get("headers", [])
156 response_started = True

Callers

nothing calls this directly

Calls 5

create_eventFunction · 0.85
ASGIResponseStreamClass · 0.85
ResponseClass · 0.85
__aiter__Method · 0.45
setMethod · 0.45

Tested by

no test coverage detected