Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for project_api_key. #32

Merged
merged 3 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 /.
Expand Down
6 changes: 4 additions & 2 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions posthog/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]}
Expand Down