Flagbase Python Server SDK
Project description
Flagbase Python Server SDK
Flagbase is a feature flag management system that helps you manage your application features easily and safely. The Python Server SDK enables developers to integrate Flagbase with their Python applications and control feature flag variations for different users.
Installation
To install the Flagbase Python Server SDK, you can use pip:
pip install flagbase
Usage
Importing the SDK
First, import the necessary components from the flagbase package:
from flagbase import FlagbaseClient, Config, Identity
Creating a Flagbase Client
Create a FlagbaseClient instance with the appropriate configuration:
flagbase = FlagbaseClient(
config=Config(
# Make sure you use the server SDK key (not the client SDK key)
# Also be sure not to expose this secret, as anybody would be
# able to use it to get your flag rules from Flagbase
server_key="sdk-server_491e7607-dac2-41dc-abed-1ba904cdb032"
)
)
User Identity
Create a user identity object with an identifier and traits:
# Replace 'user_or_request_id' with a unique identifier
# This is essentially what you will use to split your traffic on
user_or_request_id = ...
user = Identity(user_or_request_id, {"some-trait-key": "blue"})
Evaluating Feature Variations
Evaluate the feature variation for a specific user:
feature_variation = flagbase.variation("some-flag-key", user, "control")
Example Application
Here's an example of a simple application that uses the Flagbase Python Server SDK:
from flagbase import FlagbaseClient, Config, Identity
if __name__ == '__main__':
flagbase = FlagbaseClient(
config=Config(
server_key="sdk-server_491e7607-dac2-41dc-abed-1ba904cdb032",
)
)
user_or_request_id = ...
# user details might be pulled from your database
user = Identity(
user_or_request_id,
{"some-trait-key": "blue"}
)
show_feature = flagbase.variation("example-flag", user, "control")
if show_feature == "treatment":
show_my_new_feature()
elif show_feature == "control":
show_existing_feature()
In this example, the application creates a Flagbase client, user identity, and evaluates the feature variation for that particular user. If the user/requestor belongs to the "treatment" cohort, then we show them the new feature. Otherwise we show them the existing feature.
Advanced
Event Listeners
The Flagbase Python Server SDK provides event listeners to help you monitor and react to events happening within the SDK. This can be useful for debugging, monitoring, and managing your application's performance.
Usage
Adding Event Listeners
You can add event listeners to the Flagbase client by using the on method:
def my_listener(message: str, context: Any):
print(f"Received event: {message}")
flagbase.on(EventType.EVALUATION, my_listener)
In this example, my_listener is a function that prints the event message when the event is triggered. You can replace it with your own custom function to handle the events.
Removing Event Listeners
You can remove an event listener by using the off
method:
flagbase.off(EventType.EVALUATION, my_listener)
This will remove the my_listener
function from listening to the CLIENT_READY
event.
Event Types
The available event types are:
NETWORK_FETCH
: A network fetch has been initiated.NETWORK_FETCH_FULL
: A full network fetch has been completed.NETWORK_FETCH_CACHED
: A cached network fetch has been completed.NETWORK_FETCH_ERROR
: An error occurred during a network fetch.EVALUATION
: An evaluation event has been triggered.
Log Event - Event Types:
Use these event types to listen in to different types of log events. Based on standard python logging levels: https://docs.python.org/3/library/logging.html#logging-levels
LOG_CRITICAL
: This event type represents a critical error that occurred in the code, which could lead to a crash or other serious issue that requires immediate attention.LOG_ERROR
: This event type represents a non-critical error that occurred in the code, which may not require immediate attention but should be logged and monitored.LOG_WARNING
: This event type represents a warning message that occurred in the code, which indicates a potential issue or error that should be investigated.LOG_INFO
: This event type represents an informational message that occurred in the code, which may be useful for monitoring or debugging purposes.LOG_DEBUG
: This event type represents a debug message that occurred in the code, which provides additional information for debugging and troubleshooting purposes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flagbase-0.0.8.tar.gz
.
File metadata
- Download URL: flagbase-0.0.8.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ee7cb3833b873049070f61d98d29b2ccde025585bdd59bd8a3cbed09a547927 |
|
MD5 | 4ba9ceda1b57313901c3b599c695cd3f |
|
BLAKE2b-256 | 5ccbceaeaf260133984406d609bf58494cdcba21f372386e061cbdadd6df9abd |
File details
Details for the file flagbase-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: flagbase-0.0.8-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f618338c24cea1524ae3a9e0b3df16298fc54d202e7b83922a06eac4dd8a8e2e |
|
MD5 | 55a72413f8d2cd27c7f35d3ce0b2b421 |
|
BLAKE2b-256 | 594da4f9ed3b9e36b1ab00937299461ab8845520eab1fc4076e5643e8752d4f3 |