1# -*- coding: utf-8 -*-
2# Copyright 2022 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 Iterator,
22 Optional,
23 Sequence,
24 Tuple,
25)
26
27from google.cloud.resourcemanager_v3.types import organizations
28
29
30class SearchOrganizationsPager:
31 """A pager for iterating through ``search_organizations`` requests.
32
33 This class thinly wraps an initial
34 :class:`google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse` object, and
35 provides an ``__iter__`` method to iterate through its
36 ``organizations`` field.
37
38 If there are more pages, the ``__iter__`` method will make additional
39 ``SearchOrganizations`` requests and continue to iterate
40 through the ``organizations`` field on the
41 corresponding responses.
42
43 All the usual :class:`google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse`
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[..., organizations.SearchOrganizationsResponse],
51 request: organizations.SearchOrganizationsRequest,
52 response: organizations.SearchOrganizationsResponse,
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.resourcemanager_v3.types.SearchOrganizationsRequest):
62 The initial request object.
63 response (google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse):
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 = organizations.SearchOrganizationsRequest(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[organizations.SearchOrganizationsResponse]:
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[organizations.Organization]:
85 for page in self.pages:
86 yield from page.organizations
87
88 def __repr__(self) -> str:
89 return "{0}<{1!r}>".format(self.__class__.__name__, self._response)
90
91
92class SearchOrganizationsAsyncPager:
93 """A pager for iterating through ``search_organizations`` requests.
94
95 This class thinly wraps an initial
96 :class:`google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse` object, and
97 provides an ``__aiter__`` method to iterate through its
98 ``organizations`` field.
99
100 If there are more pages, the ``__aiter__`` method will make additional
101 ``SearchOrganizations`` requests and continue to iterate
102 through the ``organizations`` field on the
103 corresponding responses.
104
105 All the usual :class:`google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse`
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[organizations.SearchOrganizationsResponse]],
113 request: organizations.SearchOrganizationsRequest,
114 response: organizations.SearchOrganizationsResponse,
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.resourcemanager_v3.types.SearchOrganizationsRequest):
124 The initial request object.
125 response (google.cloud.resourcemanager_v3.types.SearchOrganizationsResponse):
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 = organizations.SearchOrganizationsRequest(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[organizations.SearchOrganizationsResponse]:
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[organizations.Organization]:
147 async def async_generator():
148 async for page in self.pages:
149 for response in page.organizations:
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)