1# -*- coding: utf-8 -*- 
    2# Copyright 2025 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 google.api_core import gapic_v1 
    17from google.api_core import retry as retries 
    18from google.api_core import retry_async as retries_async 
    19from typing import ( 
    20    Any, 
    21    AsyncIterator, 
    22    Awaitable, 
    23    Callable, 
    24    Sequence, 
    25    Tuple, 
    26    Optional, 
    27    Iterator, 
    28    Union, 
    29) 
    30 
    31try: 
    32    OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] 
    33    OptionalAsyncRetry = Union[ 
    34        retries_async.AsyncRetry, gapic_v1.method._MethodDefault, None 
    35    ] 
    36except AttributeError:  # pragma: NO COVER 
    37    OptionalRetry = Union[retries.Retry, object, None]  # type: ignore 
    38    OptionalAsyncRetry = Union[retries_async.AsyncRetry, object, None]  # type: ignore 
    39 
    40from google.cloud.logging_v2.types import logging_metrics 
    41 
    42 
    43class ListLogMetricsPager: 
    44    """A pager for iterating through ``list_log_metrics`` requests. 
    45 
    46    This class thinly wraps an initial 
    47    :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` object, and 
    48    provides an ``__iter__`` method to iterate through its 
    49    ``metrics`` field. 
    50 
    51    If there are more pages, the ``__iter__`` method will make additional 
    52    ``ListLogMetrics`` requests and continue to iterate 
    53    through the ``metrics`` field on the 
    54    corresponding responses. 
    55 
    56    All the usual :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` 
    57    attributes are available on the pager. If multiple requests are made, only 
    58    the most recent response is retained, and thus used for attribute lookup. 
    59    """ 
    60 
    61    def __init__( 
    62        self, 
    63        method: Callable[..., logging_metrics.ListLogMetricsResponse], 
    64        request: logging_metrics.ListLogMetricsRequest, 
    65        response: logging_metrics.ListLogMetricsResponse, 
    66        *, 
    67        retry: OptionalRetry = gapic_v1.method.DEFAULT, 
    68        timeout: Union[float, object] = gapic_v1.method.DEFAULT, 
    69        metadata: Sequence[Tuple[str, Union[str, bytes]]] = () 
    70    ): 
    71        """Instantiate the pager. 
    72 
    73        Args: 
    74            method (Callable): The method that was originally called, and 
    75                which instantiated this pager. 
    76            request (google.cloud.logging_v2.types.ListLogMetricsRequest): 
    77                The initial request object. 
    78            response (google.cloud.logging_v2.types.ListLogMetricsResponse): 
    79                The initial response object. 
    80            retry (google.api_core.retry.Retry): Designation of what errors, 
    81                if any, should be retried. 
    82            timeout (float): The timeout for this request. 
    83            metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be 
    84                sent along with the request as metadata. Normally, each value must be of type `str`, 
    85                but for metadata keys ending with the suffix `-bin`, the corresponding values must 
    86                be of type `bytes`. 
    87        """ 
    88        self._method = method 
    89        self._request = logging_metrics.ListLogMetricsRequest(request) 
    90        self._response = response 
    91        self._retry = retry 
    92        self._timeout = timeout 
    93        self._metadata = metadata 
    94 
    95    def __getattr__(self, name: str) -> Any: 
    96        return getattr(self._response, name) 
    97 
    98    @property 
    99    def pages(self) -> Iterator[logging_metrics.ListLogMetricsResponse]: 
    100        yield self._response 
    101        while self._response.next_page_token: 
    102            self._request.page_token = self._response.next_page_token 
    103            self._response = self._method( 
    104                self._request, 
    105                retry=self._retry, 
    106                timeout=self._timeout, 
    107                metadata=self._metadata, 
    108            ) 
    109            yield self._response 
    110 
    111    def __iter__(self) -> Iterator[logging_metrics.LogMetric]: 
    112        for page in self.pages: 
    113            yield from page.metrics 
    114 
    115    def __repr__(self) -> str: 
    116        return "{0}<{1!r}>".format(self.__class__.__name__, self._response) 
    117 
    118 
    119class ListLogMetricsAsyncPager: 
    120    """A pager for iterating through ``list_log_metrics`` requests. 
    121 
    122    This class thinly wraps an initial 
    123    :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` object, and 
    124    provides an ``__aiter__`` method to iterate through its 
    125    ``metrics`` field. 
    126 
    127    If there are more pages, the ``__aiter__`` method will make additional 
    128    ``ListLogMetrics`` requests and continue to iterate 
    129    through the ``metrics`` field on the 
    130    corresponding responses. 
    131 
    132    All the usual :class:`google.cloud.logging_v2.types.ListLogMetricsResponse` 
    133    attributes are available on the pager. If multiple requests are made, only 
    134    the most recent response is retained, and thus used for attribute lookup. 
    135    """ 
    136 
    137    def __init__( 
    138        self, 
    139        method: Callable[..., Awaitable[logging_metrics.ListLogMetricsResponse]], 
    140        request: logging_metrics.ListLogMetricsRequest, 
    141        response: logging_metrics.ListLogMetricsResponse, 
    142        *, 
    143        retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT, 
    144        timeout: Union[float, object] = gapic_v1.method.DEFAULT, 
    145        metadata: Sequence[Tuple[str, Union[str, bytes]]] = () 
    146    ): 
    147        """Instantiates the pager. 
    148 
    149        Args: 
    150            method (Callable): The method that was originally called, and 
    151                which instantiated this pager. 
    152            request (google.cloud.logging_v2.types.ListLogMetricsRequest): 
    153                The initial request object. 
    154            response (google.cloud.logging_v2.types.ListLogMetricsResponse): 
    155                The initial response object. 
    156            retry (google.api_core.retry.AsyncRetry): Designation of what errors, 
    157                if any, should be retried. 
    158            timeout (float): The timeout for this request. 
    159            metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be 
    160                sent along with the request as metadata. Normally, each value must be of type `str`, 
    161                but for metadata keys ending with the suffix `-bin`, the corresponding values must 
    162                be of type `bytes`. 
    163        """ 
    164        self._method = method 
    165        self._request = logging_metrics.ListLogMetricsRequest(request) 
    166        self._response = response 
    167        self._retry = retry 
    168        self._timeout = timeout 
    169        self._metadata = metadata 
    170 
    171    def __getattr__(self, name: str) -> Any: 
    172        return getattr(self._response, name) 
    173 
    174    @property 
    175    async def pages(self) -> AsyncIterator[logging_metrics.ListLogMetricsResponse]: 
    176        yield self._response 
    177        while self._response.next_page_token: 
    178            self._request.page_token = self._response.next_page_token 
    179            self._response = await self._method( 
    180                self._request, 
    181                retry=self._retry, 
    182                timeout=self._timeout, 
    183                metadata=self._metadata, 
    184            ) 
    185            yield self._response 
    186 
    187    def __aiter__(self) -> AsyncIterator[logging_metrics.LogMetric]: 
    188        async def async_generator(): 
    189            async for page in self.pages: 
    190                for response in page.metrics: 
    191                    yield response 
    192 
    193        return async_generator() 
    194 
    195    def __repr__(self) -> str: 
    196        return "{0}<{1!r}>".format(self.__class__.__name__, self._response)