Rejig response streaming/closing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Tom Christie · parent ee6f42a · Browse files · 4 files·+16 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+16 added−18 removed
- other:+16 added−18 removed
Provenance
- Commit
63d73ad- Tree
a91717c- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
ee6f42a
4 files changed+16 added−18 removed
modified:httpcore/connections.py+5 added-16 removed
38 unmodified lines39 )40 except asyncio.TimeoutError:41 raise ConnectTimeout()42 43- async def send(self, request: Request, stream: bool = False) -> Response:43+ async def send(self, request: Request) -> Response:44 method = request.method.encode()45 target = request.url.target46 headers = request.headers47 20 unmodified lines68 event = await self._receive_event()69 assert isinstance(event, h11.Response)70 status_code = event.status_code71 headers = event.headers72+ body = self.body_iter()73+ return Response(74+ status_code=status_code, headers=headers, body=body, on_close=self.close75+ )76 73- if stream:74- body_iter = self.body_iter()75- return Response(status_code=status_code, headers=headers, body=body_iter)76-77- # Get the response body.78- body = b""79- event = await self._receive_event()80- while isinstance(event, h11.Data):81- body += event.data82- event = await self._receive_event()83- assert isinstance(event, h11.EndOfMessage)84- await self.close()85-86- return Response(status_code=status_code, headers=headers, body=body)87-77 async def body_iter(self) -> typing.AsyncIterator[bytes]:78 event = await self._receive_event()79 while isinstance(event, h11.Data):80 yield event.data32 unmodified linesmodified:httpcore/datastructures.py+4 added
188 unmodified lines189 yield part190 await self.close()191 192 async def close(self) -> None:193+ """194+ Close the response and release the connection.195+ Automatically called if the response body is read to completion.196+ """197 if not self.is_closed:198 self.is_closed = True199 if self.on_close is not None:200 await self.on_close()modified:httpcore/pool.py+6 added-1 removed
41 unmodified lines42 parsed_url = URL(url)43 request = Request(method, parsed_url, headers=headers, body=body)44 ssl_context = await self.get_ssl_context(parsed_url)45 connection = await self.acquire_connection(parsed_url, ssl=ssl_context)46- response = await connection.send(request, stream=stream)46+ response = await connection.send(request)47+ if not stream:48+ try:49+ await response.read()50+ finally:51+ await response.close()52 return response53 54 async def acquire_connection(55 self, url: URL, *, ssl: typing.Union[bool, ssl.SSLContext] = False76 unmodified linesmodified:setup.py+1 added-1 removed
45 unmodified lines46 author="Tom Christie",47 author_email="tom@tomchristie.com",48 packages=get_packages("httpcore"),49 data_files=[("", ["LICENSE.md"])],50- install_requires=["h11"],50+ install_requires=["h11", "certifi"],51 classifiers=[52 "Development Status :: 3 - Alpha",53 "Environment :: Web Environment",54 "Intended Audience :: Developers",9 unmodified lines