diff --git a/README.md b/README.md index 7edef23..6d46eeb 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,28 @@ class SegmentAssignmentLogger(AssignmentLogger): client_config = Config(api_key="", assignment_logger=SegmentAssignmentLogger()) ``` +## Export configuration + +To support the use-case of needing to bootstrap a front-end client, the Eppo SDK provides a function to export flag configurations to a JSON string. + +Use the `get_flag_configurations` function to export flag configurations to a JSON string and then send it to the front-end client. + +```python +from fastapi import JSONResponse + +import eppo_client +import json + +client = eppo_client.get_instance() +flag_configurations = client.get_flag_configurations() + +# Convert flag configurations to a JSON string +flag_config_json = json.dumps(flag_configurations) + +# Create a JSONResponse object with the stringified JSON +response = JSONResponse(content={"flagConfigurations": flag_config_json}) +``` + ## Philosophy Eppo's SDKs are built for simplicity, speed and reliability. Flag configurations are compressed and distributed over a global CDN (Fastly), typically reaching your servers in under 15ms. Server SDKs continue polling Eppo’s API at 30-second intervals. Configurations are then cached locally, ensuring that each assignment is made instantly. Evaluation logic within each SDK consists of a few lines of simple numeric and string comparisons. The typed functions listed above are all developers need to understand, abstracting away the complexity of the Eppo's underlying (and expanding) feature set. diff --git a/eppo_client/client.py b/eppo_client/client.py index 640482a..cc7852c 100644 --- a/eppo_client/client.py +++ b/eppo_client/client.py @@ -10,6 +10,7 @@ ContextAttributes, ActionContexts, ) +from eppo_client.models import Flag from eppo_client.configuration_requestor import ( ExperimentConfigurationRequestor, ) @@ -386,6 +387,13 @@ def get_flag_keys(self): """ return self.__config_requestor.get_flag_keys() + def get_flag_configurations(self) -> Dict[str, Flag]: + """ + Returns a dictionary of all flag configurations that have been initialized. + This can be useful to debug the initialization process or to bootstrap a front-end client. + """ + return self.__config_requestor.get_flag_configurations() + def get_bandit_keys(self): """ Returns a list of all bandit keys that have been initialized. diff --git a/eppo_client/configuration_requestor.py b/eppo_client/configuration_requestor.py index 7dbb966..a0e5d96 100644 --- a/eppo_client/configuration_requestor.py +++ b/eppo_client/configuration_requestor.py @@ -36,6 +36,9 @@ def get_bandit_model(self, bandit_key: str) -> Optional[BanditData]: def get_flag_keys(self): return self.__flag_config_store.get_keys() + def get_flag_configurations(self): + return self.__flag_config_store.get_configurations() + def get_bandit_keys(self): return self.__bandit_config_store.get_keys() diff --git a/eppo_client/configuration_store.py b/eppo_client/configuration_store.py index 87b7be5..475e884 100644 --- a/eppo_client/configuration_store.py +++ b/eppo_client/configuration_store.py @@ -21,3 +21,7 @@ def set_configurations(self, configs: Dict[str, T]): def get_keys(self): with self.__lock.reader(): return set(self.__cache.keys()) + + def get_configurations(self): + with self.__lock.reader(): + return self.__cache diff --git a/eppo_client/version.py b/eppo_client/version.py index 1da6a55..88c513e 100644 --- a/eppo_client/version.py +++ b/eppo_client/version.py @@ -1 +1 @@ -__version__ = "3.2.1" +__version__ = "3.3.0" diff --git a/test/client_test.py b/test/client_test.py index a260c88..9d340dc 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -218,6 +218,13 @@ def test_client_has_flags(): assert len(client.get_flag_keys()) > 0, "No flags have been loaded by the client" +def test_client_flag_configurations(): + client = get_instance() + assert ( + len(client.get_flag_configurations()) > 0 + ), "No flags have been loaded by the client" + + @pytest.mark.parametrize("test_case", test_data) def test_assign_subject_in_sample(test_case): client = get_instance() diff --git a/test/configuration_store_test.py b/test/configuration_store_test.py index 3bda8d0..5f79a7d 100644 --- a/test/configuration_store_test.py +++ b/test/configuration_store_test.py @@ -34,6 +34,13 @@ def test_get_keys(): assert len(keys) == 2 +def test_get_configurations(): + config = {"flag1": mock_flag, "flag2": mock_flag} + store.set_configurations(config) + configurations = store.get_configurations() + assert configurations == config + + def test_evicts_old_entries_when_max_size_exceeded(): store.set_configurations({"item_to_be_evicted": mock_flag}) assert store.get_configuration("item_to_be_evicted") == mock_flag