|
| 1 | +from functools import partial |
| 2 | +from typing import Iterator |
| 3 | + |
| 4 | +from citrine._rest.collection import Collection, ResourceType |
| 5 | + |
| 6 | + |
| 7 | +class AdminCollection(Collection[ResourceType]): |
| 8 | + """Abstract class for representing collections of REST resources with as_admin access.""" |
| 9 | + |
| 10 | + def list( |
| 11 | + self, *, per_page: int = 100, as_admin: bool = False |
| 12 | + ) -> Iterator[ResourceType]: |
| 13 | + """ |
| 14 | + Paginate over the elements of the collection. |
| 15 | +
|
| 16 | + Leaving page and per_page as default values will yield all elements in the |
| 17 | + collection, paginating over all available pages. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + --------- |
| 21 | + per_page: int, optional |
| 22 | + Max number of results to return per page. Default is 100. This parameter |
| 23 | + is used when making requests to the backend service. If the page parameter |
| 24 | + is specified it limits the maximum number of elements in the response. |
| 25 | +
|
| 26 | + as_admin: bool, optional |
| 27 | + Whether this request should be made as an admin (returning all teams, |
| 28 | + rather than only those to which the user belongs). |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + Iterator[ResourceType] |
| 33 | + An iterator that can be used to loop over all the resources in this collection. |
| 34 | + Use list() to force evaluation of all results into an in-memory list. |
| 35 | +
|
| 36 | + """ |
| 37 | + fetcher = partial( |
| 38 | + self._fetch_page, additional_params={"as_admin": "true"} if as_admin else {} |
| 39 | + ) |
| 40 | + return self._paginator.paginate( |
| 41 | + page_fetcher=fetcher, |
| 42 | + collection_builder=self._build_collection_elements, |
| 43 | + per_page=per_page, |
| 44 | + ) |
0 commit comments