Skip to content

Commit 306fb2a

Browse files
utkuzihUtku Zihnioglutimgl
authored
Feature: Enable multi variate feature flags for Python library (#60)
* Capturing $feature_flag_called at the end of client.feature_enabled method * Enabling multi-variants for feature flags Co-authored-by: Utku Zihnioglu <[email protected]> Co-authored-by: Tim Glaser <[email protected]>
1 parent faffd1f commit 306fb2a

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

posthog/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ def feature_enabled(self, key, distinct_id, default=False, *, groups={}):
380380
)
381381
self.log.warning(e)
382382
else:
383-
response = True if key in resp_data["featureFlags"] else default
384-
383+
response = resp_data["featureFlags"].get(key, default)
385384
self.capture(distinct_id, "$feature_flag_called", {"$feature_flag": key, "$feature_flag_response": response})
386385
return response
387386

posthog/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _process_response(
6868

6969
def decide(api_key: str, host: Optional[str] = None, gzip: bool = False, timeout: int = 15, **kwargs) -> Any:
7070
"""Post the `kwargs to the decide API endpoint"""
71-
res = post(api_key, host, "/decide/", gzip, timeout, **kwargs)
71+
res = post(api_key, host, "/decide/?v=2", gzip, timeout, **kwargs)
7272
return _process_response(res, success_message="Feature flags decided successfully")
7373

7474

posthog/test/test_client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,22 @@ def test_feature_enabled_simple_with_project_api_key(self, patch_get):
434434

435435
@mock.patch("posthog.client.decide")
436436
def test_feature_enabled_request(self, patch_decide):
437-
patch_decide.return_value = {"featureFlags": ["beta-feature"]}
437+
patch_decide.return_value = {"featureFlags": {"beta-feature": True}}
438438
client = Client(TEST_API_KEY)
439439
client.feature_flags = [
440440
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
441441
]
442442
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
443443

444+
@mock.patch("posthog.client.decide")
445+
def test_feature_enabled_request_multi_variate(self, patch_decide):
446+
patch_decide.return_value = {"featureFlags": {"beta-feature": "variant-1"}}
447+
client = Client(TEST_API_KEY)
448+
client.feature_flags = [
449+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
450+
]
451+
self.assertEqual(client.feature_enabled("beta-feature", "distinct_id"), "variant-1")
452+
444453
@mock.patch("posthog.client.get")
445454
def test_feature_enabled_simple_without_rollout_percentage(self, patch_get):
446455
client = Client(TEST_API_KEY)
@@ -458,7 +467,7 @@ def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get):
458467
@mock.patch("posthog.client.Poller")
459468
@mock.patch("posthog.client.decide")
460469
def test_feature_enabled_doesnt_exist(self, patch_decide, patch_poll):
461-
patch_decide.return_value = {"featureFlags": []}
470+
patch_decide.return_value = {"featureFlags": {}}
462471
client = Client(TEST_API_KEY, personal_api_key="test")
463472
client.feature_flags = []
464473

@@ -471,7 +480,7 @@ def test_personal_api_key_doesnt_exist(self, patch_decide, patch_poll):
471480
client = Client(TEST_API_KEY)
472481
client.feature_flags = []
473482

474-
patch_decide.return_value = {"featureFlags": ["feature-flag"]}
483+
patch_decide.return_value = {"featureFlags": {"feature-flag": True}}
475484

476485
self.assertTrue(client.feature_enabled("feature-flag", "distinct_id"))
477486

0 commit comments

Comments
 (0)