diff --git a/arkindex/pagination.py b/arkindex/pagination.py index 0b5c2d18a3b3dec703effe3aec40760655a06392..31ecfd8bbb2de4791e6d48085b69b4cc2ed223f9 100644 --- a/arkindex/pagination.py +++ b/arkindex/pagination.py @@ -48,6 +48,11 @@ class ResponsePaginator(Sized, Iterator): if self.data and self.data.get('next') is None: raise StopIteration self._fetch_page(self.current_page + 1) + + # Even after fetching a new page, if the new page is empty, just fail + if len(self.results) < 1: + raise StopIteration + return self.results.pop(0) def __len__(self): diff --git a/arkindex/tests/test_pagination.py b/arkindex/tests/test_pagination.py new file mode 100644 index 0000000000000000000000000000000000000000..621695be458e650b6827fe5da9b49407255d0cee --- /dev/null +++ b/arkindex/tests/test_pagination.py @@ -0,0 +1,16 @@ +import pytest +import responses +from arkindex import ArkindexClient + + +@responses.activate +def test_pagination_empty(): + responses.add( + responses.GET, + 'https://dummy.test/api/v1/elements/', + json={'count': 0, 'previous': None, 'next': None, 'results': []}, + ) + + cli = ArkindexClient('t0k3n', base_url='https://dummy.test') + with pytest.raises(StopIteration): + next(cli.paginate('ListElements'))