from typing import Any from splitio.client.factory import Client, SplitFactory, get_factory IDENTITY_ID_ATTRIBUTE = "identity_id" FEATURE_ON = "on" # Supported features AUDIENCE_ALLOW_AUDIENCE_EXPORT_TO_FILE = "audience_allow_audience_export_to_file" AUDIENCE_SHOW_ADD_SECONDARY_FANS_TOGGLE = "audience_enable_secondary_fans_toggle" AUDIENCE_SHOW_QA_FAN_SEGMENTATION_MODEL = "audience_show_qa_fan_segmentation_model" AUDIENCE_ALLOW_AUDIENCE_SHARING_TO_ALL_AD_ACCOUNTS = ( "audience_allow_audience_sharing_to_all_ad_accounts" ) AUDIENCE_SHOW_SMS_CAMPAIGNS_PAGE = "audience_show_sms_campaigns_page" class Features: def __init__( self, api_key: str, block_until_ready_timeout: int = 60, config: dict[str, Any] | None = None, ) -> None: config = config or {} self.block_until_ready_timeout = block_until_ready_timeout self._factory = get_factory(api_key, config=config) self._client: Client | None = None @property def factory(self) -> SplitFactory: if self._factory is None: raise RuntimeError("SplitioFactory not started.") return self._factory @property def client(self) -> Client: """Get the Splitio Client.""" if self._client is None: raise RuntimeError("SplitioClient not started.") return self._client def get_treatment( self, key: str, feature_flag_name: str, attributes: dict[str, Any] | None = None ) -> str: """Get the value of a feature flag for a given key.""" treatment = self.client.get_treatment( key=key, feature_flag_name=feature_flag_name, attributes=attributes ) return str(treatment) def is_on_for_identity(self, feature_flag_name: str, *, identity_id: str) -> bool: """Check if a feature is on for an identity.""" treatment = self.get_treatment( key=IDENTITY_ID_ATTRIBUTE, feature_flag_name=feature_flag_name, attributes={IDENTITY_ID_ATTRIBUTE: identity_id}, ) return treatment == FEATURE_ON def start(self) -> None: """Start the SDK.""" self.factory.block_until_ready(self.block_until_ready_timeout) self._client = self.factory.client() def close(self) -> None: """Close the SDK.""" self._client = None