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

Be able to add his proper sync consumer in mixpanel initializer #114

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions mixpanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ class Mixpanel(object):
The *serializer* parameter.
"""

def __init__(self, token, consumer=None, serializer=DatetimeSerializer):
def __init__(self, token, consumer=None, serializer=DatetimeSerializer, sync_consumer=None):
self._token = token
self._consumer = consumer or Consumer()

if hasattr(self._consumer, "flush"):
self._sync_consumer = sync_consumer or Consumer()
else:
self._sync_consumer = self._consumer

self._serializer = serializer

def _now(self):
Expand Down Expand Up @@ -185,8 +191,7 @@ def alias(self, alias_id, original, meta=None):
if meta:
event.update(meta)

sync_consumer = Consumer()
sync_consumer.send('events', json_dumps(event, cls=self._serializer))
self._sync_consumer.send('events', json_dumps(event, cls=self._serializer))

def merge(self, api_key, distinct_id1, distinct_id2, meta=None, api_secret=None):
"""
Expand Down
28 changes: 9 additions & 19 deletions test_mixpanel.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -309,26 +309,16 @@ def test_people_meta(self):


class TestMixpanelIdentity(TestMixpanelBase):

def test_alias(self):
# More complicated since alias() forces a synchronous call.

with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
'https://api.mixpanel.com/track',
json={"status": 1, "error": None},
status=200,
)

self.mp.alias('ALIAS', 'ORIGINAL ID')

assert self.consumer.log == []
call = rsps.calls[0]
assert call.request.method == "POST"
assert call.request.url == "https://api.mixpanel.com/track"
posted_data = dict(urllib.parse.parse_qsl(six.ensure_str(call.request.body)))
assert json.loads(posted_data["data"]) == {"event":"$create_alias","properties":{"alias":"ALIAS","token":"12345","distinct_id":"ORIGINAL ID"}}
self.mp.alias('ALIAS', 'ORIGINAL ID')

assert self.mp._consumer == self.mp._sync_consumer
assert self.consumer.log == [
('events', {
'event': '$create_alias',
'properties': {'distinct_id': 'ORIGINAL ID', 'alias': 'ALIAS', 'token': '12345'}
})
]

def test_merge(self):
self.mp.merge('my_good_api_key', 'd1', 'd2')
Expand Down