diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3a6685b --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +API_CLIENT="" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8e808a3..2351f12 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ confidence_openfeature_provider.egg-info/ dist/ build/ confidence/_version.py - +.env diff --git a/README.md b/README.md index 424d3a1..b68bb1d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # Python Confidence SDK -Python library for [Confidence](https://confidence.spotify.com/). +This repo contains the [Confidence](https://confidence.spotify.com/) Python SDK and the Confidence OpenFeature provider. We recommend using the [OpenFeature Python SDK](https://github.com/open-feature/python-sdk) to access Confidence feature flags. + + +To learn more about the basic concepts (flags, targeting key, evaluation contexts), +the [OpenFeature reference documentation](https://openfeature.dev/docs/reference/intro) or the [getting started guide](https://openfeature.dev/docs/tutorials/getting-started/python) can be useful. ## Install @@ -20,18 +24,31 @@ pip install -r requirements.txt ## Usage -### Resolving flags +### Creating the SDK -Flag values are evaluated remotely and returned to the application: +The OpenFeature provider is created by using the SDK. More details on how to setup the SDK can be found [here](#configuration-options). ```python from confidence.confidence import Confidence +from openfeature import api +from confidence.openfeature_provider import ConfidenceOpenFeatureProvider + +provider = ConfidenceOpenFeatureProvider(Confidence(api_client, timeout_ms=500)) +api.set_provider(provider) +``` + +### Resolving flags + +Flag values are evaluated remotely and returned to the application by using the OpenFeature `client`: + +```python +from openfeature import api -root_confidence = Confidence("CLIENT_TOKEN") -confidence = root_confidence.with_context({"user_id": "some-user-id"}) -default_value = False -flag_details = confidence.resolve_boolean_details("flag-name.property-name", default_value) -print(flag_details) +client = api.get_client() +user_identifier = "" # your identifier for a specific user + +flag_value = client.get_string_value("flag-name.property-name", "my-default-value", api.EvaluationContext(attributes={"user_id": user_identifier})) +print(f"Flag value: {flag_value}") ``` ### Configuration options @@ -41,35 +58,14 @@ The SDK can be configured with several options: ```python from confidence.confidence import Confidence, Region -# Configure timeout for network requests confidence = Confidence( client_secret="CLIENT_TOKEN", region=Region.EU, # Optional: defaults to GLOBAL - timeout_ms=5000 # Optional: specify timeout in milliseconds for network requests (default: 10000ms) + timeout_ms=5000, # Optional: specify timeout in milliseconds for network requests (default: 10000ms) + custom_resolve_base_url="https://my-custom-endpoint.org" # we will append /v1/flags:resolve to this for the resolve endpoint. ) ``` -### Tracking events - -Events are emitted to the Confidence backend: - -```python -confidence.track("event_name", { - "field_1": False -}) -``` - -## OpenFeature - -The library includes a `Provider` for -the [OpenFeature Python SDK](https://openfeature.dev/docs/tutorials/getting-started/python), that can be -used to resolve feature flag values from the Confidence platform. - -To learn more about the basic concepts (flags, targeting key, evaluation contexts), -the [OpenFeature reference documentation](https://openfeature.dev/docs/reference/intro) can be -useful. - - ## Logging The SDK includes built-in logging functionality to help with debugging and monitoring. By default, the SDK creates a logger named `confidence_logger` that outputs to the console with DEBUG level logging enabled. diff --git a/demo.py b/demo.py index f1700a7..277038d 100644 --- a/demo.py +++ b/demo.py @@ -1,37 +1,22 @@ -import asyncio import uuid +import os from confidence.confidence import Confidence +from openfeature import api +from confidence.openfeature_provider import ConfidenceOpenFeatureProvider +from dotenv import load_dotenv +def get_flag(): + api_client = os.getenv("API_CLIENT") + api.set_provider(ConfidenceOpenFeatureProvider(Confidence(api_client, timeout_ms=100))) -async def get_flag(): - root = Confidence("API CLIENT", timeout_ms=100) - random_uuid = uuid.uuid4() - uuid_string = str(random_uuid) - confidence = root.with_context({"targeting_key": uuid_string}) - # confidence.with_context({"app": "python"}).track("navigate", {}) - # print("Tracked navigate event") + random_uuid = str(uuid.uuid4()) + client = api.get_client() - details = confidence.resolve_string_details("hawkflag.color", "default") - print(f"Flag value: {details.value}") - print(f"Flag reason: {details.reason}") - print(f"Flag error code: {details.error_code}") - print(f"Flag error message: {details.error_message}") + flag_value = client.get_string_value("hawkflag.color", "default", api.EvaluationContext(attributes={"visitor_id": random_uuid})) + print(f"Flag value: {flag_value}") -# Another asynchronous function that calls the first one -async def main(): - await get_flag() - print("Finished calling get_flag") - await asyncio.sleep(1) - print("Finished sleeping for 1 seconds") - - -def sync_main(): - print("Running main asynchronously") - - -# Run the main function using asyncio.run (Python 3.7+) if __name__ == "__main__": - asyncio.run(main()) - sync_main() + load_dotenv() + get_flag() diff --git a/pyproject.toml b/pyproject.toml index 3ece8f8..ed08cff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,8 @@ dev = [ "flake8==6.1.0", "requests_mock==1.11.0", "setuptools_scm==8.0.4", - "mypy==1.5.1" + "mypy==1.5.1", + "python-dotenv==1.0.1" ] [project.urls]