1
1
from abc import ABC , abstractmethod
2
2
from collections import namedtuple
3
3
from typing import Generic
4
- from sinch .domains . numbers . models . numbers import BM
4
+ from sinch .core . types import BM
5
5
6
6
7
7
class PageIterator :
8
8
def __init__ (self , paginator , yield_first_page = False ):
9
9
self .paginator = paginator
10
- self .yield_first_page = yield_first_page
11
10
# If yielding the first page, set started to False
12
11
self .started = not yield_first_page
13
12
@@ -27,25 +26,25 @@ def __next__(self):
27
26
28
27
29
28
class AsyncPageIterator :
30
- def __init__ (self , paginator ):
29
+ def __init__ (self , paginator , yield_first_page = False ):
31
30
self .paginator = paginator
32
- self .first_yield = True
31
+ self .started = not yield_first_page
33
32
34
33
def __aiter__ (self ):
35
34
return self
36
35
37
36
async def __anext__ (self ):
38
- if self .first_yield :
39
- self .first_yield = False
37
+ if not self .started :
38
+ self .started = True
40
39
return self .paginator
41
40
42
41
if self .paginator .has_next_page :
43
42
next_paginator = await self .paginator .next_page ()
44
43
if next_paginator :
45
44
self .paginator = next_paginator
46
45
return self .paginator
47
-
48
- raise StopAsyncIteration
46
+ else :
47
+ raise StopAsyncIteration
49
48
50
49
51
50
class Paginator (ABC , Generic [BM ]):
@@ -82,8 +81,9 @@ def content(self):
82
81
def iterator (self ):
83
82
pass
84
83
85
- @abstractmethod
86
- def auto_paging_iter (self ):
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
87
pass
88
88
89
89
@abstractmethod
@@ -134,7 +134,7 @@ async def next_page(self):
134
134
return self
135
135
136
136
def auto_paging_iter (self ):
137
- return AsyncPageIterator (self )
137
+ return AsyncPageIterator (self , yield_first_page = True )
138
138
139
139
@classmethod
140
140
async def _initialize (cls , sinch , endpoint ):
@@ -147,7 +147,6 @@ class TokenBasedPaginator(Paginator[BM]):
147
147
148
148
def __init__ (self , sinch , endpoint , yield_first_page = False , result = None ):
149
149
super ().__init__ (sinch , endpoint , result or sinch .configuration .transport .request (endpoint ))
150
- self .yield_first_page = yield_first_page
151
150
152
151
def content (self ) -> list [BM ]:
153
152
return getattr (self .result , "content" , [])
@@ -162,10 +161,6 @@ def next_page(self):
162
161
163
162
return self .__class__ (self ._sinch , self .endpoint , result = next_result )
164
163
165
- def auto_paging_iter (self ):
166
- """Returns an iterator for automatic pagination."""
167
- return PageIterator (self , yield_first_page = True )
168
-
169
164
def iterator (self ):
170
165
"""Iterates over individual items across all pages."""
171
166
paginator = self
@@ -177,13 +172,13 @@ def iterator(self):
177
172
break
178
173
paginator = next_page_instance
179
174
180
- def list (self ):
175
+ def get_content (self ):
181
176
"""Returns structured pagination metadata along with the first page's content (sync)."""
182
177
next_page_instance = self .next_page ()
183
- return self ._list (next_page_instance , sync = True )
178
+ return self ._get_content (next_page_instance , sync = True )
184
179
185
- def _list (self , next_page_instance , sync = True ):
186
- """Core logic for `list ()`, shared between sync and async versions."""
180
+ def _get_content (self , next_page_instance , sync = True ):
181
+ """Core logic for `get_content ()`, shared between sync and async versions."""
187
182
PagedListResponse = namedtuple (
188
183
"PagedResponse" , ["result" , "has_next_page" , "next_page_info" , "next_page" ]
189
184
)
@@ -209,10 +204,10 @@ def _list(self, next_page_instance, sync=True):
209
204
def _get_next_page_wrapper (self , next_page_instance , sync ):
210
205
"""Returns a function for fetching the next page."""
211
206
if sync :
212
- return lambda : next_page_instance .list () if next_page_instance else None
207
+ return lambda : next_page_instance .get_content () if next_page_instance else None
213
208
else :
214
209
async def async_next_page_wrapper ():
215
- return await next_page_instance .list () if next_page_instance else None
210
+ return await next_page_instance .get_content () if next_page_instance else None
216
211
return async_next_page_wrapper
217
212
218
213
def _calculate_next_page (self ):
@@ -235,20 +230,7 @@ async def next_page(self):
235
230
self .endpoint .request_data .page_token = self .result .next_page_token
236
231
next_result = await self ._sinch .configuration .transport .request (self .endpoint )
237
232
238
- return AsyncTokenBasedPaginator (self ._sinch , self .endpoint , result = next_result )
239
-
240
- def auto_paging_iter (self ):
241
- return AsyncPageIterator (self )
242
-
243
- @classmethod
244
- async def _initialize (cls , sinch , endpoint ):
245
- result = await sinch .configuration .transport .request (endpoint )
246
- return cls (sinch , endpoint , result = result )
247
-
248
- async def list (self ):
249
- """Returns structured pagination metadata"""
250
- next_page_instance = await self .next_page ()
251
- return self ._list (next_page_instance , sync = False )
233
+ return self .__class__ (self ._sinch , self .endpoint , result = next_result )
252
234
253
235
async def iterator (self ):
254
236
"""Iterates asynchronously over individual items across all pages."""
@@ -261,3 +243,13 @@ async def iterator(self):
261
243
if not next_page_instance :
262
244
break
263
245
paginator = next_page_instance
246
+
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
+ @classmethod
253
+ async def _initialize (cls , sinch , endpoint ):
254
+ result = await sinch .configuration .transport .request (endpoint )
255
+ return cls (sinch , endpoint , yield_first_page = False , result = result )
0 commit comments