"""Convenience wrappers for split.io client.""" from typing import Any, Dict, Optional from splitio.client.factory import ( Client as SplitioClient, ) from contributor.connectors.features.client import ( FEATURE_ON, IDENTITY_ID_ATTRIBUTE, ) IS_ENABLED_ALL_ID = "all_users" class Feature: """Base class to fetch feature flag values.""" def __init__(self, client: SplitioClient, feature_name: str) -> None: self.split_client = client self.feature_name = feature_name def get_value(self, key: str, attributes: Optional[Dict[str, Any]] = None) -> str: """Fetch feature value with attributes.""" treatment = self.split_client.get_treatment( key=key, feature_flag_name=self.feature_name, attributes=attributes, ) return str(treatment) def get_value_for_identity(self, identity_uuid: str) -> str: """Fetch feature value identity_id attribute.""" return self.get_value( key=IDENTITY_ID_ATTRIBUTE, attributes={IDENTITY_ID_ATTRIBUTE: identity_uuid}, ) class BooleanFeature(Feature): """Feature class that maps on/off to true/false.""" def __init__(self, client: SplitioClient, feature_name: str) -> None: super().__init__(client=client, feature_name=feature_name) def is_on_for_identity(self, identity_uuid: str) -> bool: """Check on/off flag value for this identity.""" return bool(self.get_value_for_identity(identity_uuid) == FEATURE_ON) def is_enabled(self) -> bool: """Check on/off flag value for a generic identity.""" return bool(self.get_value_for_identity(IS_ENABLED_ALL_ID) == FEATURE_ON)