11# SPDX-License-Identifier: Apache-2.0
2- # Copyright 2024 Atlan Pte. Ltd.
2+ # Copyright 2025 Atlan Pte. Ltd.
33from __future__ import annotations
44
55import logging
66import threading
7- from typing import Dict , Optional , Union
7+ from typing import TYPE_CHECKING , Optional , Union
88
99from pyatlan .cache .abstract_asset_cache import AbstractAssetCache , AbstractAssetName
10- from pyatlan .client .atlan import AtlanClient
1110from pyatlan .model .assets import Asset , Connection
1211from pyatlan .model .enums import AtlanConnectorType
1312from pyatlan .model .fluent_search import FluentSearch
1413from pyatlan .model .search import Term
1514
16- LOGGER = logging .getLogger (__name__ )
15+ if TYPE_CHECKING :
16+ from pyatlan .client .atlan import AtlanClient
1717
1818lock = threading .Lock ()
19+ LOGGER = logging .getLogger (__name__ )
1920
2021
2122class ConnectionCache (AbstractAssetCache ):
@@ -37,24 +38,11 @@ class ConnectionCache(AbstractAssetCache):
3738 Connection .CONNECTOR_NAME ,
3839 ]
3940 SEARCH_ATTRIBUTES = [field .atlan_field_name for field in _SEARCH_FIELDS ]
40- caches : Dict [int , ConnectionCache ] = dict ()
4141
4242 def __init__ (self , client : AtlanClient ):
4343 super ().__init__ (client )
4444
45- @classmethod
46- def get_cache (cls ) -> ConnectionCache :
47- from pyatlan .client .atlan import AtlanClient
48-
49- with lock :
50- default_client = AtlanClient .get_default_client ()
51- cache_key = default_client .cache_key
52- if cache_key not in cls .caches :
53- cls .caches [cache_key ] = ConnectionCache (client = default_client )
54- return cls .caches [cache_key ]
55-
56- @classmethod
57- def get_by_guid (cls , guid : str , allow_refresh : bool = True ) -> Connection :
45+ def get_by_guid (self , guid : str , allow_refresh : bool = True ) -> Connection :
5846 """
5947 Retrieve a connection from the cache by its UUID.
6048 If the asset is not found, it will be looked up and added to the cache.
@@ -66,11 +54,10 @@ def get_by_guid(cls, guid: str, allow_refresh: bool = True) -> Connection:
6654 :raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
6755 :raises InvalidRequestError: if no UUID was provided for the connection to retrieve
6856 """
69- return cls . get_cache () ._get_by_guid (guid = guid , allow_refresh = allow_refresh )
57+ return self ._get_by_guid (guid = guid , allow_refresh = allow_refresh )
7058
71- @classmethod
7259 def get_by_qualified_name (
73- cls , qualified_name : str , allow_refresh : bool = True
60+ self , qualified_name : str , allow_refresh : bool = True
7461 ) -> Connection :
7562 """
7663 Retrieve a connection from the cache by its unique Atlan-internal name.
@@ -84,13 +71,12 @@ def get_by_qualified_name(
8471 :raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
8572 :raises InvalidRequestError: if no qualified_name was provided for the connection to retrieve
8673 """
87- return cls . get_cache () ._get_by_qualified_name (
74+ return self ._get_by_qualified_name (
8875 qualified_name = qualified_name , allow_refresh = allow_refresh
8976 )
9077
91- @classmethod
9278 def get_by_name (
93- cls , name : ConnectionName , allow_refresh : bool = True
79+ self , name : ConnectionName , allow_refresh : bool = True
9480 ) -> Connection :
9581 """
9682 Retrieve an connection from the cache by its uniquely identifiable name.
@@ -104,7 +90,7 @@ def get_by_name(
10490 :raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
10591 :raises InvalidRequestError: if no name was provided for the connection to retrieve
10692 """
107- return cls . get_cache () ._get_by_name (name = name , allow_refresh = allow_refresh )
93+ return self ._get_by_name (name = name , allow_refresh = allow_refresh )
10894
10995 def lookup_by_guid (self , guid : str ) -> None :
11096 if not guid :
@@ -139,21 +125,22 @@ def lookup_by_qualified_name(self, connection_qn: str) -> None:
139125 def lookup_by_name (self , name : ConnectionName ) -> None :
140126 if not isinstance (name , ConnectionName ):
141127 return
142- results = self .client .asset .find_connections_by_name (
143- name = name .name ,
144- connector_type = name .type ,
145- attributes = self .SEARCH_ATTRIBUTES ,
146- )
147- if not results :
148- return
149- if len (results ) > 1 :
150- LOGGER .warning (
151- (
152- "Found multiple connections of the same type with the same name, caching only the first: %s"
153- ),
154- name ,
128+ with self .lock :
129+ results = self .client .asset .find_connections_by_name (
130+ name = name .name , # type: ignore[arg-type]
131+ connector_type = name .type , # type: ignore[arg-type]
132+ attributes = self .SEARCH_ATTRIBUTES ,
155133 )
156- self .cache (results [0 ])
134+ if not results :
135+ return
136+ if len (results ) > 1 :
137+ LOGGER .warning (
138+ (
139+ "Found multiple connections of the same type with the same name, caching only the first: %s"
140+ ),
141+ name ,
142+ )
143+ self .cache (results [0 ])
157144
158145 def get_name (self , asset : Asset ):
159146 if not isinstance (asset , Connection ):
@@ -188,7 +175,7 @@ def __init__(
188175 elif isinstance (connection , str ):
189176 tokens = connection .split ("/" )
190177 if len (tokens ) > 1 :
191- self .type = AtlanConnectorType (tokens [0 ]) # type: ignore[call-arg]
178+ self .type = AtlanConnectorType (tokens [0 ]). value # type: ignore[call-arg]
192179 self .name = connection [len (tokens [0 ]) + 1 :] # noqa
193180
194181 def __hash__ (self ):
0 commit comments