diff options
author | kaylajin <60764042+kaylajin@users.noreply.github.com> | 2020-02-07 10:29:21 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 13:29:21 -0500 |
commit | bf0261315f5d4eded86b80303b4f363657d43994 (patch) | |
tree | 7a74f14e550f95e7a3598ade87676c00843c995b | |
parent | cfefe5a8322b40c6e7bd3cc794fd644edcc3a6d6 (diff) | |
download | apitools-bf0261315f5d4eded86b80303b4f363657d43994.tar.gz |
Pager helper to enable custom field getter (#287)
* Refactor to enable custom field getter
Example custom field getter for a response with
```
def customGetter(message, attribute):
del attribute # Unused argument
for book in message.bookshelf:
yield book
for book in message.library:
yield book
```
-rw-r--r-- | apitools/base/py/list_pager.py | 7 | ||||
-rw-r--r-- | apitools/base/py/list_pager_test.py | 26 |
2 files changed, 31 insertions, 2 deletions
diff --git a/apitools/base/py/list_pager.py b/apitools/base/py/list_pager.py index b755755..a2c1080 100644 --- a/apitools/base/py/list_pager.py +++ b/apitools/base/py/list_pager.py @@ -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_func=_GetattrNested): """Make a series of List requests, keeping track of page tokens. Args: @@ -94,6 +95,8 @@ 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_func: Function that returns the items to be yielded. Argument + is response message, and field. Yields: protorpc.message.Message, The resources listed by the service. @@ -116,7 +119,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_func(response, field) if predicate: items = list(filter(predicate, items)) for item in items: diff --git a/apitools/base/py/list_pager_test.py b/apitools/base/py/list_pager_test.py index f644146..e1141ae 100644 --- a/apitools/base/py/list_pager_test.py +++ b/apitools/base/py/list_pager_test.py @@ -268,6 +268,32 @@ class ListPagerTest(unittest2.TestCase): 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_func=Custom_Getter) + + self._AssertInstanceSequence(results, 1) + self.assertEquals(1, len(custom_getter_called)) + class ListPagerAttributeTest(unittest2.TestCase): |