Skip to content

Commit 6bd8d15

Browse files
author
David Grant
authored
For subclasser compatibility, cram (key, secret) into api_key param. (#92)
* For subclasser compatibility, cram (key,secret) into api_key param. * Fix tests.
1 parent 721e41c commit 6bd8d15

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

mixpanel/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def import_data(self, api_key, distinct_id, event_name, timestamp,
152152
if meta:
153153
event.update(meta)
154154

155-
self._consumer.send('imports', json_dumps(event, cls=self._serializer), api_key, api_secret)
155+
self._consumer.send('imports', json_dumps(event, cls=self._serializer), (api_key, api_secret))
156156

157157
def alias(self, alias_id, original, meta=None):
158158
"""Creates an alias which Mixpanel will use to remap one id to another.
@@ -221,7 +221,7 @@ def merge(self, api_key, distinct_id1, distinct_id2, meta=None, api_secret=None)
221221
}
222222
if meta:
223223
event.update(meta)
224-
self._consumer.send('imports', json_dumps(event, cls=self._serializer), api_key, api_secret)
224+
self._consumer.send('imports', json_dumps(event, cls=self._serializer), (api_key, api_secret))
225225

226226
def people_set(self, distinct_id, properties, meta=None):
227227
"""Set properties of a people record.
@@ -572,7 +572,6 @@ def send(self, endpoint, json_message, api_key=None, api_secret=None):
572572
:raises MixpanelException: if the endpoint doesn't exist, the server is
573573
unreachable, or the message cannot be processed
574574
575-
576575
.. versionadded:: 4.8.0
577576
The *api_secret* parameter.
578577
"""
@@ -587,6 +586,12 @@ def _write_request(self, request_url, json_message, api_key=None, api_secret=Non
587586
'verbose': 1,
588587
'ip': 0,
589588
}
589+
590+
if isinstance(api_key, tuple):
591+
# For compatibility with subclassers, allow the auth details to be
592+
# packed into the existing api_key param.
593+
api_key, api_secret = api_key
594+
590595
if api_key:
591596
data.update({'api_key': api_key})
592597

@@ -685,6 +690,9 @@ def send(self, endpoint, json_message, api_key=None, api_secret=None):
685690
if endpoint not in self._buffers:
686691
raise MixpanelException('No such endpoint "{0}". Valid endpoints are one of {1}'.format(endpoint, self._buffers.keys()))
687692

693+
if not isinstance(api_key, tuple):
694+
api_key = (api_key, api_secret)
695+
688696
buf = self._buffers[endpoint]
689697
buf.append(json_message)
690698
# Fixme: Don't stick these in the instance.
@@ -704,11 +712,12 @@ def flush(self):
704712

705713
def _flush_endpoint(self, endpoint):
706714
buf = self._buffers[endpoint]
715+
707716
while buf:
708717
batch = buf[:self._max_size]
709718
batch_json = '[{0}]'.format(','.join(batch))
710719
try:
711-
self._consumer.send(endpoint, batch_json, self._api_key, self._api_secret)
720+
self._consumer.send(endpoint, batch_json, api_key=self._api_key)
712721
except MixpanelException as orig_e:
713722
mp_e = MixpanelException(orig_e)
714723
mp_e.message = batch_json

test_mixpanel.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717

1818
class LogConsumer(object):
19-
2019
def __init__(self):
2120
self.log = []
2221

2322
def send(self, endpoint, event, api_key=None, api_secret=None):
2423
entry = [endpoint, json.loads(event)]
25-
if api_key:
26-
entry.append(api_key)
27-
if api_secret:
28-
entry.append(api_secret)
24+
if api_key != (None, None):
25+
if api_key:
26+
entry.append(api_key)
27+
if api_secret:
28+
entry.append(api_secret)
2929
self.log.append(tuple(entry))
3030

31+
def clear(self):
32+
self.log = []
33+
3134

3235
class TestMixpanel:
3336
TOKEN = '12345'
@@ -98,8 +101,7 @@ def test_import_data(self):
98101
'$lib_version': mixpanel.__version__,
99102
},
100103
},
101-
'MY_API_KEY',
102-
'MY_SECRET',
104+
('MY_API_KEY', 'MY_SECRET'),
103105
)]
104106

105107
def test_track_meta(self):
@@ -301,7 +303,21 @@ def test_alias(self):
301303

302304
def test_merge(self):
303305
self.mp.merge('my_good_api_key', 'd1', 'd2')
306+
assert self.consumer.log == [(
307+
'imports',
308+
{
309+
'event': '$merge',
310+
'properties': {
311+
'$distinct_ids': ['d1', 'd2'],
312+
'token': self.TOKEN,
313+
}
314+
},
315+
('my_good_api_key', None),
316+
)]
317+
318+
self.consumer.clear()
304319

320+
self.mp.merge('my_good_api_key', 'd1', 'd2', api_secret='my_secret')
305321
assert self.consumer.log == [(
306322
'imports',
307323
{
@@ -311,7 +327,7 @@ def test_merge(self):
311327
'token': self.TOKEN,
312328
}
313329
},
314-
'my_good_api_key',
330+
('my_good_api_key', 'my_secret'),
315331
)]
316332

317333
def test_people_meta(self):
@@ -520,13 +536,13 @@ def test_send_remembers_api_key(self):
520536
self.consumer.send('imports', '"Event"', api_key='MY_API_KEY')
521537
assert len(self.log) == 0
522538
self.consumer.flush()
523-
assert self.log == [('imports', ['Event'], 'MY_API_KEY')]
539+
assert self.log == [('imports', ['Event'], ('MY_API_KEY', None))]
524540

525541
def test_send_remembers_api_secret(self):
526542
self.consumer.send('imports', '"Event"', api_secret='ZZZZZZ')
527543
assert len(self.log) == 0
528544
self.consumer.flush()
529-
assert self.log == [('imports', ['Event'], 'ZZZZZZ')]
545+
assert self.log == [('imports', ['Event'], (None, 'ZZZZZZ'))]
530546

531547

532548

0 commit comments

Comments
 (0)