Skip to content

Commit

Permalink
Merge 8c7ef64 into cfefe5a
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylajin committed Feb 7, 2020
2 parents cfefe5a + 8c7ef64 commit bdec11c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions apitools/base/py/list_pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def YieldFromList(
method='List', field='items', predicate=None,
current_token_attribute='pageToken',
next_token_attribute='nextPageToken',
batch_size_attribute='maxResults'):
batch_size_attribute='maxResults',
get_field_fn=_GetattrNested):
"""Make a series of List requests, keeping track of page tokens.
Args:
Expand All @@ -94,6 +95,7 @@ def YieldFromList(
response message holding the maximum number of results to be
returned. None if caller-specified batch size is unsupported.
If a tuple, path to the attribute.
get_field_fn: lambda, A function that returns the items to be yielded.
Yields:
protorpc.message.Message, The resources listed by the service.
Expand All @@ -116,7 +118,7 @@ def YieldFromList(
_SetattrNested(request, batch_size_attribute, request_batch_size)
response = getattr(service, method)(request,
global_params=global_params)
items = _GetattrNested(response, field)
items = get_field_fn(response, field)
if predicate:
items = list(filter(predicate, items))
for item in items:
Expand Down
26 changes: 26 additions & 0 deletions apitools/base/py/list_pager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ def testYieldFromListWithPredicate(self):
client.column, request, predicate=lambda x: 'c' in x.name)

self._AssertInstanceSequence(results, 3)

def testYieldFromListWithCustomGetFieldFunction(self):
self.mocked_client.column.List.Expect(
messages.FusiontablesColumnListRequest(
maxResults=100,
pageToken=None,
tableId='mytable',
),
messages.ColumnList(
items=[
messages.Column(name='c0')
]
))
custom_getter_called = []

def Custom_Getter(message, attribute):
custom_getter_called.append(True)
return getattr(message, attribute)

client = fusiontables.FusiontablesV1(get_credentials=False)
request = messages.FusiontablesColumnListRequest(tableId='mytable')
results = list_pager.YieldFromList(
client.column, request, get_field_fn=Custom_Getter)

self._AssertInstanceSequence(results, 1)
self.assertEquals(1, len(custom_getter_called))


class ListPagerAttributeTest(unittest2.TestCase):
Expand Down

0 comments on commit bdec11c

Please sign in to comment.