Skip to content

Commit

Permalink
For subclasser compatibility, cram (key, secret) into api_key param. (#…
Browse files Browse the repository at this point in the history
…92)

* For subclasser compatibility, cram (key,secret) into api_key param.

* Fix tests.
  • Loading branch information
David Grant authored Jan 5, 2021
1 parent 721e41c commit 6bd8d15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
17 changes: 13 additions & 4 deletions mixpanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def import_data(self, api_key, distinct_id, event_name, timestamp,
if meta:
event.update(meta)

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

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

def people_set(self, distinct_id, properties, meta=None):
"""Set properties of a people record.
Expand Down Expand Up @@ -572,7 +572,6 @@ def send(self, endpoint, json_message, api_key=None, api_secret=None):
:raises MixpanelException: if the endpoint doesn't exist, the server is
unreachable, or the message cannot be processed
.. versionadded:: 4.8.0
The *api_secret* parameter.
"""
Expand All @@ -587,6 +586,12 @@ def _write_request(self, request_url, json_message, api_key=None, api_secret=Non
'verbose': 1,
'ip': 0,
}

if isinstance(api_key, tuple):
# For compatibility with subclassers, allow the auth details to be
# packed into the existing api_key param.
api_key, api_secret = api_key

if api_key:
data.update({'api_key': api_key})

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

if not isinstance(api_key, tuple):
api_key = (api_key, api_secret)

buf = self._buffers[endpoint]
buf.append(json_message)
# Fixme: Don't stick these in the instance.
Expand All @@ -704,11 +712,12 @@ def flush(self):

def _flush_endpoint(self, endpoint):
buf = self._buffers[endpoint]

while buf:
batch = buf[:self._max_size]
batch_json = '[{0}]'.format(','.join(batch))
try:
self._consumer.send(endpoint, batch_json, self._api_key, self._api_secret)
self._consumer.send(endpoint, batch_json, api_key=self._api_key)
except MixpanelException as orig_e:
mp_e = MixpanelException(orig_e)
mp_e.message = batch_json
Expand Down
36 changes: 26 additions & 10 deletions test_mixpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@


class LogConsumer(object):

def __init__(self):
self.log = []

def send(self, endpoint, event, api_key=None, api_secret=None):
entry = [endpoint, json.loads(event)]
if api_key:
entry.append(api_key)
if api_secret:
entry.append(api_secret)
if api_key != (None, None):
if api_key:
entry.append(api_key)
if api_secret:
entry.append(api_secret)
self.log.append(tuple(entry))

def clear(self):
self.log = []


class TestMixpanel:
TOKEN = '12345'
Expand Down Expand Up @@ -98,8 +101,7 @@ def test_import_data(self):
'$lib_version': mixpanel.__version__,
},
},
'MY_API_KEY',
'MY_SECRET',
('MY_API_KEY', 'MY_SECRET'),
)]

def test_track_meta(self):
Expand Down Expand Up @@ -301,7 +303,21 @@ def test_alias(self):

def test_merge(self):
self.mp.merge('my_good_api_key', 'd1', 'd2')
assert self.consumer.log == [(
'imports',
{
'event': '$merge',
'properties': {
'$distinct_ids': ['d1', 'd2'],
'token': self.TOKEN,
}
},
('my_good_api_key', None),
)]

self.consumer.clear()

self.mp.merge('my_good_api_key', 'd1', 'd2', api_secret='my_secret')
assert self.consumer.log == [(
'imports',
{
Expand All @@ -311,7 +327,7 @@ def test_merge(self):
'token': self.TOKEN,
}
},
'my_good_api_key',
('my_good_api_key', 'my_secret'),
)]

def test_people_meta(self):
Expand Down Expand Up @@ -520,13 +536,13 @@ def test_send_remembers_api_key(self):
self.consumer.send('imports', '"Event"', api_key='MY_API_KEY')
assert len(self.log) == 0
self.consumer.flush()
assert self.log == [('imports', ['Event'], 'MY_API_KEY')]
assert self.log == [('imports', ['Event'], ('MY_API_KEY', None))]

def test_send_remembers_api_secret(self):
self.consumer.send('imports', '"Event"', api_secret='ZZZZZZ')
assert len(self.log) == 0
self.consumer.flush()
assert self.log == [('imports', ['Event'], 'ZZZZZZ')]
assert self.log == [('imports', ['Event'], (None, 'ZZZZZZ'))]



Expand Down

0 comments on commit 6bd8d15

Please sign in to comment.