Skip to content

Commit 5e70ca8

Browse files
committed
minor changes
1 parent adef8d4 commit 5e70ca8

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ jobs:
2424
2525
- name: Run posthog tests
2626
run: |
27-
python setup.py test
27+
python setup.py test

posthog/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def __init__(self, api_key=None, host=None, debug=False,
3535
require('api_key', api_key, string_types)
3636

3737
self.queue = queue.Queue(max_queue_size)
38+
39+
# api_key: This should be the Team API Key (token), public
3840
self.api_key = api_key
3941
self.on_error = on_error
4042
self.debug = debug
@@ -45,6 +47,8 @@ def __init__(self, api_key=None, host=None, debug=False,
4547
self.timeout = timeout
4648
self.feature_flags = None
4749
self.poll_interval = poll_interval
50+
51+
# personal_api_key: This should be a generated Personal API Key, private
4852
self.personal_api_key = personal_api_key
4953

5054
if debug:
@@ -230,7 +234,12 @@ def _load_feature_flags(self):
230234
self.feature_flags = get(self.personal_api_key, '/api/feature_flag/', self.host)['results']
231235
except APIError as e:
232236
if e.status == 401:
233-
raise APIError(status=401, message='You are using a write-only key with feature flags. To use feature flags, use a personal API key as api_key instead.')
237+
raise APIError(
238+
status=401,
239+
message='You are using a write-only key with feature flags. ' \
240+
'To use feature flags, please set a personal_api_key ' \
241+
'More information: https://posthog.com/docs/api/overview'
242+
)
234243

235244
self._last_feature_flag_poll = datetime.utcnow().replace(tzinfo=tzutc())
236245

posthog/request.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,21 @@ def post(api_key, host=None, gzip=False, timeout=15, **kwargs):
5454
def get(api_key, url, host=None):
5555
log = logging.getLogger('posthog')
5656
url = remove_trailing_slash(host or DEFAULT_HOST) + url
57-
request = requests.get(
57+
response = requests.get(
5858
url,
5959
headers={
6060
'Authorization': 'Bearer %s' % api_key,
6161
'User-Agent': USER_AGENT
6262
},
6363
)
64-
if request.status_code == 200:
65-
return request.json()
64+
if response.status_code == 200:
65+
return response.json()
6666
try:
67-
payload = request.json()
67+
payload = response.json()
6868
log.debug('received response: %s', payload)
69-
print(payload['detail'])
70-
raise APIError(request.status_code, payload['detail'])
69+
raise APIError(response.status_code, payload['detail'])
7170
except ValueError:
72-
raise APIError(request.status_code, res.text)
71+
raise APIError(response.status_code, response.text)
7372

7473

7574
class APIError(Exception):

posthog/test/client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
from posthog.client import Client
1010
from posthog.test.utils import TEST_API_KEY
1111

12+
from posthog.request import APIError
13+
1214
class TestClient(unittest.TestCase):
1315

1416
def set_fail(self, e, batch):
1517
"""Mark the failure handler"""
16-
print('FAILL', e, batch)
18+
print('FAIL', e, batch)
1719
self.failed = True
1820

1921
def setUp(self):
@@ -77,7 +79,7 @@ def test_basic_identify(self):
7779
self.assertTrue(success)
7880
self.assertFalse(self.failed)
7981

80-
self.assertEqual(msg['$set'], {'trait': 'value'})
82+
self.assertEqual(msg['$set']['trait'], 'value')
8183
self.assertTrue(isinstance(msg['timestamp'], str))
8284
self.assertTrue(isinstance(msg['messageId'], str))
8385
self.assertEqual(msg['distinct_id'], 'distinct_id')
@@ -92,7 +94,7 @@ def test_advanced_identify(self):
9294

9395
self.assertEqual(msg['timestamp'], '2014-09-03T00:00:00+00:00')
9496
self.assertEqual(msg['context']['ip'], '192.168.0.1')
95-
self.assertEqual(msg['$set'], {'trait': 'value'})
97+
self.assertEqual(msg['$set']['trait'], 'value')
9698
self.assertEqual(msg['properties']['$lib'], 'posthog-python')
9799
self.assertEqual(msg['properties']['$lib_version'], VERSION)
98100
self.assertTrue(isinstance(msg['timestamp'], str))
@@ -239,6 +241,11 @@ def test_load_feature_flags(self, patch_get, patch_poll):
239241
self.assertEqual(client.feature_flags[0]['key'], 'beta-feature')
240242
self.assertEqual(client._last_feature_flag_poll.isoformat(), "2020-01-01T12:01:00+00:00")
241243
self.assertEqual(patch_poll.call_count, 1)
244+
245+
def test_load_feature_flags_wrong_key(self):
246+
client = Client(TEST_API_KEY, personal_api_key=TEST_API_KEY)
247+
with freeze_time('2020-01-01T12:01:00.0000Z'):
248+
self.assertRaises(APIError, client.load_feature_flags)
242249

243250
@mock.patch('posthog.client.get')
244251
def test_feature_enabled_simple(self, patch_get):

simulator.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def failed(status, msg):
4545
raise Exception(msg)
4646

4747

48-
def track():
49-
posthog.track(options.distinct_id, options.event, anonymous_id=options.anonymousId,
48+
def capture():
49+
posthog.capture(options.distinct_id, options.event, anonymous_id=options.anonymousId,
5050
properties=json_hash(options.properties), context=json_hash(options.context))
5151

5252

@@ -55,21 +55,11 @@ def page():
5555
properties=json_hash(options.properties), context=json_hash(options.context))
5656

5757

58-
def screen():
59-
posthog.screen(options.distinct_id, name=options.name, anonymous_id=options.anonymousId,
60-
properties=json_hash(options.properties), context=json_hash(options.context))
61-
62-
6358
def identify():
6459
posthog.identify(options.distinct_id, anonymous_id=options.anonymousId,
6560
traits=json_hash(options.traits), context=json_hash(options.context))
6661

6762

68-
def group():
69-
posthog.group(options.distinct_id, options.groupId, json_hash(options.traits),
70-
json_hash(options.context), anonymous_id=options.anonymousId)
71-
72-
7363
def unknown():
7464
print()
7565

@@ -84,11 +74,9 @@ def unknown():
8474
log.addHandler(ch)
8575

8676
switcher = {
87-
"track": track,
77+
"capture": capture,
8878
"page": page,
89-
"screen": screen,
90-
"identify": identify,
91-
"group": group
79+
"identify": identify
9280
}
9381

9482
func = switcher.get(options.type)

0 commit comments

Comments
 (0)