@@ -94,7 +94,9 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
9494 self .attribute_key_map : dict [str , entities .Attribute ] = self ._generate_key_map (
9595 self .attributes , 'key' , entities .Attribute
9696 )
97-
97+ self .attribute_id_to_key_map : dict [str , str ] = {}
98+ for attribute in self .attributes :
99+ self .attribute_id_to_key_map [attribute ['id' ]] = attribute ['key' ]
98100 self .audience_id_map : dict [str , entities .Audience ] = self ._generate_key_map (
99101 self .audiences , 'id' , entities .Audience
100102 )
@@ -510,6 +512,34 @@ def get_attribute_id(self, attribute_key: str) -> Optional[str]:
510512 self .error_handler .handle_error (exceptions .InvalidAttributeException (enums .Errors .INVALID_ATTRIBUTE ))
511513 return None
512514
515+ def get_attribute_by_key (self , key : str ) -> Optional [entities .Attribute ]:
516+ """ Get attribute for the provided attribute key.
517+
518+ Args:
519+ key: Attribute key for which attribute is to be fetched.
520+
521+ Returns:
522+ Attribute corresponding to the provided attribute key.
523+ """
524+ if key in self .attribute_key_map :
525+ return self .attribute_key_map [key ]
526+ self .logger .error (f'Attribute with key:"{ key } " is not in datafile.' )
527+ return None
528+
529+ def get_attribute_key_by_id (self , id : str ) -> Optional [str ]:
530+ """ Get attribute key for the provided attribute id.
531+
532+ Args:
533+ id: Attribute id for which attribute is to be fetched.
534+
535+ Returns:
536+ Attribute key corresponding to the provided attribute id.
537+ """
538+ if id in self .attribute_id_to_key_map :
539+ return self .attribute_id_to_key_map [id ]
540+ self .logger .error (f'Attribute with id:"{ id } " is not in datafile.' )
541+ return None
542+
513543 def get_feature_from_key (self , feature_key : str ) -> Optional [entities .FeatureFlag ]:
514544 """ Get feature for the provided feature key.
515545
0 commit comments