|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Atlan Pte. Ltd. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import List, Optional |
| 7 | + |
| 8 | +from pydantic.v1 import validate_arguments |
| 9 | + |
| 10 | +from pyatlan.client.common import ( |
| 11 | + AsyncApiCaller, |
| 12 | + OAuthClientCreate, |
| 13 | + OAuthClientGet, |
| 14 | + OAuthClientGetById, |
| 15 | + OAuthClientPurge, |
| 16 | + OAuthClientUpdate, |
| 17 | + RoleGet, |
| 18 | +) |
| 19 | +from pyatlan.errors import ErrorCode |
| 20 | +from pyatlan.model.aio.oauth_client import AsyncOAuthClientListResponse |
| 21 | +from pyatlan.model.oauth_client import OAuthClientCreateResponse, OAuthClientResponse |
| 22 | + |
| 23 | + |
| 24 | +class AsyncOAuthClient: |
| 25 | + """ |
| 26 | + Async client for managing OAuth client credentials. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, client: AsyncApiCaller): |
| 30 | + if not isinstance(client, AsyncApiCaller): |
| 31 | + raise ErrorCode.INVALID_PARAMETER_TYPE.exception_with_parameters( |
| 32 | + "client", "AsyncApiCaller" |
| 33 | + ) |
| 34 | + self._client = client |
| 35 | + |
| 36 | + @validate_arguments |
| 37 | + async def get( |
| 38 | + self, |
| 39 | + limit: int = 20, |
| 40 | + offset: int = 0, |
| 41 | + sort: Optional[str] = None, |
| 42 | + ) -> AsyncOAuthClientListResponse: |
| 43 | + """ |
| 44 | + Retrieves OAuth clients defined in Atlan with pagination support. |
| 45 | +
|
| 46 | + :param limit: maximum number of results to be returned per page (default: 20) |
| 47 | + :param offset: starting point for results to return, for paging |
| 48 | + :param sort: property by which to sort the results (e.g., 'createdAt' for descending) |
| 49 | + :returns: an AsyncOAuthClientListResponse containing records and pagination info |
| 50 | + :raises AtlanError: on any API communication issue |
| 51 | + """ |
| 52 | + endpoint, query_params = OAuthClientGet.prepare_request(limit, offset, sort) |
| 53 | + raw_json = await self._client._call_api(endpoint, query_params) |
| 54 | + return AsyncOAuthClientListResponse( |
| 55 | + **raw_json, |
| 56 | + endpoint=endpoint, |
| 57 | + client=self._client, |
| 58 | + size=limit, |
| 59 | + start=offset, |
| 60 | + sort=sort, |
| 61 | + ) |
| 62 | + |
| 63 | + @validate_arguments |
| 64 | + async def get_by_id(self, client_id: str) -> OAuthClientResponse: |
| 65 | + """ |
| 66 | + Retrieves the OAuth client with the specified client ID. |
| 67 | +
|
| 68 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 69 | + :returns: the OAuthClientResponse with the specified client ID |
| 70 | + :raises AtlanError: on any API communication issue |
| 71 | + """ |
| 72 | + endpoint, query_params = OAuthClientGetById.prepare_request(client_id) |
| 73 | + raw_json = await self._client._call_api(endpoint, query_params) |
| 74 | + return OAuthClientGetById.process_response(raw_json) |
| 75 | + |
| 76 | + @validate_arguments |
| 77 | + async def update( |
| 78 | + self, |
| 79 | + client_id: str, |
| 80 | + display_name: Optional[str] = None, |
| 81 | + description: Optional[str] = None, |
| 82 | + ) -> OAuthClientResponse: |
| 83 | + """ |
| 84 | + Update an existing OAuth client with the provided settings. |
| 85 | +
|
| 86 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 87 | + :param display_name: human-readable name for the OAuth client |
| 88 | + :param description: optional explanation of the OAuth client |
| 89 | + :returns: the updated OAuthClientResponse |
| 90 | + :raises AtlanError: on any API communication issue |
| 91 | + """ |
| 92 | + endpoint, request_obj = OAuthClientUpdate.prepare_request( |
| 93 | + client_id, display_name, description |
| 94 | + ) |
| 95 | + raw_json = await self._client._call_api(endpoint, request_obj=request_obj) |
| 96 | + return OAuthClientUpdate.process_response(raw_json) |
| 97 | + |
| 98 | + @validate_arguments |
| 99 | + async def purge(self, client_id: str) -> None: |
| 100 | + """ |
| 101 | + Delete (purge) the specified OAuth client. |
| 102 | +
|
| 103 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 104 | + :raises AtlanError: on any API communication issue |
| 105 | + """ |
| 106 | + endpoint, _ = OAuthClientPurge.prepare_request(client_id) |
| 107 | + await self._client._call_api(endpoint) |
| 108 | + |
| 109 | + async def _fetch_available_roles(self): |
| 110 | + """ |
| 111 | + Fetch all available roles (workspace and admin-subrole levels). |
| 112 | +
|
| 113 | + :returns: list of AtlanRole objects |
| 114 | + """ |
| 115 | + filter_str = OAuthClientCreate.build_roles_filter() |
| 116 | + endpoint, query_params = RoleGet.prepare_request( |
| 117 | + limit=100, |
| 118 | + post_filter=filter_str, |
| 119 | + ) |
| 120 | + raw_json = await self._client._call_api(endpoint, query_params) |
| 121 | + response = RoleGet.process_response(raw_json) |
| 122 | + return response.records or [] |
| 123 | + |
| 124 | + @validate_arguments |
| 125 | + async def create( |
| 126 | + self, |
| 127 | + name: str, |
| 128 | + role: str, |
| 129 | + description: Optional[str] = None, |
| 130 | + persona_qualified_names: Optional[List[str]] = None, |
| 131 | + ) -> OAuthClientCreateResponse: |
| 132 | + """ |
| 133 | + Create a new OAuth client with the provided settings. |
| 134 | +
|
| 135 | + :param name: human-readable name for the OAuth client (displayed in UI) |
| 136 | + :param role: role description to assign to the OAuth client (e.g., 'Admin', 'Member'). |
| 137 | + :param description: optional explanation of the OAuth client |
| 138 | + :param persona_qualified_names: qualified names of personas to associate with the OAuth client |
| 139 | + :returns: the created OAuthClientCreateResponse (includes client_id and client_secret) |
| 140 | + :raises AtlanError: on any API communication issue |
| 141 | + :raises NotFoundError: if the specified role description is not found |
| 142 | + """ |
| 143 | + # Fetch available roles and resolve the user-provided role name |
| 144 | + available_roles = await self._fetch_available_roles() |
| 145 | + resolved_role = OAuthClientCreate.resolve_role_name(role, available_roles) |
| 146 | + |
| 147 | + # Prepare and execute the request |
| 148 | + endpoint, request_obj = OAuthClientCreate.prepare_request( |
| 149 | + display_name=name, |
| 150 | + role=resolved_role, |
| 151 | + description=description, |
| 152 | + persona_qualified_names=persona_qualified_names, |
| 153 | + ) |
| 154 | + raw_json = await self._client._call_api(endpoint, request_obj=request_obj) |
| 155 | + return OAuthClientCreate.process_response(raw_json) |
0 commit comments