2020logger = getLogger (__name__ )
2121
2222
23- class Alias :
23+ class AliasResolver :
2424 """Class for handling aliases.
2525
2626 The purpose of this is class is to ensure that alias storages are created with correct id. This is achieved by using
@@ -42,9 +42,9 @@ def __init__(
4242 ) -> None :
4343 self ._storage_type = storage_type
4444 self ._alias = alias
45- self ._additional_cache_key = self . get_additional_cache_key (configuration )
45+ self ._additional_cache_key = hash_api_base_url_and_token (configuration )
4646
47- async def __aenter__ (self ) -> Alias :
47+ async def __aenter__ (self ) -> AliasResolver :
4848 """Context manager to prevent race condition in alias creation."""
4949 lock = await self ._get_alias_init_lock ()
5050 await lock .acquire ()
@@ -60,26 +60,26 @@ async def __aexit__(
6060 async def _get_alias_init_lock (cls ) -> Lock :
6161 """Get lock for controlling the creation of the alias storages.
6262
63- The lock is shared for all instances of the Alias class.
63+ The lock is shared for all instances of the AliasResolver class.
6464 It is created in async method to ensure that some event loop is already running.
6565 """
6666 if cls ._alias_init_lock is None :
6767 cls ._alias_init_lock = Lock ()
6868 return cls ._alias_init_lock
6969
7070 @classmethod
71- async def get_alias_map (cls ) -> dict [str , str ]:
71+ async def _get_alias_map (cls ) -> dict [str , str ]:
7272 """Get the aliases and storage ids mapping from the default kvs.
7373
74- Mapping is loaded from kvs only once and is shared for all instances of the Alias class.
74+ Mapping is loaded from kvs only once and is shared for all instances of the AliasResolver class.
7575
7676 Returns:
7777 Map of aliases and storage ids.
7878 """
7979 if not cls ._alias_map :
80- default_kvs_client = await get_default_kvs_client ()
80+ default_kvs_client = await _get_default_kvs_client ()
8181
82- record = await default_kvs_client .get_record (cls .ALIAS_MAPPING_KEY )
82+ record = await default_kvs_client .get_record (cls ._ALIAS_MAPPING_KEY )
8383
8484 # get_record can return {key: ..., value: ..., content_type: ...}
8585 if isinstance (record , dict ):
@@ -92,24 +92,14 @@ async def get_alias_map(cls) -> dict[str, str]:
9292
9393 return cls ._alias_map
9494
95- @classmethod
96- def get_additional_cache_key (cls , configuration : Configuration ) -> str :
97- """Get additional cache key based on configuration.
98-
99- Use only api_public_base_url and token as the relevant for differentiating storages.
100- """
101- if configuration .api_public_base_url is None or configuration .token is None :
102- raise ValueError ("'Configuration.api_public_base_url' and 'Configuration.token' must be set." )
103- return compute_short_hash (f'{ configuration .api_public_base_url } { configuration .token } ' .encode ())
104-
10595 @property
106- def storage_key (self ) -> str :
96+ def _storage_key (self ) -> str :
10797 """Get a unique storage key used for storing the alias in the mapping."""
108- return self .ALIAS_STORAGE_KEY_SEPARATOR .join (
98+ return self ._ALIAS_STORAGE_KEY_SEPARATOR .join (
10999 [
110- self .storage_type .__name__ ,
111- self .alias ,
112- self .additional_cache_key ,
100+ self ._storage_type .__name__ ,
101+ self ._alias ,
102+ self ._additional_cache_key ,
113103 ]
114104 )
115105
@@ -121,38 +111,38 @@ async def resolve_id(self) -> str | None:
121111 Returns:
122112 Storage id if it exists, None otherwise.
123113 """
124- return (await self .get_alias_map ()).get (self .storage_key , None )
114+ return (await self ._get_alias_map ()).get (self ._storage_key , None )
125115
126116 async def store_mapping (self , storage_id : str ) -> None :
127117 """Add alias and related storage id to the mapping in default kvs and local in-memory mapping."""
128118 # Update in-memory mapping
129- (await self .get_alias_map ())[self .storage_key ] = storage_id
119+ (await self ._get_alias_map ())[self ._storage_key ] = storage_id
130120 if not Configuration .get_global_configuration ().is_at_home :
131121 logging .getLogger (__name__ ).warning (
132- 'Alias storage limited retention is only supported on Apify platform. Storage is not exported.'
122+ 'AliasResolver storage limited retention is only supported on Apify platform. Storage is not exported.'
133123 )
134124 return
135125
136- default_kvs_client = await get_default_kvs_client ()
126+ default_kvs_client = await _get_default_kvs_client ()
137127 await default_kvs_client .get ()
138128
139129 try :
140- record = await default_kvs_client .get_record (self .ALIAS_MAPPING_KEY )
130+ record = await default_kvs_client .get_record (self ._ALIAS_MAPPING_KEY )
141131
142132 # get_record can return {key: ..., value: ..., content_type: ...}
143133 if isinstance (record , dict ) and 'value' in record :
144134 record = record ['value' ]
145135
146136 # Update or create the record with the new alias mapping
147137 if isinstance (record , dict ):
148- record [self .storage_key ] = storage_id
138+ record [self ._storage_key ] = storage_id
149139 else :
150- record = {self .storage_key : storage_id }
140+ record = {self ._storage_key : storage_id }
151141
152142 # Store the mapping back in the KVS.
153- await default_kvs_client .set_record (self .ALIAS_MAPPING_KEY , record )
143+ await default_kvs_client .set_record (self ._ALIAS_MAPPING_KEY , record )
154144 except Exception as exc :
155- logger .warning (f'Error storing alias mapping for { self .alias } : { exc } ' )
145+ logger .warning (f'Error storing alias mapping for { self ._alias } : { exc } ' )
156146
157147
158148async def _get_default_kvs_client () -> KeyValueStoreClientAsync :
@@ -168,3 +158,10 @@ async def _get_default_kvs_client() -> KeyValueStoreClientAsync:
168158 )
169159
170160 return apify_client_async .key_value_store (key_value_store_id = configuration .default_key_value_store_id )
161+
162+
163+ def hash_api_base_url_and_token (configuration : Configuration ) -> str :
164+ """Hash configuration.api_public_base_url and configuration.token in deterministic way."""
165+ if configuration .api_public_base_url is None or configuration .token is None :
166+ raise ValueError ("'Configuration.api_public_base_url' and 'Configuration.token' must be set." )
167+ return compute_short_hash (f'{ configuration .api_public_base_url } { configuration .token } ' .encode ())
0 commit comments