Skip to content

Commit 11b3841

Browse files
committed
Expose get_feature_flag_result method in public API
This exposes the existing get_feature_flag_result method that returns a FeatureFlagResult object containing enabled, variant, and payload properties. The payload is automatically deserialized from JSON. Fixes #226
1 parent c61236b commit 11b3841

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

example.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
# get payload
6060
print(posthog.get_feature_flag_payload("beta-feature", "distinct_id"))
6161
print(posthog.get_all_flags_and_payloads("distinct_id"))
62+
63+
# get feature flag result with all details (enabled, variant, payload)
64+
result = posthog.get_feature_flag_result("beta-feature", "distinct_id")
65+
if result:
66+
print(f"Flag enabled: {result.enabled}")
67+
print(f"Variant: {result.variant}")
68+
print(f"Payload: {result.payload}")
69+
6270
exit()
6371
# # Alias a previous distinct id with a new one
6472

posthog/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,48 @@ def get_all_flags(
352352
)
353353

354354

355+
def get_feature_flag_result(
356+
key,
357+
distinct_id,
358+
groups={},
359+
person_properties={},
360+
group_properties={},
361+
only_evaluate_locally=False,
362+
send_feature_flag_events=True,
363+
disable_geoip=None, # type: Optional[bool]
364+
): # type: ignore
365+
"""
366+
Get a FeatureFlagResult object which contains the flag result and payload.
367+
368+
This method evaluates a feature flag and returns a FeatureFlagResult object containing:
369+
- enabled: Whether the flag is enabled
370+
- variant: The variant value if the flag has variants
371+
- payload: The payload associated with the flag (automatically deserialized from JSON)
372+
- key: The flag key
373+
- reason: Why the flag was enabled/disabled
374+
375+
Example:
376+
```python
377+
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
378+
if result and result.enabled:
379+
# Use the variant and payload
380+
print(f"Variant: {result.variant}")
381+
print(f"Payload: {result.payload}")
382+
```
383+
"""
384+
return _proxy(
385+
"get_feature_flag_result",
386+
key=key,
387+
distinct_id=distinct_id,
388+
groups=groups,
389+
person_properties=person_properties,
390+
group_properties=group_properties,
391+
only_evaluate_locally=only_evaluate_locally,
392+
send_feature_flag_events=send_feature_flag_events,
393+
disable_geoip=disable_geoip,
394+
)
395+
396+
355397
def get_feature_flag_payload(
356398
key,
357399
distinct_id,

0 commit comments

Comments
 (0)