Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/writerai/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
from typing import Any, List, Generic, TypeVar, Optional, cast
from typing_extensions import Protocol, override, runtime_checkable

from pydantic import Field as FieldInfo

from ._models import BaseModel
from ._base_client import BasePage, PageInfo, BaseSyncPage, BaseAsyncPage

__all__ = ["SyncCursorPage", "AsyncCursorPage", "SyncApplicationJobsOffset", "AsyncApplicationJobsOffset"]
__all__ = [
"SyncCursorPage",
"AsyncCursorPage",
"ApplicationJobsOffsetPagination",
"SyncApplicationJobsOffset",
"AsyncApplicationJobsOffset",
]

_T = TypeVar("_T")

Expand Down Expand Up @@ -85,8 +94,16 @@ def next_page_info(self) -> Optional[PageInfo]:
return PageInfo(params={"before": item.id})


class ApplicationJobsOffsetPagination(BaseModel):
limit: Optional[int] = None

offset: Optional[int] = None


class SyncApplicationJobsOffset(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
result: List[_T]
total_count: Optional[int] = FieldInfo(alias="totalCount", default=None)
pagination: Optional[ApplicationJobsOffsetPagination] = None

@override
def _get_page_items(self) -> List[_T]:
Expand All @@ -104,11 +121,20 @@ def next_page_info(self) -> Optional[PageInfo]:
length = len(self._get_page_items())
current_count = offset + length

return PageInfo(params={"offset": current_count})
total_count = self.total_count
if total_count is None:
return None

if current_count < total_count:
return PageInfo(params={"offset": current_count})

return None


class AsyncApplicationJobsOffset(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
result: List[_T]
total_count: Optional[int] = FieldInfo(alias="totalCount", default=None)
pagination: Optional[ApplicationJobsOffsetPagination] = None

@override
def _get_page_items(self) -> List[_T]:
Expand All @@ -126,4 +152,11 @@ def next_page_info(self) -> Optional[PageInfo]:
length = len(self._get_page_items())
current_count = offset + length

return PageInfo(params={"offset": current_count})
total_count = self.total_count
if total_count is None:
return None

if current_count < total_count:
return PageInfo(params={"offset": current_count})

return None