An async byte stream that is bound to a given response instance, and that ensures the `response.elapsed` is set once the response is closed.
| 160 | |
| 161 | |
| 162 | class BoundAsyncStream(AsyncByteStream): |
| 163 | """ |
| 164 | An async byte stream that is bound to a given response instance, and that |
| 165 | ensures the `response.elapsed` is set once the response is closed. |
| 166 | """ |
| 167 | |
| 168 | def __init__( |
| 169 | self, stream: AsyncByteStream, response: Response, start: float |
| 170 | ) -> None: |
| 171 | self._stream = stream |
| 172 | self._response = response |
| 173 | self._start = start |
| 174 | |
| 175 | async def __aiter__(self) -> typing.AsyncIterator[bytes]: |
| 176 | async for chunk in self._stream: |
| 177 | yield chunk |
| 178 | |
| 179 | async def aclose(self) -> None: |
| 180 | elapsed = time.perf_counter() - self._start |
| 181 | self._response.elapsed = datetime.timedelta(seconds=elapsed) |
| 182 | await self._stream.aclose() |
| 183 | |
| 184 | |
| 185 | EventHook = typing.Callable[..., typing.Any] |
no outgoing calls
no test coverage detected