|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2025 Atlan Pte. Ltd. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import Dict, Optional |
| 7 | + |
| 8 | +from pyatlan.client.constants import ( |
| 9 | + DELETE_OAUTH_CLIENT, |
| 10 | + GET_OAUTH_CLIENT_BY_ID, |
| 11 | + GET_OAUTH_CLIENTS, |
| 12 | + UPDATE_OAUTH_CLIENT, |
| 13 | +) |
| 14 | +from pyatlan.model.oauth_clients import OAuthClient, OAuthClientResponse |
| 15 | + |
| 16 | + |
| 17 | +class OAuthClientGetAll: |
| 18 | + """Shared logic for getting all OAuth clients without pagination.""" |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def prepare_request() -> tuple: |
| 22 | + """ |
| 23 | + Prepare the request for getting all OAuth clients. |
| 24 | +
|
| 25 | + :returns: tuple of (endpoint, query_params) |
| 26 | + """ |
| 27 | + return GET_OAUTH_CLIENTS.format_path_with_params(), None |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def process_response(raw_json: Dict) -> OAuthClientResponse: |
| 31 | + """ |
| 32 | + Process the API response into an OAuthClientResponse object. |
| 33 | +
|
| 34 | + :param raw_json: raw response from the API |
| 35 | + :returns: OAuthClientResponse with pagination info and records |
| 36 | + """ |
| 37 | + return OAuthClientResponse(**raw_json) |
| 38 | + |
| 39 | + |
| 40 | +class OAuthClientGet: |
| 41 | + """Shared logic for getting OAuth clients with pagination.""" |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def prepare_request( |
| 45 | + limit: Optional[int] = None, |
| 46 | + offset: int = 0, |
| 47 | + sort: Optional[str] = None, |
| 48 | + ) -> tuple: |
| 49 | + """ |
| 50 | + Prepare the request for getting OAuth clients with pagination. |
| 51 | +
|
| 52 | + :param limit: maximum number of results to be returned |
| 53 | + :param offset: starting point for results to return, for paging |
| 54 | + :param sort: property by which to sort the results (e.g., 'createdAt' for descending) |
| 55 | + :returns: tuple of (endpoint, query_params) |
| 56 | + """ |
| 57 | + query_params: Dict[str, str] = { |
| 58 | + "count": "true", |
| 59 | + "offset": str(offset), |
| 60 | + } |
| 61 | + if limit is not None: |
| 62 | + query_params["limit"] = str(limit) |
| 63 | + if sort is not None: |
| 64 | + query_params["sort"] = sort |
| 65 | + |
| 66 | + return GET_OAUTH_CLIENTS.format_path_with_params(), query_params |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def process_response(raw_json: Dict) -> OAuthClientResponse: |
| 70 | + """ |
| 71 | + Process the API response into an OAuthClientResponse object. |
| 72 | +
|
| 73 | + :param raw_json: raw response from the API |
| 74 | + :returns: OAuthClientResponse with pagination info and records |
| 75 | + """ |
| 76 | + return OAuthClientResponse(**raw_json) |
| 77 | + |
| 78 | + |
| 79 | +class OAuthClientGetById: |
| 80 | + """Shared logic for getting an OAuth client by its client ID.""" |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def prepare_request(client_id: str) -> tuple: |
| 84 | + """ |
| 85 | + Prepare the request for getting an OAuth client by its client ID. |
| 86 | +
|
| 87 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 88 | + :returns: tuple of (endpoint, query_params) |
| 89 | + """ |
| 90 | + endpoint = GET_OAUTH_CLIENT_BY_ID.format_path( |
| 91 | + {"client_id": client_id} |
| 92 | + ).format_path_with_params() |
| 93 | + return endpoint, None |
| 94 | + |
| 95 | + @staticmethod |
| 96 | + def process_response(raw_json: Dict) -> OAuthClient: |
| 97 | + """ |
| 98 | + Process the API response into an OAuthClient object. |
| 99 | +
|
| 100 | + :param raw_json: raw response from the API |
| 101 | + :returns: OAuthClient object |
| 102 | + """ |
| 103 | + return OAuthClient(**raw_json) |
| 104 | + |
| 105 | + |
| 106 | +class OAuthClientUpdate: |
| 107 | + """Shared logic for updating OAuth clients.""" |
| 108 | + |
| 109 | + @staticmethod |
| 110 | + def prepare_request( |
| 111 | + client_id: str, |
| 112 | + display_name: Optional[str] = None, |
| 113 | + description: Optional[str] = None, |
| 114 | + ) -> tuple: |
| 115 | + """ |
| 116 | + Prepare the request for updating an OAuth client. |
| 117 | +
|
| 118 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 119 | + :param display_name: human-readable name for the OAuth client |
| 120 | + :param description: optional explanation of the OAuth client |
| 121 | + :returns: tuple of (endpoint, request_dict) |
| 122 | + """ |
| 123 | + # Build request dict with only non-None values to avoid sending null fields |
| 124 | + request_dict: Dict[str, str] = {} |
| 125 | + if display_name is not None: |
| 126 | + request_dict["displayName"] = display_name |
| 127 | + if description is not None: |
| 128 | + request_dict["description"] = description |
| 129 | + |
| 130 | + endpoint = UPDATE_OAUTH_CLIENT.format_path( |
| 131 | + {"client_id": client_id} |
| 132 | + ).format_path_with_params() |
| 133 | + return endpoint, request_dict |
| 134 | + |
| 135 | + @staticmethod |
| 136 | + def process_response(raw_json: Dict) -> OAuthClient: |
| 137 | + """ |
| 138 | + Process the API response into an OAuthClient. |
| 139 | +
|
| 140 | + :param raw_json: raw response from the API |
| 141 | + :returns: the updated OAuthClient |
| 142 | + """ |
| 143 | + return OAuthClient(**raw_json) |
| 144 | + |
| 145 | + |
| 146 | +class OAuthClientPurge: |
| 147 | + """Shared logic for deleting OAuth clients.""" |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def prepare_request(client_id: str) -> tuple: |
| 151 | + """ |
| 152 | + Prepare the request for deleting an OAuth client. |
| 153 | +
|
| 154 | + :param client_id: unique client identifier (e.g., 'oauth-client-xxx') |
| 155 | + :returns: tuple of (endpoint, None) |
| 156 | + """ |
| 157 | + endpoint = DELETE_OAUTH_CLIENT.format_path( |
| 158 | + {"client_id": client_id} |
| 159 | + ).format_path_with_params() |
| 160 | + return endpoint, None |
0 commit comments