Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update paging implementation to handle unconventional pagination #750

Merged
merged 8 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
Empty file.
14 changes: 10 additions & 4 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,13 +866,19 @@ def paged_result_field(self) -> Optional[Field]:
"""Return the response pagination field if the method is paginated."""
# If the request field lacks any of the expected pagination fields,
# then the method is not paginated.
for page_field in ((self.input, int, 'page_size'),
(self.input, str, 'page_token'),
for page_field_token in ((self.input, str, 'page_token'),
(self.output, str, 'next_page_token')):
field = page_field[0].fields.get(page_field[2], None)
if not field or field.type != page_field[1]:
field = page_field_token[0].fields.get(page_field_token[2], None)
if not field or field.type != page_field_token[1]:
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great extension. Since we're changing things here, can we tweak the loop so that we're destructuring into names instead of indexing the tuple?

for source, name in ((self.input, "page_token"), (self.output, "next_page_token")):

Something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done


page_fields = (self.input.fields.get('max_results', None),
self.input.fields.get('page_size', None))
page_field_size = next(
(field for field in page_fields if field is not None), None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we don't have to explicitly check for None.

next((field for field in page_fields if field), None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to be extra explicit in case there was some false-ish value field might have. I suppose that's not the case for the Field class though.

if not page_field_size or page_field_size.type != int:
return None

# Return the first repeated field.
for field in self.output.fields.values():
if field.repeated:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{# This lives within the loop in order to ensure that this template
is empty if there are no paged methods.
-#}
from typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple
from typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple, Optional

{% filter sort_lines -%}
{% for method in service.methods.values() | selectattr('paged_result_field') -%}
Expand Down Expand Up @@ -68,14 +68,25 @@ class {{ method.name }}Pager:
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

{% if method.paged_result_field.map %}
def __iter__(self) -> Iterable[Tuple[str, {{ method.paged_result_field.ident | replace('Sequence[', '') | replace(']', '') }}]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the typing issue as we discussed offline.

for page in self.pages:
yield from page.{{ method.paged_result_field.name}}.items()

def get(self, key: str) -> {{ method.paged_result_field.ident | replace('Sequence', 'Optional') }}:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

return self._response.items.get(key)
{% else %}
def __iter__(self) -> {{ method.paged_result_field.ident | replace('Sequence', 'Iterable') }}:
for page in self.pages:
yield from page.{{ method.paged_result_field.name }}
{% endif %}

def __repr__(self) -> str:
return '{0}<{1!r}>'.format(self.__class__.__name__, self._response)


{# TODO(yon-mg): remove on rest async transport impl #}
{% if 'grpc' in opts.transport %}
class {{ method.name }}AsyncPager:
"""A pager for iterating through ``{{ method.name|snake_case }}`` requests.

Expand Down Expand Up @@ -138,5 +149,6 @@ class {{ method.name }}AsyncPager:
def __repr__(self) -> str:
return '{0}<{1!r}>'.format(self.__class__.__name__, self._response)

{% endif %}
{% endfor %}
{% endblock %}
1 change: 1 addition & 0 deletions gapic/templates/noxfile.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def mypy(session):
session.install('.')
session.run(
'mypy',
'--explicit-package-bases',
{%- if api.naming.module_namespace %}
'{{ api.naming.module_namespace[0] }}',
{%- else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def test_{{ method.name|snake_case }}_raw_page_lro():
assert response.raw_page is response
{% endif %} {#- method.paged_result_field #}

{% endfor -%} {#- method in methods #}
{% endfor -%} {#- method in methods for grpc #}

{% for method in service.methods.values() if 'rest' in opts.transport -%}
def test_{{ method.name|snake_case }}_rest(transport: str = 'rest', request_type={{ method.input.ident }}):
Expand Down Expand Up @@ -1162,7 +1162,122 @@ def test_{{ method.name|snake_case }}_rest_flattened_error():
)


{% endfor -%}
{% if method.paged_result_field %}
def test_{{ method.name|snake_case }}_pager():
client = {{ service.client_name }}(
credentials=credentials.AnonymousCredentials(),
)

# Mock the http request call within the method and fake a response.
with mock.patch.object(Session, 'request') as req:
# Set the response as a series of pages
{% if method.paged_result_field.map%}
response = (
{{ method.output.ident }}(
{{ method.paged_result_field.name }}={
'a':{{ method.paged_result_field.type.fields.get('value').ident }}(),
'b':{{ method.paged_result_field.type.fields.get('value').ident }}(),
'c':{{ method.paged_result_field.type.fields.get('value').ident }}(),
},
next_page_token='abc',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}={},
next_page_token='def',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}={
'g':{{ method.paged_result_field.type.fields.get('value').ident }}(),
},
next_page_token='ghi',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}={
'h':{{ method.paged_result_field.type.fields.get('value').ident }}(),
'i':{{ method.paged_result_field.type.fields.get('value').ident }}(),
},
),
)
{% else %}
response = (
{{ method.output.ident }}(
{{ method.paged_result_field.name }}=[
{{ method.paged_result_field.type.ident }}(),
{{ method.paged_result_field.type.ident }}(),
{{ method.paged_result_field.type.ident }}(),
],
next_page_token='abc',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}=[],
next_page_token='def',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}=[
{{ method.paged_result_field.type.ident }}(),
],
next_page_token='ghi',
),
{{ method.output.ident }}(
{{ method.paged_result_field.name }}=[
{{ method.paged_result_field.type.ident }}(),
{{ method.paged_result_field.type.ident }}(),
],
),
)
{% endif %}
# Two responses for two calls
response = response + response

# Wrap the values into proper Response objs
response = tuple(map(lambda x: {{ method.output.ident }}.to_json(x), response))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python question: response and side_effect only contain one element. Why do you need to make them tuples? (I see zip() requires iterables, but so does tuple())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you don't need to call both map and tuple: tuple can take generator expressions as its iterable.

response = tuple({{ method.output.ident }}.to_json(x) for x in response)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that. Not sure why I was dead set on using map.

side_effect = tuple(map(lambda x: Response(), response))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being inexperienced with Python mocks, I found the name side_effect confusing here. IIUC, this could be called return_values (and then assigned to req.side_effect)

(Note to self: https://docs.python.org/3/library/unittest.mock-examples.html#side-effect-functions-and-iterables)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be a little bit clever here:

side_effect = (Response(),) * len(response)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does not work because new Response objects are not made. Instead it is aliases to the same object and editing a value within one changes it for all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, good point.

for return_val, response_val in zip(side_effect, response):
return_val._content = response_val.encode('UTF-8')
req.side_effect = side_effect

metadata = ()
{% if method.field_headers -%}
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((
{%- for field_header in method.field_headers %}
{%- if not method.client_streaming %}
('{{ field_header }}', ''),
{%- endif %}
{%- endfor %}
)),
)
{% endif -%}
pager = client.{{ method.name|snake_case }}(request={})

assert pager._metadata == metadata

{% if method.paged_result_field.map %}
assert isinstance(pager.get('a'), {{ method.paged_result_field.type.fields.get('value').ident }})
assert pager.get('h') is None
{% endif %}

results = [i for i in pager]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judgement call: can also do results = list(pager)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cleaner. Done.

assert len(results) == 6
{% if method.paged_result_field.map %}
assert all(
isinstance(i, tuple) and
tuple(map(lambda x: type(x), results[0])) == (str, {{ method.paged_result_field.type.fields.get('value').ident }})
for i in results)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I misreading this expression, or is this a loop invariant? If so, let's lift it up. In either case, let's clarifiy it to

tuple(type(r) for r in results[0])

Actually, the whole line looks like a loop invariant.

assert pager.get('a') is None
assert isinstance(pager.get('h'), {{ method.paged_result_field.type.fields.get('value').ident }})
{% else %}
assert all(isinstance(i, {{ method.paged_result_field.type.ident }})
for i in results)
{% endif %}

pages = list(client.{{ method.name|snake_case }}(request={}).pages)
for page_, token in zip(pages, ['abc','def','ghi', '']):
assert page_.raw_page.next_page_token == token


{% endif %} {# paged methods #}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the results of this template visible in a generated file within this repo? (just asking, the repo may not be structured that way)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It may be useful to include a generated client but that might clog the repo. The showcase client is generated as part of CircleCI tests though.

{% endfor -%} {#- method in methods for rest #}
def test_credentials_transport_error():
# It is an error to provide credentials and a transport instance.
transport = transports.{{ service.name }}{{ opts.transport[0].capitalize() }}Transport(
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def showcase_mypy(
session.chdir(lib)

# Run the tests.
session.run("mypy", "google")
session.run("mypy", "--explicit-package-bases", "google")


@nox.session(python="3.8")
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/schema/wrappers/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def test_method_paged_result_field_no_page_field():
)
assert method.paged_result_field is None

method = make_method('Foo',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting nit: please add a newline and then indent here for better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.. I think.

input_message=make_message(name='FooRequest', fields=(
make_field(name='page_token', type=9), # str
)),
output_message=make_message(name='FooResponse', fields=(
make_field(name='next_page_token', type=9), # str
))
)
assert method.paged_result_field is None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we add tests in this file for max_results and for mapped responses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapped responses are treated the same here. Checking for repeated fields should be sufficient since mapped fields are also repeated. Test for max_results is now also added.



def test_method_paged_result_ref_types():
input_msg = make_message(
Expand All @@ -139,7 +149,7 @@ def test_method_paged_result_ref_types():
name='ListMolluscsResponse',
fields=(
make_field(name='molluscs', message=mollusc_msg, repeated=True),
make_field(name='next_page_token', type=9)
make_field(name='next_page_token', type=9) # str
),
module='mollusc'
)
Expand Down Expand Up @@ -207,7 +217,7 @@ def test_flattened_ref_types():


def test_method_paged_result_primitive():
paged = make_field(name='squids', type=9, repeated=True)
paged = make_field(name='squids', type=9, repeated=True) # str
input_msg = make_message(
name='ListSquidsRequest',
fields=(
Expand Down