Skip to content

Commit fcf6a8e

Browse files
committed
Updating list_metrics() to Iterator pattern.
1 parent 4b4023f commit fcf6a8e

3 files changed

Lines changed: 54 additions & 30 deletions

File tree

logging/google/cloud/logging/_gax.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from google.cloud.iterator import GAXIterator
3434
from google.cloud.logging._helpers import entry_from_resource
3535
from google.cloud.logging.sink import Sink
36+
from google.cloud.logging.metric import Metric
3637

3738

3839
class _LoggingAPI(object):
@@ -316,21 +317,18 @@ def list_metrics(self, project, page_size=0, page_token=None):
316317
passed, the API will return the first page of
317318
metrics.
318319
319-
:rtype: tuple, (list, str)
320-
:returns: list of mappings, plus a "next page token" string:
321-
if not None, indicates that more metrics can be retrieved
322-
with another call (pass that value as ``page_token``).
320+
:rtype: :class:`~google.cloud.iterator.Iterator`
321+
:returns: Iterator of
322+
:class:`~google.cloud.logging.metric.Metric`
323+
accessible to the current API.
323324
"""
324325
if page_token is None:
325326
page_token = INITIAL_PAGE
326327
options = CallOptions(page_token=page_token)
327328
path = 'projects/%s' % (project,)
328329
page_iter = self._gax_api.list_log_metrics(
329330
path, page_size=page_size, options=options)
330-
metrics = [MessageToDict(log_metric_pb)
331-
for log_metric_pb in page_iter.next()]
332-
token = page_iter.page_token or None
333-
return metrics, token
331+
return GAXIterator(self._client, page_iter, _item_to_metric)
334332

335333
def metric_create(self, project, metric_name, filter_, description):
336334
"""API call: create a metric resource.
@@ -496,3 +494,21 @@ def _item_to_sink(iterator, log_sink_pb):
496494
"""
497495
resource = MessageToDict(log_sink_pb)
498496
return Sink.from_api_repr(resource, iterator.client)
497+
498+
499+
500+
def _item_to_metric(iterator, log_metric_pb):
501+
"""Convert a metric protobuf 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 log_metric_pb:
507+
:class:`~google.logging.v2.logging_metrics_pb2.LogMetric`
508+
:param log_metric_pb: Metric protobuf returned from the API.
509+
510+
:rtype: :class:`~google.cloud.logging.metric.Metric`
511+
:returns: The next metric in the page.
512+
"""
513+
resource = MessageToDict(log_metric_pb)
514+
return Metric.from_api_repr(resource, iterator.client)

logging/google/cloud/logging/client.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,9 @@ def list_metrics(self, page_size=None, page_token=None):
267267
passed, the API will return the first page of
268268
metrics.
269269
270-
:rtype: tuple, (list, str)
271-
:returns: list of :class:`google.cloud.logging.metric.Metric`, plus a
272-
"next page token" string: if not None, indicates that
273-
more metrics can be retrieved with another call (pass that
274-
value as ``page_token``).
270+
:rtype: :class:`~google.cloud.iterator.Iterator`
271+
:returns: Iterator of :class:`~google.cloud.logging.metric.Metric`
272+
accessible to the current client.
275273
"""
276-
resources, token = self.metrics_api.list_metrics(
274+
return self.metrics_api.list_metrics(
277275
self.project, page_size, page_token)
278-
metrics = [Metric.from_api_repr(resource, self)
279-
for resource in resources]
280-
return metrics, token

logging/google/cloud/logging/connection.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.cloud.iterator import HTTPIterator
2121
from google.cloud.logging._helpers import entry_from_resource
2222
from google.cloud.logging.sink import Sink
23+
from google.cloud.logging.metric import Metric
2324

2425

2526
class Connection(base_connection.JSONConnection):
@@ -347,24 +348,21 @@ def list_metrics(self, project, page_size=None, page_token=None):
347348
passed, the API will return the first page of
348349
metrics.
349350
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.
354355
"""
355-
params = {}
356+
extra_params = {}
356357

357358
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
362360

363361
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)
368366

369367
def metric_create(self, project, metric_name, filter_, description=None):
370368
"""API call: create a metric resource.
@@ -497,3 +495,18 @@ def _item_to_sink(iterator, resource):
497495
:returns: The next sink in the page.
498496
"""
499497
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

Comments
 (0)