Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: third_party/google-endpoints/apitools/base/py/list_pager.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright 2015 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """A helper function that executes a series of List queries for many APIs."""
18
19 from apitools.base.py import encoding
20
21 __all__ = [
22 'YieldFromList',
23 ]
24
25
26 def YieldFromList(
27 service, request, global_params=None, limit=None, batch_size=100,
28 method='List', field='items', predicate=None,
29 current_token_attribute='pageToken',
30 next_token_attribute='nextPageToken',
31 batch_size_attribute='maxResults'):
32 """Make a series of List requests, keeping track of page tokens.
33
34 Args:
35 service: apitools_base.BaseApiService, A service with a .List() method.
36 request: protorpc.messages.Message, The request message
37 corresponding to the service's .List() method, with all the
38 attributes populated except the .maxResults and .pageToken
39 attributes.
40 global_params: protorpc.messages.Message, The global query parameters to
41 provide when calling the given method.
42 limit: int, The maximum number of records to yield. None if all available
43 records should be yielded.
44 batch_size: int, The number of items to retrieve per request.
45 method: str, The name of the method used to fetch resources.
46 field: str, The field in the response that will be a list of items.
47 predicate: lambda, A function that returns true for items to be yielded.
48 current_token_attribute: str, The name of the attribute in a
49 request message holding the page token for the page being
50 requested.
51 next_token_attribute: str, The name of the attribute in a
52 response message holding the page token for the next page.
53 batch_size_attribute: str, The name of the attribute in a
54 response message holding the maximum number of results to be
55 returned.
56
57 Yields:
58 protorpc.message.Message, The resources listed by the service.
59
60 """
61 request = encoding.CopyProtoMessage(request)
62 setattr(request, batch_size_attribute, batch_size)
63 setattr(request, current_token_attribute, None)
64 while limit is None or limit:
65 response = getattr(service, method)(request,
66 global_params=global_params)
67 items = getattr(response, field)
68 if predicate:
69 items = list(filter(predicate, items))
70 for item in items:
71 yield item
72 if limit is None:
73 continue
74 limit -= 1
75 if not limit:
76 return
77 token = getattr(response, next_token_attribute)
78 if not token:
79 return
80 setattr(request, current_token_attribute, token)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698