|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections.abc import Iterator |
| 4 | +from typing import Any |
| 5 | + |
3 | 6 | from ..errors import ( |
4 | 7 | InvalidNameError, |
5 | 8 | InvalidOrgError, |
|
11 | 14 | from ..models.policy import ( |
12 | 15 | Policy, |
13 | 16 | PolicyCreateOptions, |
14 | | - PolicyList, |
15 | 17 | PolicyListOptions, |
16 | 18 | PolicyUpdateOptions, |
17 | 19 | ) |
|
22 | 24 | class Policies(_Service): |
23 | 25 | def list( |
24 | 26 | self, organization: str, options: PolicyListOptions | None = None |
25 | | - ) -> PolicyList: |
26 | | - """List all the policies of the given organization.""" |
| 27 | + ) -> Iterator[Policy]: |
| 28 | + """Iterate all the policies of the given organization.""" |
27 | 29 | if not valid_string_id(organization): |
28 | 30 | raise InvalidOrgError() |
29 | | - params = ( |
30 | | - options.model_dump(by_alias=True, exclude_none=True) if options else None |
31 | | - ) |
32 | | - r = self.t.request( |
33 | | - "GET", |
34 | | - f"/api/v2/organizations/{organization}/policies", |
35 | | - params=params, |
36 | | - ) |
37 | | - jd = r.json() |
38 | | - items = [] |
39 | | - meta = jd.get("meta", {}) |
40 | | - pagination = meta.get("pagination", {}) |
41 | | - for d in jd.get("data", []): |
42 | | - attrs = d.get("attributes", {}) |
43 | | - attrs["id"] = d.get("id") |
44 | | - attrs["organization"] = d.get("relationships", {}).get("organization", {}) |
45 | | - items.append(Policy.model_validate(attrs)) |
46 | | - return PolicyList( |
47 | | - items=items, |
48 | | - current_page=pagination.get("current-page"), |
49 | | - total_pages=pagination.get("total-pages"), |
50 | | - prev_page=pagination.get("prev-page"), |
51 | | - next_page=pagination.get("next-page"), |
52 | | - total_count=pagination.get("total-count"), |
53 | | - ) |
| 31 | + |
| 32 | + path = f"/api/v2/organizations/{organization}/policies" |
| 33 | + params: dict[str, Any] = {} |
| 34 | + |
| 35 | + if options: |
| 36 | + if getattr(options, "page_size", None): |
| 37 | + params["page[size]"] = str(options.page_size) |
| 38 | + |
| 39 | + def _gen() -> Iterator[Policy]: |
| 40 | + for item in self._list(path, params=params): |
| 41 | + attrs = item.get("attributes", {}) |
| 42 | + attrs["id"] = item.get("id") |
| 43 | + attrs["organization"] = item.get("relationships", {}).get( |
| 44 | + "organization", {} |
| 45 | + ) |
| 46 | + yield Policy.model_validate(attrs) |
| 47 | + |
| 48 | + return _gen() |
54 | 49 |
|
55 | 50 | def create(self, organization: str, options: PolicyCreateOptions) -> Policy: |
56 | 51 | """Create a new policy in the given organization.""" |
|
0 commit comments