in aws_lambda_powertools/utilities/feature_flags/feature_flags.py [0:0]
def get_enabled_features(self, *, context: Optional[Dict[str, Any]] = None) -> List[str]:
"""Get all enabled feature flags while also taking into account context
(when a feature has defined rules)
Parameters
----------
context: Optional[Dict[str, Any]]
dict of attributes that you would like to match the rules
against, can be `{'tenant_id: 'X', 'username':' 'Y', 'region': 'Z'}` etc.
Returns
----------
List[str]
list of all feature names that either matches context or have True as default
**Example**
```python
["premium_features", "my_feature_two", "always_true_feature"]
```
Raises
------
SchemaValidationError
When schema doesn't conform with feature flag schema
"""
if context is None:
context = {}
features_enabled: List[str] = []
try:
features: Dict[str, Any] = self.get_configuration()
except ConfigurationStoreError as err:
self.logger.debug(f"Failed to fetch feature flags from store, returning empty list, reason={err}")
return features_enabled
self.logger.debug("Evaluating all features")
for name, feature in features.items():
rules = feature.get(schema.RULES_KEY, {})
feature_default_value = feature.get(schema.FEATURE_DEFAULT_VAL_KEY)
boolean_feature = feature.get(
schema.FEATURE_DEFAULT_VAL_TYPE_KEY, True
) # backwards compatability ,assume feature flag
if feature_default_value and not rules:
self.logger.debug(f"feature is enabled by default and has no defined rules, name={name}")
features_enabled.append(name)
elif self._evaluate_rules(
feature_name=name,
context=context,
feat_default=feature_default_value,
rules=rules,
boolean_feature=boolean_feature,
):
self.logger.debug(f"feature's calculated value is True, name={name}")
features_enabled.append(name)
return features_enabled