Reputation: 521
It first time to use 'nextPageToken' for paginating.
I split pages like this↓
pages = Math.ceil(data size / 100)
Official docs: https://developers.google.com/people/api/rest/v1/people.connections/list?hl=ko#google.people.v1.PeopleService.ListConnections
But in docs, explaination about nextPageToken says "A token, which can be sent as pageToken to retrieve the next page. If this field is omitted, there are no subsequent pages."
I think it means, nextPageToken only retrieve one next result.
If I made API that calls 100 datas at once and customer click '3rd page' button(she was at first page), maybe response is 2nd page's result. right?
Then, people only can use this API with infinite scroll way not pagination way, am I right???
Upvotes: 2
Views: 2462
Reputation: 3725
The cursor pagination used by Google makes it tricky to implement page skipping, but I believe you can still work out a method to implement pagination even with the page tokens.
First, the people.connections.list
method allows you to set your own pageSize
and also returns a totalItems
field which has the total results, so for a given pageSize
you can calculate the amount of pages with your formula, pages = Math.ceil(totalItems/pageSize)
.
Next, the API accepts a fields
parameter, which decides which fields are returned. You can specify nextPageToken
so you only get the token. That way if a user requests page n
, you can go page by page requesting only the token until you reach page n
to avoid querying unnecessary data and get the actual results on the last call only. You'll have to store these tokens at least temporarily and correlate them with their page number in case that the user wants to jump back to a previous page.
A possible alternative could be to store the tokens. One way to perform the cursor pagination is to reference a unique index for the position of the next result in the database combined with the page size. I don't know Google's exact implementation, but I've tested and the nextPageToken
with a pageSize
of n
is the same even if you make the API calls from different accounts.
For example, the people.connections.list
method you referenced seems to always use the token GgYKAggBEAI
for page 2 when you set a pageSize
of 1. This could change depending on certain parameters and for different API endpoints, so it would require testing on your part to make sure that you're storing the correct tokens. Other users have even tried to figure out the obfuscation algorithm of the YouTube API and have also compiled lists of tokens, so it is possible, but do note that this may change over time so you should have a fallback in case the tokens fail. Also note that the YouTube tokens are different from the People API.
According to Google's own best practices, you should expect these tokens to be consistent as long as their back-end implementations don't change, so storing the tokens seems viable, but it cannot be guaranteed forever. If you really need to skip pages you could consider these methods, although right now infinite scrolls seem to be the recommended method.
Upvotes: 4