Skip to content

Commit 1777b70

Browse files
authored
fix: Personal api key not required (#56)
* fix: Personal api key not required * formatting * Fix false
1 parent 565bb8a commit 1777b70

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
[![PyPI](https://img.shields.io/pypi/v/posthog)](https://pypi.org/project/posthog/)
44

5-
Please see the main [PostHog docs](https://posthog.com/docs).
65

7-
Specifically, the [Python integration](https://posthog.com/docs/integrations/python-integration) details.
6+
Please see the [Python integration docs](https://posthog.com/docs/integrations/python-integration) for details.
87

98
## Development
109

@@ -14,6 +13,7 @@ Specifically, the [Python integration](https://posthog.com/docs/integrations/pyt
1413
2. Run `source env/bin/activate` (activates the virtual environment)
1514
3. Run `python3 -m pip install -e ".[test]"` (installs the package in develop mode, along with test dependencies)
1615
4. Run `make test`
16+
1. To run a specific test do `pytest -k test_no_api_key`
1717

1818
### Running Locally
1919

posthog/client.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -354,39 +354,36 @@ def feature_enabled(self, key, distinct_id, default=False, *, groups={}):
354354
require("distinct_id", distinct_id, ID_TYPES)
355355
require("groups", groups, dict)
356356

357-
if not self.personal_api_key:
358-
self.log.warning("[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.")
359-
if not self.feature_flags:
357+
if self.feature_flags == None and self.personal_api_key:
360358
self.load_feature_flags()
359+
response = None
361360

362361
# If loading in previous line failed
363-
if not self.feature_flags:
364-
response = default
365-
else:
362+
if self.feature_flags:
366363
for flag in self.feature_flags:
367364
if flag["key"] == key:
368365
feature_flag = flag
369-
break
370-
else:
371-
return default
372-
373-
if feature_flag.get("is_simple_flag"):
374-
response = _hash(key, distinct_id) <= ((feature_flag.get("rollout_percentage", 100) or 100) / 100)
366+
if feature_flag.get("is_simple_flag"):
367+
response = _hash(key, distinct_id) <= (feature_flag.get("rollout_percentage", 100) / 100)
368+
if response == None:
369+
try:
370+
request_data = {
371+
"distinct_id": distinct_id,
372+
"personal_api_key": self.personal_api_key,
373+
"groups": groups,
374+
}
375+
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
376+
except Exception as e:
377+
response = default
378+
self.log.warning(
379+
"[FEATURE FLAGS] Unable to get data for flag %s, because of the following error:" % key
380+
)
381+
self.log.warning(e)
375382
else:
376-
try:
377-
request_data = {
378-
"distinct_id": distinct_id,
379-
"personal_api_key": self.personal_api_key,
380-
"groups": groups,
381-
}
382-
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
383-
response = key in resp_data["featureFlags"]
384-
except Exception as e:
385-
response = default
386-
self.log.warning(
387-
"[FEATURE FLAGS] Unable to get data for flag %s, because of the following error:" % key
388-
)
389-
self.log.warning(e)
383+
if key in resp_data["featureFlags"]:
384+
return True
385+
else:
386+
return default
390387

391388
self.capture(distinct_id, "$feature_flag_called", {"$feature_flag": key, "$feature_flag_response": response})
392389
return response

posthog/test/test_client.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,25 @@ def test_load_feature_flags_wrong_key(self):
404404
with freeze_time("2020-01-01T12:01:00.0000Z"):
405405
self.assertRaises(APIError, client.load_feature_flags)
406406

407+
@mock.patch("posthog.client.decide")
407408
@mock.patch("posthog.client.get")
408-
def test_feature_enabled_simple(self, patch_get):
409+
def test_feature_enabled_simple(self, patch_get, patch_decide):
409410
client = Client(TEST_API_KEY)
410411
client.feature_flags = [
411412
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": 100}
412413
]
413414
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
415+
self.assertEqual(patch_decide.call_count, 0)
416+
417+
@mock.patch("posthog.client.decide")
418+
@mock.patch("posthog.client.get")
419+
def test_feature_enabled_simple_is_false(self, patch_get, patch_decide):
420+
client = Client(TEST_API_KEY)
421+
client.feature_flags = [
422+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": 0}
423+
]
424+
self.assertFalse(client.feature_enabled("beta-feature", "distinct_id"))
425+
self.assertEqual(patch_decide.call_count, 0)
414426

415427
@mock.patch("posthog.client.get")
416428
def test_feature_enabled_simple_with_project_api_key(self, patch_get):
@@ -444,22 +456,24 @@ def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get):
444456
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
445457

446458
@mock.patch("posthog.client.Poller")
447-
@mock.patch("posthog.client.get")
448-
def test_feature_enabled_doesnt_exist(self, patch_get, patch_poll):
459+
@mock.patch("posthog.client.decide")
460+
def test_feature_enabled_doesnt_exist(self, patch_decide, patch_poll):
461+
patch_decide.return_value = {"featureFlags": []}
449462
client = Client(TEST_API_KEY, personal_api_key="test")
450463
client.feature_flags = []
451464

452465
self.assertFalse(client.feature_enabled("doesnt-exist", "distinct_id"))
453466
self.assertTrue(client.feature_enabled("doesnt-exist", "distinct_id", True))
454467

455468
@mock.patch("posthog.client.Poller")
456-
@mock.patch("posthog.client.get")
457-
def test_personal_api_key_doesnt_exist(self, patch_get, patch_poll):
469+
@mock.patch("posthog.client.decide")
470+
def test_personal_api_key_doesnt_exist(self, patch_decide, patch_poll):
458471
client = Client(TEST_API_KEY)
459472
client.feature_flags = []
460473

461-
self.assertFalse(client.feature_enabled("doesnt-exist", "distinct_id"))
462-
self.assertTrue(client.feature_enabled("doesnt-exist", "distinct_id", True))
474+
patch_decide.return_value = {"featureFlags": ["feature-flag"]}
475+
476+
self.assertTrue(client.feature_enabled("feature-flag", "distinct_id"))
463477

464478
@mock.patch("posthog.client.Poller")
465479
@mock.patch("posthog.client.get")

0 commit comments

Comments
 (0)