diff --git a/example.py b/example.py index 71214305..08d64e63 100644 --- a/example.py +++ b/example.py @@ -6,7 +6,7 @@ import posthog # You can find this key on the /setup page in PostHog -posthog.api_key = "" +posthog.project_api_key = "" posthog.personal_api_key = "" # Where you host PostHog, with no trailing /. diff --git a/posthog/__init__.py b/posthog/__init__.py index 0894139d..be488ced 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 4a586310..a416b7d2 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 e5e4acfd..5e403e86 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 9c867a0c..3c9ea4a6 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"]}