Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
88 lines (68 loc) · 2.96 KB
/
Copy pathserver.py
File metadata and controls
88 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Example: Using Flagship with OpenFeature (synchronous)
Demonstrates server-side flag evaluation for Python applications
such as Django, Flask, or any synchronous server environment.
"""
from openfeature import api
from openfeature.evaluation_context import EvaluationContext
from flagship import FlagshipServerProvider, LoggingHook
FLAGSHIP_APP_ID = "your-app-id"
FLAGSHIP_ACCOUNT_ID = "your-account-id"
def main() -> None:
# 1. Set up the Flagship provider
api.set_provider(
FlagshipServerProvider(
app_id=FLAGSHIP_APP_ID,
account_id=FLAGSHIP_ACCOUNT_ID,
auth_token="your-token",
timeout=5.0,
retries=1,
logging=True,
cache_ttl=30.0, # cache evaluations per context for 30s (off by default)
)
)
# 2. Add hooks for logging (optional)
api.add_hooks([LoggingHook()])
# 3. Get a client and evaluate flags with context
client = api.get_client()
context = EvaluationContext(
targeting_key="user-123", # user identifier for targeting rules
attributes={
"email": "user@example.com",
"plan": "premium",
"country": "US",
"age": 25,
},
)
# Boolean flag
dark_mode_enabled = client.get_boolean_value("dark-mode", False, context)
print("Dark mode enabled:", dark_mode_enabled)
# String flag — e.g. for A/B testing copy
welcome_message = client.get_string_value("welcome-message", "Welcome!", context)
print("Welcome message:", welcome_message)
# Integer flag — e.g. for feature limits
max_uploads = client.get_integer_value("max-uploads", 5, context)
print("Max uploads:", max_uploads)
# Float flag — e.g. for sampling rates
sample_rate = client.get_float_value("sample-rate", 0.1, context)
print("Sample rate:", sample_rate)
# Object flag — e.g. for complex configuration
theme_config = client.get_object_value("theme-config", {"primary_color": "#000000", "font_size": 14}, context)
print("Theme config:", theme_config)
# 4. Detailed evaluation — reason reflects how the flag resolved
# ('STATIC', 'TARGETING_MATCH', 'DEFAULT', 'DISABLED', 'SPLIT')
details = client.get_boolean_details("premium-features", False, context)
print("Premium features details:")
print(" value: ", details.value)
print(" reason: ", details.reason) # why this value was served
print(" variant:", details.variant) # which variation key was matched
# 5. Non-existent flags return the default value — no exceptions raised
unknown = client.get_boolean_value("non-existent-flag", False, context)
print("Unknown flag (returns default):", unknown)
# 6. Evaluate without context — rules that don't require targeting still apply
beta_access = client.get_boolean_value("beta-access", False)
print("Beta access:", beta_access)
# 7. Shut down cleanly
api.shutdown()
if __name__ == "__main__":
main()