"""Convenience wrappers for split.io client.""" from typing import Any, Dict, Optional from pdp.connectors.features import FEATURE_ON, IDENTITY_ID_ATTRIBUTE, SplitioClient IS_ENABLED_ALL_ID = "all_users" class Feature: """Base class to fetch feature flag values.""" def __init__(self, client: SplitioClient, feature_name: str) -> None: """Create a Feature object. Args: feature_name: split.io feature name """ 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 the on/off to true/false.""" def __init__(self, client: SplitioClient, feature_name: str) -> None: """Create a BooleanFeature object.""" 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. Args: identity_uuid: orchardIdentityId from JWT Returns: bool: True if feature value is 'on' """ return bool(self.get_value_for_identity(identity_uuid) == FEATURE_ON) def is_enabled(self) -> bool: """Check on/off flag value for any identity. Returns: bool: True if feature value is 'on' """ return bool(self.get_value_for_identity(IS_ENABLED_ALL_ID) == FEATURE_ON)