|
1 | 1 | # File generated from our OpenAPI spec by Stainless.
|
2 | 2 |
|
3 |
| -from typing import List, Generic, TypeVar, Optional, cast |
| 3 | +from typing import Any, List, Type, Generic, Mapping, TypeVar, Optional, cast |
4 | 4 | from typing_extensions import override
|
5 | 5 |
|
| 6 | +from httpx import Response |
| 7 | + |
| 8 | +from ._utils import is_mapping |
6 | 9 | from ._models import BaseModel, GenericModel
|
7 | 10 | from ._base_client import BasePage, PageInfo, BaseSyncPage, BaseAsyncPage
|
8 | 11 |
|
|
20 | 23 | "CursorLimitPaginationResultInfo",
|
21 | 24 | "SyncCursorLimitPagination",
|
22 | 25 | "AsyncCursorLimitPagination",
|
| 26 | + "SyncSinglePage", |
| 27 | + "AsyncSinglePage", |
23 | 28 | ]
|
24 | 29 |
|
| 30 | +_BaseModelT = TypeVar("_BaseModelT", bound=BaseModel) |
| 31 | + |
25 | 32 | _T = TypeVar("_T")
|
26 | 33 |
|
27 | 34 |
|
@@ -247,3 +254,59 @@ def next_page_info(self) -> Optional[PageInfo]:
|
247 | 254 | return None
|
248 | 255 |
|
249 | 256 | return PageInfo(params={"cursor": cursor})
|
| 257 | + |
| 258 | + |
| 259 | +class SyncSinglePage(BaseSyncPage[_T], BasePage[_T], Generic[_T]): |
| 260 | + items: List[_T] |
| 261 | + |
| 262 | + @override |
| 263 | + def _get_page_items(self) -> List[_T]: |
| 264 | + items = self.items |
| 265 | + if not items: |
| 266 | + return [] |
| 267 | + return items |
| 268 | + |
| 269 | + @override |
| 270 | + def next_page_info(self) -> None: |
| 271 | + """ |
| 272 | + This page represents a response that isn't actually paginated at the API level |
| 273 | + so there will never be a next page. |
| 274 | + """ |
| 275 | + return None |
| 276 | + |
| 277 | + @classmethod |
| 278 | + def build(cls: Type[_BaseModelT], *, response: Response, data: object) -> _BaseModelT: # noqa: ARG003 |
| 279 | + return cls.construct( |
| 280 | + None, |
| 281 | + **{ |
| 282 | + **(cast(Mapping[str, Any], data) if is_mapping(data) else {"items": data}), |
| 283 | + }, |
| 284 | + ) |
| 285 | + |
| 286 | + |
| 287 | +class AsyncSinglePage(BaseAsyncPage[_T], BasePage[_T], Generic[_T]): |
| 288 | + items: List[_T] |
| 289 | + |
| 290 | + @override |
| 291 | + def _get_page_items(self) -> List[_T]: |
| 292 | + items = self.items |
| 293 | + if not items: |
| 294 | + return [] |
| 295 | + return items |
| 296 | + |
| 297 | + @override |
| 298 | + def next_page_info(self) -> None: |
| 299 | + """ |
| 300 | + This page represents a response that isn't actually paginated at the API level |
| 301 | + so there will never be a next page. |
| 302 | + """ |
| 303 | + return None |
| 304 | + |
| 305 | + @classmethod |
| 306 | + def build(cls: Type[_BaseModelT], *, response: Response, data: object) -> _BaseModelT: # noqa: ARG003 |
| 307 | + return cls.construct( |
| 308 | + None, |
| 309 | + **{ |
| 310 | + **(cast(Mapping[str, Any], data) if is_mapping(data) else {"items": data}), |
| 311 | + }, |
| 312 | + ) |
0 commit comments