1
1
from abc import ABC , abstractmethod
2
- from collections import namedtuple
3
2
from typing import Generic
4
3
from sinch .core .types import BM
5
4
@@ -50,16 +49,6 @@ async def __anext__(self):
50
49
class Paginator (ABC , Generic [BM ]):
51
50
"""
52
51
Pagination response object.
53
-
54
- auto_paging_iter method returns an iterator object that can be used for iterator-based page traversing.
55
- For example:
56
- for page in paginated_response.auto_paging_iter():
57
- ...process page object
58
-
59
- For manual pagination use has_next_page property with next_page() method.
60
- For example:
61
- if paginated_response.has_next_page:
62
- paginated_response = paginated_response.next_page()
63
52
"""
64
53
def __init__ (self , sinch , endpoint , result : BM ):
65
54
self ._sinch = sinch
@@ -81,11 +70,6 @@ def content(self):
81
70
def iterator (self ):
82
71
pass
83
72
84
- # TODO: Make get_content() method abstract in Parent class as we implement in the other domains:
85
- # - Refactor pydantic models in other domains to have a content property.
86
- def get_content (self ):
87
- pass
88
-
89
73
@abstractmethod
90
74
def next_page (self ):
91
75
pass
@@ -145,7 +129,7 @@ async def _initialize(cls, sinch, endpoint):
145
129
class TokenBasedPaginator (Paginator [BM ]):
146
130
"""Base paginator for token-based pagination with explicit page navigation and metadata."""
147
131
148
- def __init__ (self , sinch , endpoint , yield_first_page = False , result = None ):
132
+ def __init__ (self , sinch , endpoint , result = None ):
149
133
super ().__init__ (sinch , endpoint , result or sinch .configuration .transport .request (endpoint ))
150
134
151
135
def content (self ) -> list [BM ]:
@@ -157,9 +141,9 @@ def next_page(self):
157
141
return None
158
142
159
143
self .endpoint .request_data .page_token = self .result .next_page_token
160
- next_result = self ._sinch .configuration .transport .request (self .endpoint )
161
-
162
- return self . __class__ ( self . _sinch , self . endpoint , result = next_result )
144
+ self . result = self ._sinch .configuration .transport .request (self .endpoint )
145
+ self . _calculate_next_page ()
146
+ return self
163
147
164
148
def iterator (self ):
165
149
"""Iterates over individual items across all pages."""
@@ -172,52 +156,14 @@ def iterator(self):
172
156
break
173
157
paginator = next_page_instance
174
158
175
- def get_content (self ):
176
- """Returns structured pagination metadata along with the first page's content (sync)."""
177
- next_page_instance = self .next_page ()
178
- return self ._get_content (next_page_instance , sync = True )
179
-
180
- def _get_content (self , next_page_instance , sync = True ):
181
- """Core logic for `get_content()`, shared between sync and async versions."""
182
- PagedListResponse = namedtuple (
183
- "PagedResponse" , ["result" , "has_next_page" , "next_page_info" , "next_page" ]
184
- )
185
-
186
- next_page_info = {
187
- "result" : self .content (),
188
- "result.next" : (
189
- self .content () + (next_page_instance .content () if next_page_instance else [])
190
- ),
191
- "has_next_page" : self .has_next_page ,
192
- "has_next_page.next" : bool (next_page_instance and next_page_instance .has_next_page ),
193
- }
194
-
195
- next_page_wrapper = self ._get_next_page_wrapper (next_page_instance , sync )
196
-
197
- return PagedListResponse (
198
- result = self .content (),
199
- has_next_page = self .has_next_page ,
200
- next_page_info = next_page_info ,
201
- next_page = next_page_wrapper
202
- )
203
-
204
- def _get_next_page_wrapper (self , next_page_instance , sync ):
205
- """Returns a function for fetching the next page."""
206
- if sync :
207
- return lambda : next_page_instance .get_content () if next_page_instance else None
208
- else :
209
- async def async_next_page_wrapper ():
210
- return await next_page_instance .get_content () if next_page_instance else None
211
- return async_next_page_wrapper
212
-
213
159
def _calculate_next_page (self ):
214
160
self .has_next_page = bool (getattr (self .result , "next_page_token" , None ))
215
161
216
162
@classmethod
217
163
def _initialize (cls , sinch , endpoint ):
218
164
"""Creates an instance of the paginator skipping first page."""
219
165
result = sinch .configuration .transport .request (endpoint )
220
- return cls (sinch , endpoint , yield_first_page = False , result = result )
166
+ return cls (sinch , endpoint , result = result )
221
167
222
168
223
169
class AsyncTokenBasedPaginator (TokenBasedPaginator ):
@@ -244,12 +190,7 @@ async def iterator(self):
244
190
break
245
191
paginator = next_page_instance
246
192
247
- async def get_content (self ):
248
- """Returns structured pagination metadata"""
249
- next_page_instance = await self .next_page ()
250
- return self ._get_content (next_page_instance , sync = False )
251
-
252
193
@classmethod
253
194
async def _initialize (cls , sinch , endpoint ):
254
195
result = await sinch .configuration .transport .request (endpoint )
255
- return cls (sinch , endpoint , yield_first_page = False , result = result )
196
+ return cls (sinch , endpoint , result = result )
0 commit comments