Skip to content

Commit 1e382dc

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 09dad81 commit 1e382dc

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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
@@ -528,6 +528,48 @@ def get_all_flags(
528528
)
529529

530530

531+
def get_feature_flag_result(
532+
key,
533+
distinct_id,
534+
groups={},
535+
person_properties={},
536+
group_properties={},
537+
only_evaluate_locally=False,
538+
send_feature_flag_events=True,
539+
disable_geoip=None, # type: Optional[bool]
540+
): # type: ignore
541+
"""
542+
Get a FeatureFlagResult object which contains the flag result and payload.
543+
544+
This method evaluates a feature flag and returns a FeatureFlagResult object containing:
545+
- enabled: Whether the flag is enabled
546+
- variant: The variant value if the flag has variants
547+
- payload: The payload associated with the flag (automatically deserialized from JSON)
548+
- key: The flag key
549+
- reason: Why the flag was enabled/disabled
550+
551+
Example:
552+
```python
553+
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
554+
if result and result.enabled:
555+
# Use the variant and payload
556+
print(f"Variant: {result.variant}")
557+
print(f"Payload: {result.payload}")
558+
```
559+
"""
560+
return _proxy(
561+
"get_feature_flag_result",
562+
key=key,
563+
distinct_id=distinct_id,
564+
groups=groups,
565+
person_properties=person_properties,
566+
group_properties=group_properties,
567+
only_evaluate_locally=only_evaluate_locally,
568+
send_feature_flag_events=send_feature_flag_events,
569+
disable_geoip=disable_geoip,
570+
)
571+
572+
531573
def get_feature_flag_payload(
532574
key,
533575
distinct_id,

0 commit comments

Comments
 (0)