|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Atlan Pte. Ltd. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, List, Optional |
| 7 | + |
| 8 | +from pydantic.v1 import Field, PrivateAttr, ValidationError, parse_obj_as |
| 9 | + |
| 10 | +from pyatlan.errors import ErrorCode |
| 11 | +from pyatlan.model.core import AtlanObject |
| 12 | +from pyatlan.model.oauth_client import OAuthClientResponse |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from pyatlan.client.aio.client import AsyncAtlanClient |
| 16 | + from pyatlan.client.constants import API |
| 17 | + |
| 18 | + |
| 19 | +class AsyncOAuthClientListResponse(AtlanObject): |
| 20 | + """Async version of OAuthClientListResponse with async pagination support.""" |
| 21 | + |
| 22 | + _size: int = PrivateAttr() |
| 23 | + _start: int = PrivateAttr() |
| 24 | + _endpoint: API = PrivateAttr() |
| 25 | + _client: AsyncAtlanClient = PrivateAttr() |
| 26 | + _sort: Optional[str] = PrivateAttr() |
| 27 | + |
| 28 | + total_record: Optional[int] = Field( |
| 29 | + default=None, description="Total number of OAuth clients." |
| 30 | + ) |
| 31 | + filter_record: Optional[int] = Field( |
| 32 | + default=None, |
| 33 | + description="Number of OAuth clients that matched the specified filters.", |
| 34 | + ) |
| 35 | + records: Optional[List[OAuthClientResponse]] = Field( |
| 36 | + default=None, description="List of OAuth clients." |
| 37 | + ) |
| 38 | + |
| 39 | + def __init__(self, **data: Any): |
| 40 | + super().__init__(**data) |
| 41 | + self._endpoint = data.get("endpoint") # type: ignore[assignment] |
| 42 | + self._client = data.get("client") # type: ignore[assignment] |
| 43 | + self._size = data.get("size") or 20 # type: ignore[assignment] |
| 44 | + self._start = data.get("start") or 0 # type: ignore[assignment] |
| 45 | + self._sort = data.get("sort") # type: ignore[assignment] |
| 46 | + |
| 47 | + def current_page(self) -> Optional[List[OAuthClientResponse]]: |
| 48 | + """Get the current page of OAuth clients.""" |
| 49 | + return self.records |
| 50 | + |
| 51 | + async def next_page( |
| 52 | + self, start: Optional[int] = None, size: Optional[int] = None |
| 53 | + ) -> bool: |
| 54 | + """ |
| 55 | + Retrieve the next page of results. |
| 56 | +
|
| 57 | + :param start: starting point for the next page |
| 58 | + :param size: page size for the next page |
| 59 | + :returns: True if there was a next page, False otherwise |
| 60 | + """ |
| 61 | + self._start = start or self._start + self._size |
| 62 | + if size: |
| 63 | + self._size = size |
| 64 | + return await self._get_next_page() if self.records else False |
| 65 | + |
| 66 | + async def _get_next_page(self) -> bool: |
| 67 | + """Fetch the next page of results.""" |
| 68 | + query_params: Dict[str, str] = { |
| 69 | + "count": "true", |
| 70 | + "offset": str(self._start), |
| 71 | + "limit": str(self._size), |
| 72 | + } |
| 73 | + if self._sort is not None: |
| 74 | + query_params["sort"] = self._sort |
| 75 | + raw_json = await self._client._call_api( |
| 76 | + api=self._endpoint, |
| 77 | + query_params=query_params, |
| 78 | + ) |
| 79 | + if not raw_json.get("records"): |
| 80 | + self.records = [] |
| 81 | + return False |
| 82 | + try: |
| 83 | + self.records = parse_obj_as( |
| 84 | + List[OAuthClientResponse], raw_json.get("records") |
| 85 | + ) |
| 86 | + except ValidationError as err: |
| 87 | + raise ErrorCode.JSON_ERROR.exception_with_parameters( |
| 88 | + raw_json, 200, str(err) |
| 89 | + ) from err |
| 90 | + return True |
| 91 | + |
| 92 | + async def __aiter__(self) -> AsyncGenerator[OAuthClientResponse, None]: |
| 93 | + """Async iterator for OAuth clients across all pages.""" |
| 94 | + while self.records: |
| 95 | + for oauth_client in self.records: |
| 96 | + yield oauth_client |
| 97 | + if not await self.next_page(): |
| 98 | + break |
0 commit comments