"""Features utilities.""" from flask import g from pythonfeatures import pythonfeatures def _check_feature_enabled_by_attribute(feature, attr_key, attr_val): """Check if a feature is enabled. Args: feature (str): Feature to check attr_key (str): The attribute name to check attr_val (str): The attribute value Returns: bool """ feature_response = pythonfeatures.get_single_feature_by_attributes( feature, {attr_key: attr_val} ) message = ( feature_response.message if feature_response.status == 200 else "fetch_failed" ) enabled = message == "enabled" print( f"feature: {feature}, " f"attr_key: {attr_key}, " f"attr_val: {attr_val}, " f"enabled: {enabled}" ) return enabled def is_feature_enabled_for_account(feature, account_id): """Check if a feature is enabled for account.""" return _check_feature_enabled_by_attribute(feature, "vendor_id", account_id) def is_feature_enabled(feature): """Check if a feature is enabled for requesting user.""" return _check_feature_enabled_by_attribute( feature, "identity_id", g.request_context.identity_id )