"""Helpers for feature flags.""" from ddex_ingester_common.constants.feature_flag import ( ENABLED ) from ddex_ingester_common.graphql.feature_flag_queries import ( FEATURE_FLAG_LABEL_QUERY, FEATURE_FLAG_QUERY ) from marshmallow.utils import get_value def is_flag_enabled(graphql_connector: object, flag_name: str) -> bool: """Check if a feature flag is enabled.""" response = graphql_connector.execute( FEATURE_FLAG_QUERY, {'prefix': flag_name} ) features = get_value( response, 'data.account.features', [] ) feature = next( (feature for feature in features if feature['feature'] == flag_name), None ) return feature and feature['value'] == ENABLED def is_flag_enabled_for_label( graphql_connector: object, flag_name: str, vendor_id: int, subaccount_id: int = 0) -> bool: """Check if a feature flag is enabled on a label level.""" response = graphql_connector.execute( FEATURE_FLAG_LABEL_QUERY, { 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'prefix': flag_name } ) features = get_value( response, 'data.orchardLabel.features', [] ) feature = next( (feature for feature in features if feature['feature'] == flag_name), None ) return feature and feature['value'] == ENABLED