Skip to content

Commit 54473f0

Browse files
committed
remove cursor when end of result set reached
1 parent ca11798 commit 54473f0

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

server/app/interfaces/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(self, *args, content_type="application/json", **kwargs):
148148
super().__init__(*args, **kwargs, content_type=content_type)
149149

150150
def serialize(self, obj: ResponseData, cursor: Optional[int], stripped: bool) -> str:
151-
if cursor is None or (isinstance(obj, list) and not obj):
151+
if cursor is None:
152152
data = obj
153153
else:
154154
data = {
@@ -273,8 +273,11 @@ def _get_slice(cls, request: Request, iterator: Iterable[T]) -> Tuple[Iterator[T
273273
raise BadRequest("Limit can not be negative, cursor must be positive!")
274274
start_index = cursor
275275
end_index = cursor + limit
276-
paginated_slice = itertools.islice(iterator, start_index, end_index)
277-
return paginated_slice, end_index
276+
items = list(itertools.islice(iterator, start_index, end_index + 1))
277+
has_more = len(items) > limit
278+
paginated_slice = iter(items[:limit])
279+
next_cursor = cursor + limit if has_more else None
280+
return paginated_slice, next_cursor
278281

279282
def handle_request(self, request: Request):
280283
map_adapter: MapAdapter = self.url_map.bind_to_environ(request.environ)

0 commit comments

Comments
 (0)