googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.55k stars 2.37k forks source link

backupRuns().list does not generate pages correctly for large result sets #2403

Open ve4eslav opened 1 month ago

ve4eslav commented 1 month ago

When I try to get a list of created backups, I get different results. The number of records differs if they are returned all in one request and if they are divided into pages. When comparing the results, in the current example, the first record disappeared in each new window. But this behavior occurs if there are more than two windows.

Environment details

Steps to reproduce

Run the below code

Code example

from googleapiclient import discovery

project_id = "<project_id>"
sql_api = discovery.build("sqladmin", "v1", cache_discovery=False)
request = sql_api.backupRuns().list(project=project_id, instance="-", maxResults=100)
page = request.execute()
backups = page.get("items", [])
print(f"All in one transaction. Total records: {len(backups)}")
backups = []
next_page = {}
while True:
    request = sql_api.backupRuns().list(project=project_id, instance="-", maxResults=10, **next_page)
    page = request.execute()
    items = page.get("items", [])
    print(f"transaction {next_page}. records: {len(items)}")
    backups.extend(items)
    next_page = page.get("nextPageToken", 0)
    if next_page:
        next_page = {"pageToken": next_page}
    else:
        break
print(f"Multiple transactions. Total records: {len(backups)}")

Stack trace

All in one transaction. Total records: 49
transaction {}. records: 10
transaction {'pageToken': '1715713200000'}. records: 10
transaction {'pageToken': '1715547600000'}. records: 10
transaction {'pageToken': '1715374800000'}. records: 10
transaction {'pageToken': '1712929640988'}. records: 5
Multiple transactions. Total records: 45

for different maxResults I get different results. for 20 the result is

All in one transaction. Total records: 49
Multiple transactions. Total records: 48

for 25 the result is

All in one transaction. Total records: 49
Multiple transactions. Total records: 49

Thanks!