2323from google .cloud .bigquery .job import LoadTableFromStorageJob
2424from google .cloud .bigquery .job import QueryJob
2525from google .cloud .bigquery .query import QueryResults
26+ from google .cloud .iterator import Iterator
2627
2728
2829class Project (object ):
@@ -87,26 +88,13 @@ def list_projects(self, max_results=None, page_token=None):
8788 not passed, the API will return the first page of
8889 projects.
8990
90- :rtype: tuple, (list, str)
91- :returns: list of :class:`~google.cloud.bigquery.client.Project`,
92- plus a "next page token" string: if the token is not None,
93- indicates that more projects can be retrieved with another
94- call (pass that value as ``page_token``).
91+ :rtype: :class:`~google.cloud.iterator.Iterator`
92+ :returns: Iterator of :class:`~google.cloud.bigquery.client.Project`
93+ accessible to the current client.
9594 """
96- params = {}
97-
98- if max_results is not None :
99- params ['maxResults' ] = max_results
100-
101- if page_token is not None :
102- params ['pageToken' ] = page_token
103-
104- path = '/projects'
105- resp = self .connection .api_request (method = 'GET' , path = path ,
106- query_params = params )
107- projects = [Project .from_api_repr (resource )
108- for resource in resp .get ('projects' , ())]
109- return projects , resp .get ('nextPageToken' )
95+ return Iterator (client = self , path = '/projects' ,
96+ items_key = 'projects' , item_to_value = _item_to_project ,
97+ page_token = page_token , max_results = max_results )
11098
11199 def list_datasets (self , include_all = False , max_results = None ,
112100 page_token = None ):
@@ -127,29 +115,18 @@ def list_datasets(self, include_all=False, max_results=None,
127115 not passed, the API will return the first page of
128116 datasets.
129117
130- :rtype: tuple, (list, str)
131- :returns: list of :class:`~google.cloud.bigquery.dataset.Dataset`,
132- plus a "next page token" string: if the token is not None,
133- indicates that more datasets can be retrieved with another
134- call (pass that value as ``page_token``).
118+ :rtype: :class:`~google.cloud.iterator.Iterator`
119+ :returns: Iterator of :class:`~google.cloud.bigquery.dataset.Dataset`.
120+ accessible to the current client.
135121 """
136- params = {}
137-
122+ extra_params = {}
138123 if include_all :
139- params ['all' ] = True
140-
141- if max_results is not None :
142- params ['maxResults' ] = max_results
143-
144- if page_token is not None :
145- params ['pageToken' ] = page_token
146-
124+ extra_params ['all' ] = True
147125 path = '/projects/%s/datasets' % (self .project ,)
148- resp = self .connection .api_request (method = 'GET' , path = path ,
149- query_params = params )
150- datasets = [Dataset .from_api_repr (resource , self )
151- for resource in resp .get ('datasets' , ())]
152- return datasets , resp .get ('nextPageToken' )
126+ return Iterator (
127+ client = self , path = path , items_key = 'datasets' ,
128+ item_to_value = _item_to_dataset , page_token = page_token ,
129+ max_results = max_results , extra_params = extra_params )
153130
154131 def dataset (self , dataset_name ):
155132 """Construct a dataset bound to this client.
@@ -215,32 +192,22 @@ def list_jobs(self, max_results=None, page_token=None, all_users=None,
215192 * ``"pending"``
216193 * ``"running"``
217194
218- :rtype: tuple, (list, str)
219- :returns: list of job instances, plus a "next page token" string:
220- if the token is not ``None``, indicates that more jobs can be
221- retrieved with another call, passing that value as
222- ``page_token``).
195+ :rtype: :class:`~google.cloud.iterator.Iterator`
196+ :returns: Iterable of job instances.
223197 """
224- params = {'projection' : 'full' }
225-
226- if max_results is not None :
227- params ['maxResults' ] = max_results
228-
229- if page_token is not None :
230- params ['pageToken' ] = page_token
198+ extra_params = {'projection' : 'full' }
231199
232200 if all_users is not None :
233- params ['allUsers' ] = all_users
201+ extra_params ['allUsers' ] = all_users
234202
235203 if state_filter is not None :
236- params ['stateFilter' ] = state_filter
204+ extra_params ['stateFilter' ] = state_filter
237205
238206 path = '/projects/%s/jobs' % (self .project ,)
239- resp = self .connection .api_request (method = 'GET' , path = path ,
240- query_params = params )
241- jobs = [self .job_from_resource (resource )
242- for resource in resp .get ('jobs' , ())]
243- return jobs , resp .get ('nextPageToken' )
207+ return Iterator (
208+ client = self , path = path , items_key = 'jobs' ,
209+ item_to_value = _item_to_job , page_token = page_token ,
210+ max_results = max_results , extra_params = extra_params )
244211
245212 def load_table_from_storage (self , job_name , destination , * source_uris ):
246213 """Construct a job for loading data into a table from CloudStorage.
@@ -334,3 +301,50 @@ def run_sync_query(self, query):
334301 :returns: a new ``QueryResults`` instance
335302 """
336303 return QueryResults (query , client = self )
304+
305+
306+ # pylint: disable=unused-argument
307+ def _item_to_project (iterator , resource ):
308+ """Convert a JSON project to the native object.
309+
310+ :type iterator: :class:`~google.cloud.iterator.Iterator`
311+ :param iterator: The iterator that is currently in use.
312+
313+ :type resource: dict
314+ :param resource: An item to be converted to a project.
315+
316+ :rtype: :class:`.Project`
317+ :returns: The next project in the page.
318+ """
319+ return Project .from_api_repr (resource )
320+ # pylint: enable=unused-argument
321+
322+
323+ def _item_to_dataset (iterator , resource ):
324+ """Convert a JSON dataset to the native object.
325+
326+ :type iterator: :class:`~google.cloud.iterator.Iterator`
327+ :param iterator: The iterator that is currently in use.
328+
329+ :type resource: dict
330+ :param resource: An item to be converted to a dataset.
331+
332+ :rtype: :class:`.Dataset`
333+ :returns: The next dataset in the page.
334+ """
335+ return Dataset .from_api_repr (resource , iterator .client )
336+
337+
338+ def _item_to_job (iterator , resource ):
339+ """Convert a JSON job to the native object.
340+
341+ :type iterator: :class:`~google.cloud.iterator.Iterator`
342+ :param iterator: The iterator that is currently in use.
343+
344+ :type resource: dict
345+ :param resource: An item to be converted to a job.
346+
347+ :rtype: job instance.
348+ :returns: The next job in the page.
349+ """
350+ return iterator .client .job_from_resource (resource )
0 commit comments