66Provides duplicate AuthPolicy detection logic used by both
77PyatlanSyncTransport and PyatlanAsyncTransport.
88"""
9+
910from __future__ import annotations
1011
1112import json
2122logger = logging .getLogger (__name__ )
2223
2324
24- def build_policy_search_request (policy_name : str , persona_guid : str ) -> IndexSearchRequest :
25+ def build_policy_search_request (
26+ policy_name : str , persona_guid : str
27+ ) -> IndexSearchRequest :
2528 """Build an IndexSearchRequest to find an existing AuthPolicy by name and persona."""
2629 query = Bool (
2730 filter = [
@@ -36,7 +39,9 @@ def build_policy_search_request(policy_name: str, persona_guid: str) -> IndexSea
3639 )
3740
3841
39- def create_mock_response (existing_policy : dict , temp_guid : str = "-1" ) -> httpx .Response :
42+ def create_mock_response (
43+ existing_policy : dict , temp_guid : str = "-1"
44+ ) -> httpx .Response :
4045 """Build a mock bulk-entity response containing an already-created policy."""
4146 response_body = {
4247 "mutatedEntities" : {"CREATE" : [existing_policy ]},
@@ -59,7 +64,14 @@ def parse_auth_policy_entity(request: httpx.Request) -> Optional[tuple[str, str,
5964 if not request .content :
6065 return None
6166
62- body = json .loads (request .content .decode ("utf-8" ))
67+ try :
68+ body = json .loads (request .content .decode ("utf-8" ))
69+ except (json .JSONDecodeError , UnicodeDecodeError ):
70+ logger .debug (
71+ "parse_auth_policy_entity: failed to decode request body, skipping duplicate check"
72+ )
73+ return None
74+
6375 for entity in body .get ("entities" , []):
6476 if entity .get ("typeName" ) != "AuthPolicy" :
6577 continue
@@ -73,7 +85,9 @@ def parse_auth_policy_entity(request: httpx.Request) -> Optional[tuple[str, str,
7385 return None
7486
7587
76- def find_existing_policy (client : Any , policy_name : str , persona_guid : str ) -> Optional [dict ]:
88+ def find_existing_policy (
89+ client : Any , policy_name : str , persona_guid : str
90+ ) -> Optional [dict ]:
7791 """
7892 Search for an existing AuthPolicy by name and persona GUID (synchronous).
7993
@@ -89,7 +103,7 @@ def find_existing_policy(client: Any, policy_name: str, persona_guid: str) -> Op
89103 except Exception as e :
90104 raise ErrorCode .UNABLE_TO_SEARCH_EXISTING_POLICY .exception_with_parameters (
91105 policy_name , persona_guid , str (e )
92- )
106+ ) from e
93107
94108
95109async def find_existing_policy_async (
@@ -110,7 +124,7 @@ async def find_existing_policy_async(
110124 except Exception as e :
111125 raise ErrorCode .UNABLE_TO_SEARCH_EXISTING_POLICY .exception_with_parameters (
112126 policy_name , persona_guid , str (e )
113- )
127+ ) from e
114128
115129
116130def check_for_duplicate_policy (
@@ -159,7 +173,9 @@ async def check_for_duplicate_policy_async(
159173 return None
160174
161175 policy_name , persona_guid , temp_guid = parsed
162- existing_policy = await find_existing_policy_async (client , policy_name , persona_guid )
176+ existing_policy = await find_existing_policy_async (
177+ client , policy_name , persona_guid
178+ )
163179 if existing_policy :
164180 logger .info (
165181 f"Found existing policy '{ policy_name } ' with guid "
0 commit comments