Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_CLIENT="<CLIENT SECRET FROM CONFIDENCE CONSOLE>"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ confidence_openfeature_provider.egg-info/
dist/
build/
confidence/_version.py

.env
58 changes: 27 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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.
Expand Down
41 changes: 13 additions & 28 deletions demo.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down