aboutsummaryrefslogtreecommitdiff
path: root/docs/pagination.md
blob: b34ede9b254ef92a7c100c569a1d26f3102c31fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Pagination

Some API methods may return very large lists of data. To reduce the response size, many of these API methods support pagination. With paginated results, your application can iteratively request and process large lists one page at a time. For API methods that support it, there exist similarly named methods with a "_next" suffix. For example, if a method is named `list()`, there may also be a method named `list_next()`. These methods can be found in the API's PyDoc documentation on the [Supported APIs page](https://developers.google.com/api-client-library/python/apis/).

To process the first page of results, create a request object and call `execute()` as you normally would. For further pages, you call the corresponding `method_name_next()` method, and pass it the previous request and response. Continue paging until `method_name_next()` returns None.

In the following code snippet, the paginated results of a Google Plus activities `list()` method are processed:

```py
activities = service.activities()
request = activities.list(userId='someUserId', collection='public')

while request is not None:
  activities_doc = request.execute(http=http)

  # Do something with the activities

  request = activities.list_next(request, activities_doc)
```

Note that you only call `execute()` on the request once inside the while loop.