Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

docs(samples): Adding pagination sample. #78

Merged
merged 22 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a27aff8
feat: Adding pagination sample.
m-strzelczyk Jul 5, 2021
85ec4a2
Merge branch 'master' into compute-pagination
m-strzelczyk Jul 19, 2021
4ff53a8
feat: Adding regions and tests to pagination samples
m-strzelczyk Jul 19, 2021
c7fe346
chore: lint fix
m-strzelczyk Jul 19, 2021
531a17e
Merge branch 'master' into compute-pagination
parthea Jul 21, 2021
0693ec2
Apply suggestions from code review
m-strzelczyk Jul 22, 2021
6207353
chore: applying review comments.
m-strzelczyk Jul 22, 2021
d60a00c
fix: Make tests run in separate projects.
m-strzelczyk Jul 22, 2021
98ade2c
Revert "fix: Make tests run in separate projects."
m-strzelczyk Jul 22, 2021
6665ace
Merge branch 'master' into compute-pagination
m-strzelczyk Jul 22, 2021
856f244
trying to figure out the noxfile_config location
m-strzelczyk Jul 23, 2021
aed9059
Revert "trying to figure out the noxfile_config location"
m-strzelczyk Jul 23, 2021
e2a3fb8
Testing nox stuff.
m-strzelczyk Jul 23, 2021
c26385d
Revert "Testing nox stuff."
m-strzelczyk Jul 23, 2021
7185344
Merge branch 'master' into compute-pagination
m-strzelczyk Jul 26, 2021
5eef04a
Merge branch 'master' into compute-pagination
m-strzelczyk Jul 30, 2021
55d229b
chore: Making the default_values tests be more stable.
m-strzelczyk Aug 2, 2021
6d67628
Merge branch 'compute-pagination' of github.com:googleapis/python-com…
m-strzelczyk Aug 2, 2021
33d998c
Merge branch 'master' into compute-pagination
m-strzelczyk Aug 4, 2021
11a4261
Merge branch 'master' into compute-pagination
m-strzelczyk Aug 6, 2021
16f39a6
Apply suggestions from code review
m-strzelczyk Aug 9, 2021
8b9a8e1
Merge branch 'master' into compute-pagination
m-strzelczyk Aug 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion samples/snippets/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ def list_all_instances(
iterable collections of Instance objects as values.
"""
instance_client = compute_v1.InstancesClient()
FrodoTheTrue marked this conversation as resolved.
Show resolved Hide resolved
agg_list = instance_client.aggregated_list(project=project_id)
# Use the `max_results` parameter to limit the number of results that the API returns per response page.
request = compute_v1.AggregatedListInstancesRequest(project=project_id, max_results=5)
agg_list = instance_client.aggregated_list(request=request)
all_instances = {}
print("Instances found:")
# Despite using the `max_results` parameter, you don't need to handle the pagination
# yourself. The returned `AggregatedListPager` object handles pagination
# automatically, returning separated pages as you iterate over the results.
for zone, response in agg_list:
if response.instances:
all_instances[zone] = response.instances
Expand Down
80 changes: 80 additions & 0 deletions samples/snippets/sample_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START compute_images_list_page ]
# [START compute_images_list ]
import google.cloud.compute_v1 as compute_v1
# [END compute_images_list ]
# [END compute_images_list_page ]


# [START compute_images_list ]
def print_images_list(project: str) -> None:
"""
Prints a list of all non-deprecated image names available in given project.
m-strzelczyk marked this conversation as resolved.
Show resolved Hide resolved

Args:
project: project ID or project number of the Cloud project you want to list images from.

Returns:
None.
"""
images_client = compute_v1.ImagesClient()
# Listing only non-deprecated images to reduce the size of the reply.
images_list_request = compute_v1.ListImagesRequest(project=project, max_results=3,
filter="deprecated.state != DEPRECATED")

# Although the `max_results` parameter is specified in the request, the iterable returned
# by the `list()` method hides the pagination mechanic. The library makes multiple
# requests to the API for you, so you can simply iterate over all the images.
for img in images_client.list(request=images_list_request):
print(f" - {img.name}")
# [END compute_images_list ]


# [START compute_images_list_page ]
def print_images_list_by_page(project: str, page_size: int = 10) -> None:
"""
Prints a list of all non-deprecated image names available in a given project,
divided into pages as returned by the Compute Engine API.

Args:
project: project ID or project number of the Cloud project you want to list images from.
page_size: size of the pages you want the API to return on each call.

Returns:
None.
"""
images_client = compute_v1.ImagesClient()
# Listing only non-deprecated images to reduce the size of the reply.
images_list_request = compute_v1.ListImagesRequest(project=project, max_results=page_size,
filter="deprecated.state != DEPRECATED")

# Use the `pages` attribute of returned iterable to have more granular control of
# iteration over paginated results from the API. Each time you want to access the
# next page, the library retrieves that page from the API.
for page_num, page in enumerate(images_client.list(request=images_list_request).pages, start=1):
print(f"Page {page_num}: ")
for img in page.items:
print(f" - {img.name}")
# [END compute_images_list_page ]


if __name__ == '__main__':
print("=================== Flat list of images ===================")
print_images_list('windows-sql-cloud')
print("================= Paginated list of images ================")
print_images_list_by_page('windows-sql-cloud', 5)
30 changes: 30 additions & 0 deletions samples/snippets/test_sample_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing

from sample_pagination import print_images_list, print_images_list_by_page

PROJECT = 'windows-sql-cloud'


def test_pagination(capsys: typing.Any) -> None:
print_images_list(PROJECT)
out, _ = capsys.readouterr()
FrodoTheTrue marked this conversation as resolved.
Show resolved Hide resolved
assert(len(out.splitlines()) > 2)


def test_pagination_page(capsys: typing.Any) -> None:
print_images_list_by_page(PROJECT, 2)
out, _ = capsys.readouterr()
assert("Page 2" in out)