|
20 | 20 | from google.cloud.iterator import HTTPIterator |
21 | 21 | from google.cloud.logging._helpers import entry_from_resource |
22 | 22 | from google.cloud.logging.sink import Sink |
| 23 | +from google.cloud.logging.metric import Metric |
23 | 24 |
|
24 | 25 |
|
25 | 26 | class Connection(base_connection.JSONConnection): |
@@ -347,24 +348,21 @@ def list_metrics(self, project, page_size=None, page_token=None): |
347 | 348 | passed, the API will return the first page of |
348 | 349 | metrics. |
349 | 350 |
|
350 | | - :rtype: tuple, (list, str) |
351 | | - :returns: list of mappings, plus a "next page token" string: |
352 | | - if not None, indicates that more metrics can be retrieved |
353 | | - with another call (pass that value as ``page_token``). |
| 351 | + :rtype: :class:`~google.cloud.iterator.Iterator` |
| 352 | + :returns: Iterator of |
| 353 | + :class:`~google.cloud.logging.metric.Metric` |
| 354 | + accessible to the current API. |
354 | 355 | """ |
355 | | - params = {} |
| 356 | + extra_params = {} |
356 | 357 |
|
357 | 358 | if page_size is not None: |
358 | | - params['pageSize'] = page_size |
359 | | - |
360 | | - if page_token is not None: |
361 | | - params['pageToken'] = page_token |
| 359 | + extra_params['pageSize'] = page_size |
362 | 360 |
|
363 | 361 | path = '/projects/%s/metrics' % (project,) |
364 | | - resp = self._connection.api_request( |
365 | | - method='GET', path=path, query_params=params) |
366 | | - metrics = resp.get('metrics', ()) |
367 | | - return metrics, resp.get('nextPageToken') |
| 362 | + return HTTPIterator( |
| 363 | + client=self._client, path=path, |
| 364 | + item_to_value=_item_to_metric, items_key='metrics', |
| 365 | + page_token=page_token, extra_params=extra_params) |
368 | 366 |
|
369 | 367 | def metric_create(self, project, metric_name, filter_, description=None): |
370 | 368 | """API call: create a metric resource. |
@@ -497,3 +495,18 @@ def _item_to_sink(iterator, resource): |
497 | 495 | :returns: The next sink in the page. |
498 | 496 | """ |
499 | 497 | return Sink.from_api_repr(resource, iterator.client) |
| 498 | + |
| 499 | + |
| 500 | +def _item_to_metric(iterator, resource): |
| 501 | + """Convert a metric resource to the native object. |
| 502 | +
|
| 503 | + :type iterator: :class:`~google.cloud.iterator.Iterator` |
| 504 | + :param iterator: The iterator that is currently in use. |
| 505 | +
|
| 506 | + :type resource: dict |
| 507 | + :param resource: Metric JSON resource returned from the API. |
| 508 | +
|
| 509 | + :rtype: :class:`~google.cloud.logging.metric.Metric` |
| 510 | + :returns: The next metric in the page. |
| 511 | + """ |
| 512 | + return Metric.from_api_repr(resource, iterator.client) |
0 commit comments