1# -*- coding: utf-8 -*-
2# Copyright 2020 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16from typing import (
17    Any,
18    AsyncIterator,
19    Awaitable,
20    Callable,
21    Sequence,
22    Tuple,
23    Optional,
24    Iterator,
25)
26
27from google.cloud.logging_v2.types import logging_metrics
28
29
30class ListLogMetricsPager:
31    """A pager for iterating through ``list_log_metrics`` requests.
32
33    This class thinly wraps an initial
34    :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` object, and
35    provides an ``__iter__`` method to iterate through its
36    ``metrics`` field.
37
38    If there are more pages, the ``__iter__`` method will make additional
39    ``ListLogMetrics`` requests and continue to iterate
40    through the ``metrics`` field on the
41    corresponding responses.
42
43    All the usual :class:`google.cloud.logging_v2.types.ListLogMetricsResponse`
44    attributes are available on the pager. If multiple requests are made, only
45    the most recent response is retained, and thus used for attribute lookup.
46    """
47
48    def __init__(
49        self,
50        method: Callable[..., logging_metrics.ListLogMetricsResponse],
51        request: logging_metrics.ListLogMetricsRequest,
52        response: logging_metrics.ListLogMetricsResponse,
53        *,
54        metadata: Sequence[Tuple[str, str]] = ()
55    ):
56        """Instantiate the pager.
57
58        Args:
59            method (Callable): The method that was originally called, and
60                which instantiated this pager.
61            request (google.cloud.logging_v2.types.ListLogMetricsRequest):
62                The initial request object.
63            response (google.cloud.logging_v2.types.ListLogMetricsResponse):
64                The initial response object.
65            metadata (Sequence[Tuple[str, str]]): Strings which should be
66                sent along with the request as metadata.
67        """
68        self._method = method
69        self._request = logging_metrics.ListLogMetricsRequest(request)
70        self._response = response
71        self._metadata = metadata
72
73    def __getattr__(self, name: str) -> Any:
74        return getattr(self._response, name)
75
76    @property
77    def pages(self) -> Iterator[logging_metrics.ListLogMetricsResponse]:
78        yield self._response
79        while self._response.next_page_token:
80            self._request.page_token = self._response.next_page_token
81            self._response = self._method(self._request, metadata=self._metadata)
82            yield self._response
83
84    def __iter__(self) -> Iterator[logging_metrics.LogMetric]:
85        for page in self.pages:
86            yield from page.metrics
87
88    def __repr__(self) -> str:
89        return "{0}<{1!r}>".format(self.__class__.__name__, self._response)
90
91
92class ListLogMetricsAsyncPager:
93    """A pager for iterating through ``list_log_metrics`` requests.
94
95    This class thinly wraps an initial
96    :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` object, and
97    provides an ``__aiter__`` method to iterate through its
98    ``metrics`` field.
99
100    If there are more pages, the ``__aiter__`` method will make additional
101    ``ListLogMetrics`` requests and continue to iterate
102    through the ``metrics`` field on the
103    corresponding responses.
104
105    All the usual :class:`google.cloud.logging_v2.types.ListLogMetricsResponse`
106    attributes are available on the pager. If multiple requests are made, only
107    the most recent response is retained, and thus used for attribute lookup.
108    """
109
110    def __init__(
111        self,
112        method: Callable[..., Awaitable[logging_metrics.ListLogMetricsResponse]],
113        request: logging_metrics.ListLogMetricsRequest,
114        response: logging_metrics.ListLogMetricsResponse,
115        *,
116        metadata: Sequence[Tuple[str, str]] = ()
117    ):
118        """Instantiates the pager.
119
120        Args:
121            method (Callable): The method that was originally called, and
122                which instantiated this pager.
123            request (google.cloud.logging_v2.types.ListLogMetricsRequest):
124                The initial request object.
125            response (google.cloud.logging_v2.types.ListLogMetricsResponse):
126                The initial response object.
127            metadata (Sequence[Tuple[str, str]]): Strings which should be
128                sent along with the request as metadata.
129        """
130        self._method = method
131        self._request = logging_metrics.ListLogMetricsRequest(request)
132        self._response = response
133        self._metadata = metadata
134
135    def __getattr__(self, name: str) -> Any:
136        return getattr(self._response, name)
137
138    @property
139    async def pages(self) -> AsyncIterator[logging_metrics.ListLogMetricsResponse]:
140        yield self._response
141        while self._response.next_page_token:
142            self._request.page_token = self._response.next_page_token
143            self._response = await self._method(self._request, metadata=self._metadata)
144            yield self._response
145
146    def __aiter__(self) -> AsyncIterator[logging_metrics.LogMetric]:
147        async def async_generator():
148            async for page in self.pages:
149                for response in page.metrics:
150                    yield response
151
152        return async_generator()
153
154    def __repr__(self) -> str:
155        return "{0}<{1!r}>".format(self.__class__.__name__, self._response)
156