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 Callable, 19 Iterator, 20 Sequence, 21 Tuple, 22) 23 24from google.longrunning import operations_pb2 25 26 27class ListOperationsPager: 28 """A pager for iterating through ``list_operations`` requests. 29 30 This class thinly wraps an initial 31 :class:`google.longrunning.operations_pb2.ListOperationsResponse` object, and 32 provides an ``__iter__`` method to iterate through its 33 ``operations`` field. 34 35 If there are more pages, the ``__iter__`` method will make additional 36 ``ListOperations`` requests and continue to iterate 37 through the ``operations`` field on the 38 corresponding responses. 39 40 All the usual :class:`google.longrunning.operations_pb2.ListOperationsResponse` 41 attributes are available on the pager. If multiple requests are made, only 42 the most recent response is retained, and thus used for attribute lookup. 43 """ 44 45 def __init__( 46 self, 47 method: Callable[..., operations_pb2.ListOperationsResponse], 48 request: operations_pb2.ListOperationsRequest, 49 response: operations_pb2.ListOperationsResponse, 50 *, 51 metadata: Sequence[Tuple[str, str]] = () 52 ): 53 """Instantiate the pager. 54 55 Args: 56 method (Callable): The method that was originally called, and 57 which instantiated this pager. 58 request (google.longrunning.operations_pb2.ListOperationsRequest): 59 The initial request object. 60 response (google.longrunning.operations_pb2.ListOperationsResponse): 61 The initial response object. 62 metadata (Sequence[Tuple[str, str]]): Strings which should be 63 sent along with the request as metadata. 64 """ 65 self._method = method 66 self._request = request 67 self._response = response 68 self._metadata = metadata 69 70 def __getattr__(self, name: str) -> Any: 71 return getattr(self._response, name) 72 73 @property 74 def pages(self) -> Iterator[operations_pb2.ListOperationsResponse]: 75 yield self._response 76 while self._response.next_page_token: 77 self._request.page_token = self._response.next_page_token 78 self._response = self._method(self._request, metadata=self._metadata) 79 yield self._response 80 81 def __iter__(self) -> Iterator[operations_pb2.Operation]: 82 for page in self.pages: 83 yield from page.operations 84 85 def __repr__(self) -> str: 86 return "{0}<{1!r}>".format(self.__class__.__name__, self._response) 87