| 273 | |
| 274 | |
| 275 | class AsyncPaginator(Generic[_T, AsyncPageT]): |
| 276 | def __init__( |
| 277 | self, |
| 278 | client: AsyncAPIClient, |
| 279 | options: FinalRequestOptions, |
| 280 | page_cls: Type[AsyncPageT], |
| 281 | model: Type[_T], |
| 282 | ) -> None: |
| 283 | self._model = model |
| 284 | self._client = client |
| 285 | self._options = options |
| 286 | self._page_cls = page_cls |
| 287 | |
| 288 | def __await__(self) -> Generator[Any, None, AsyncPageT]: |
| 289 | return self._get_page().__await__() |
| 290 | |
| 291 | async def _get_page(self) -> AsyncPageT: |
| 292 | def _parser(resp: AsyncPageT) -> AsyncPageT: |
| 293 | resp._set_private_attributes( |
| 294 | model=self._model, |
| 295 | options=self._options, |
| 296 | client=self._client, |
| 297 | ) |
| 298 | return resp |
| 299 | |
| 300 | self._options.post_parser = _parser |
| 301 | |
| 302 | return await self._client.request(self._page_cls, self._options) |
| 303 | |
| 304 | async def __aiter__(self) -> AsyncIterator[_T]: |
| 305 | # https://github.com/microsoft/pyright/issues/3464 |
| 306 | page = cast( |
| 307 | AsyncPageT, |
| 308 | await self, # type: ignore |
| 309 | ) |
| 310 | async for item in page: |
| 311 | yield item |
| 312 | |
| 313 | |
| 314 | class BaseAsyncPage(BasePage[_T], Generic[_T]): |