From 9e9e3f4f41e97f26db8bcd2fb59edb85e1acea3b Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 18 May 2021 14:08:22 +0100 Subject: [PATCH 1/3] add support for project_api_key --- example.py | 4 ++-- posthog/__init__.py | 6 ++++-- posthog/client.py | 4 ++-- posthog/consumer.py | 2 ++ posthog/test/test_client.py | 24 ++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/example.py b/example.py index 7121430..3495e64 100644 --- a/example.py +++ b/example.py @@ -6,8 +6,8 @@ import posthog # You can find this key on the /setup page in PostHog -posthog.api_key = "" -posthog.personal_api_key = "" +posthog.project_api_key = "LXP6nQXvo-2TCqGVrWvPah8uJIyVykoMmhnEkEBi5PA" +posthog.personal_api_key = "PU18e4m2KOcc3iNjpKBBz439B8jyL0IYlvg3jcWptJk" # Where you host PostHog, with no trailing /. # You can remove this line if you're using posthog.com diff --git a/posthog/__init__.py b/posthog/__init__.py index 0894139..1f5d63f 100644 --- a/posthog/__init__.py +++ b/posthog/__init__.py @@ -14,13 +14,14 @@ sync_mode = False # type: bool disabled = False # type: bool personal_api_key = None # type: str +project_api_key = None # type: str default_client = None def capture( - distinct_id, # type: str, - event, # type: str, + distinct_id, # type: str + event, # type: str properties=None, # type: Optional[Dict] context=None, # type: Optional[Dict] timestamp=None, # type: Optional[datetime.datetime] @@ -252,6 +253,7 @@ def _proxy(method, *args, **kwargs): send=send, sync_mode=sync_mode, personal_api_key=personal_api_key, + project_api_key=project_api_key, ) fn = getattr(default_client, method) diff --git a/posthog/client.py b/posthog/client.py index 4a58631..a416b7d 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -52,7 +52,7 @@ def __init__( self.queue = queue.Queue(max_queue_size) # api_key: This should be the Team API Key (token), public - self.api_key = api_key or project_api_key + self.api_key = project_api_key or api_key require("api_key", self.api_key, string_types) @@ -88,7 +88,7 @@ def __init__( self.consumers = [] consumer = Consumer( self.queue, - api_key, + self.api_key, host=host, on_error=on_error, flush_at=flush_at, diff --git a/posthog/consumer.py b/posthog/consumer.py index e5e4acf..5e403e8 100644 --- a/posthog/consumer.py +++ b/posthog/consumer.py @@ -124,6 +124,8 @@ def fatal_exception(exc): # retry on server errors and client errors # with 429 status code (rate limited), # don't retry on other client errors + if exc.status == "N/A": + return False return (400 <= exc.status < 500) and exc.status != 429 else: # retry on all other errors (eg. network) diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 9c867a0..3c9ea4a 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -43,6 +43,22 @@ def test_basic_capture(self): self.assertEqual(msg["properties"]["$lib"], "posthog-python") self.assertEqual(msg["properties"]["$lib_version"], VERSION) + def test_basic_capture_with_project_api_key(self): + + client = Client(project_api_key=TEST_API_KEY, on_error=self.set_fail) + + success, msg = client.capture("distinct_id", "python test event") + client.flush() + self.assertTrue(success) + self.assertFalse(self.failed) + + self.assertEqual(msg["event"], "python test event") + self.assertTrue(isinstance(msg["timestamp"], str)) + self.assertTrue(isinstance(msg["messageId"], str)) + self.assertEqual(msg["distinct_id"], "distinct_id") + self.assertEqual(msg["properties"]["$lib"], "posthog-python") + self.assertEqual(msg["properties"]["$lib_version"], VERSION) + def test_stringifies_distinct_id(self): # A large number that loses precision in node: # node -e "console.log(157963456373623802 + 1)" > 157963456373623800 @@ -324,6 +340,14 @@ def test_feature_enabled_simple(self, patch_get): ] self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) + @mock.patch("posthog.client.get") + def test_feature_enabled_simple_with_project_api_key(self, patch_get): + client = Client(project_api_key=TEST_API_KEY, on_error=self.set_fail) + client.feature_flags = [ + {"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": 100} + ] + self.assertTrue(client.feature_enabled("beta-feature", "distinct_id")) + @mock.patch("posthog.client.decide") def test_feature_enabled_request(self, patch_decide): patch_decide.return_value = {"featureFlags": ["beta-feature"]} From 7176db8a65a976856ed06efd1c9dadbad77a4305 Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 18 May 2021 14:09:21 +0100 Subject: [PATCH 2/3] rm keys --- example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example.py b/example.py index 3495e64..08d64e6 100644 --- a/example.py +++ b/example.py @@ -6,8 +6,8 @@ import posthog # You can find this key on the /setup page in PostHog -posthog.project_api_key = "LXP6nQXvo-2TCqGVrWvPah8uJIyVykoMmhnEkEBi5PA" -posthog.personal_api_key = "PU18e4m2KOcc3iNjpKBBz439B8jyL0IYlvg3jcWptJk" +posthog.project_api_key = "" +posthog.personal_api_key = "" # Where you host PostHog, with no trailing /. # You can remove this line if you're using posthog.com From 57897096cdb557abcecf8124e3e3e415ec7a7823 Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 18 May 2021 14:17:48 +0100 Subject: [PATCH 3/3] lint --- posthog/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/__init__.py b/posthog/__init__.py index 1f5d63f..be488ce 100644 --- a/posthog/__init__.py +++ b/posthog/__init__.py @@ -14,7 +14,7 @@ sync_mode = False # type: bool disabled = False # type: bool personal_api_key = None # type: str -project_api_key = None # type: str +project_api_key = None # type: str default_client = None