Skip to content

Commit fd92502

Browse files
authored
Add support for project_api_key. (#32)
* add support for project_api_key * rm keys * lint
1 parent fe6d0dc commit fd92502

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import posthog
77

88
# You can find this key on the /setup page in PostHog
9-
posthog.api_key = ""
9+
posthog.project_api_key = ""
1010
posthog.personal_api_key = ""
1111

1212
# Where you host PostHog, with no trailing /.

posthog/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
sync_mode = False # type: bool
1515
disabled = False # type: bool
1616
personal_api_key = None # type: str
17+
project_api_key = None # type: str
1718

1819
default_client = None
1920

2021

2122
def capture(
22-
distinct_id, # type: str,
23-
event, # type: str,
23+
distinct_id, # type: str
24+
event, # type: str
2425
properties=None, # type: Optional[Dict]
2526
context=None, # type: Optional[Dict]
2627
timestamp=None, # type: Optional[datetime.datetime]
@@ -252,6 +253,7 @@ def _proxy(method, *args, **kwargs):
252253
send=send,
253254
sync_mode=sync_mode,
254255
personal_api_key=personal_api_key,
256+
project_api_key=project_api_key,
255257
)
256258

257259
fn = getattr(default_client, method)

posthog/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
self.queue = queue.Queue(max_queue_size)
5353

5454
# api_key: This should be the Team API Key (token), public
55-
self.api_key = api_key or project_api_key
55+
self.api_key = project_api_key or api_key
5656

5757
require("api_key", self.api_key, string_types)
5858

@@ -88,7 +88,7 @@ def __init__(
8888
self.consumers = []
8989
consumer = Consumer(
9090
self.queue,
91-
api_key,
91+
self.api_key,
9292
host=host,
9393
on_error=on_error,
9494
flush_at=flush_at,

posthog/consumer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def fatal_exception(exc):
124124
# retry on server errors and client errors
125125
# with 429 status code (rate limited),
126126
# don't retry on other client errors
127+
if exc.status == "N/A":
128+
return False
127129
return (400 <= exc.status < 500) and exc.status != 429
128130
else:
129131
# retry on all other errors (eg. network)

posthog/test/test_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def test_basic_capture(self):
4343
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
4444
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
4545

46+
def test_basic_capture_with_project_api_key(self):
47+
48+
client = Client(project_api_key=TEST_API_KEY, on_error=self.set_fail)
49+
50+
success, msg = client.capture("distinct_id", "python test event")
51+
client.flush()
52+
self.assertTrue(success)
53+
self.assertFalse(self.failed)
54+
55+
self.assertEqual(msg["event"], "python test event")
56+
self.assertTrue(isinstance(msg["timestamp"], str))
57+
self.assertTrue(isinstance(msg["messageId"], str))
58+
self.assertEqual(msg["distinct_id"], "distinct_id")
59+
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
60+
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
61+
4662
def test_stringifies_distinct_id(self):
4763
# A large number that loses precision in node:
4864
# node -e "console.log(157963456373623802 + 1)" > 157963456373623800
@@ -324,6 +340,14 @@ def test_feature_enabled_simple(self, patch_get):
324340
]
325341
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
326342

343+
@mock.patch("posthog.client.get")
344+
def test_feature_enabled_simple_with_project_api_key(self, patch_get):
345+
client = Client(project_api_key=TEST_API_KEY, on_error=self.set_fail)
346+
client.feature_flags = [
347+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": 100}
348+
]
349+
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
350+
327351
@mock.patch("posthog.client.decide")
328352
def test_feature_enabled_request(self, patch_decide):
329353
patch_decide.return_value = {"featureFlags": ["beta-feature"]}

0 commit comments

Comments
 (0)