Skip to content

Commit

Permalink
Revert "Adding api_secret."
Browse files Browse the repository at this point in the history
This reverts commit 085012a.
  • Loading branch information
David Grant committed Dec 17, 2020
1 parent 6d19fc3 commit c03895d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
47 changes: 12 additions & 35 deletions mixpanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def track(self, distinct_id, event_name, properties=None, meta=None):
self._consumer.send('events', json_dumps(event, cls=self._serializer))

def import_data(self, api_key, distinct_id, event_name, timestamp,
properties=None, meta=None, api_secret=None):
properties=None, meta=None):
"""Record an event that occurred more than 5 days in the past.
:param str api_key: your Mixpanel project's API key
Expand All @@ -110,18 +110,13 @@ def import_data(self, api_key, distinct_id, event_name, timestamp,
:param dict properties: additional data to record; keys should be
strings, and values should be strings, numbers, or booleans
:param dict meta: overrides Mixpanel special properties
:param str api_secret: Your Mixpanel project's API secret.
To avoid accidentally recording invalid events, the Mixpanel API's
``track`` endpoint disallows events that occurred too long ago. This
method can be used to import such events. See our online documentation
for `more details
<https://developer.mixpanel.com/docs/importing-old-events>`__.
"""

if api_secret is None:
raise ValueError("api_secret is required in import calls")

all_properties = {
'token': self._token,
'distinct_id': distinct_id,
Expand All @@ -138,7 +133,6 @@ 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)

def alias(self, alias_id, original, meta=None):
Expand Down Expand Up @@ -172,23 +166,19 @@ def alias(self, alias_id, original, meta=None):
event.update(meta)
sync_consumer.send('events', json_dumps(event, cls=self._serializer))

def merge(self, api_key, distinct_id1, distinct_id2, meta=None, api_secret=None):
def merge(self, api_key, distinct_id1, distinct_id2, meta=None):
"""
Merges the two given distinct_ids.
:param str api_key: Your Mixpanel project's API key.
:param str distinct_id1: The first distinct_id to merge.
:param str distinct_id2: The second (other) distinct_id to merge.
:param dict meta: overrides Mixpanel special properties
:param str api_secret: Your Mixpanel project's API secret.
See our online documentation for `more
details
<https://developer.mixpanel.com/docs/http#merge>`__.
"""
if api_secret is None:
raise ValueError("api_secret is required in merge calls")

event = {
'event': '$merge',
'properties': {
Expand All @@ -198,7 +188,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)

def people_set(self, distinct_id, properties, meta=None):
"""Set properties of a people record.
Expand Down Expand Up @@ -535,27 +525,22 @@ def __init__(self, events_url=None, people_url=None, import_url=None,
timeout=urllib3.Timeout(request_timeout),
)

def send(self, endpoint, json_message, api_key=None, api_secret=None):
def send(self, endpoint, json_message, api_key=None):
"""Immediately record an event or a profile update.
:param endpoint: the Mixpanel API endpoint appropriate for the message
:type endpoint: "events" | "people" | "groups" | "imports"
:param str json_message: a JSON message formatted for the endpoint
:param str api_key: your Mixpanel project's API key
:param str api_secret: your Mixpanel project's API secret
: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.
"""
if endpoint not in self._endpoints:
if endpoint in self._endpoints:
self._write_request(self._endpoints[endpoint], json_message, api_key)
else:
raise MixpanelException('No such endpoint "{0}". Valid endpoints are one of {1}'.format(endpoint, self._endpoints.keys()))

self._write_request(self._endpoints[endpoint], json_message, api_key, api_secret)

def _write_request(self, request_url, json_message, api_key=None, api_secret=None):
def _write_request(self, request_url, json_message, api_key=None):
data = {
'data': json_message,
'verbose': 1,
Expand All @@ -564,17 +549,11 @@ def _write_request(self, request_url, json_message, api_key=None, api_secret=Non
if api_key:
data.update({'api_key': api_key})

headers = None

if api_secret is not None:
headers = urllib3.util.make_headers(basic_auth="{}:".format(api_secret))

try:
response = self._http.request(
'POST',
request_url,
fields=data,
headers=headers,
encode_multipart=False, # URL-encode payload in POST body.
)
except Exception as e:
Expand Down Expand Up @@ -634,7 +613,7 @@ def __init__(self, max_size=50, events_url=None, people_url=None, import_url=Non
self._max_size = min(50, max_size)
self._api_key = None

def send(self, endpoint, json_message, api_key=None, api_secret=None):
def send(self, endpoint, json_message, api_key=None):
"""Record an event or profile update.
Internally, adds the message to a buffer, and then flushes the buffer
Expand All @@ -646,7 +625,6 @@ def send(self, endpoint, json_message, api_key=None, api_secret=None):
:type endpoint: "events" | "people" | "groups" | "imports"
:param str json_message: a JSON message formatted for the endpoint
:param str api_key: your Mixpanel project's API key
:param str api_secret: your Mixpanel project's API secret
:raises MixpanelException: if the endpoint doesn't exist, the server is
unreachable, or any buffered message cannot be processed
Expand All @@ -658,9 +636,8 @@ def send(self, endpoint, json_message, api_key=None, api_secret=None):

buf = self._buffers[endpoint]
buf.append(json_message)
# Fixme: Don't stick these in the instance.
self._api_key = api_key
self._api_secret = api_secret
if api_key is not None:
self._api_key = api_key
if len(buf) >= self._max_size:
self._flush_endpoint(endpoint)

Expand All @@ -679,7 +656,7 @@ def _flush_endpoint(self, endpoint):
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, self._api_key)
except MixpanelException as orig_e:
mp_e = MixpanelException(orig_e)
mp_e.message = batch_json
Expand Down
10 changes: 4 additions & 6 deletions test_mixpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ class LogConsumer(object):
def __init__(self):
self.log = []

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


class TestMixpanel:
Expand Down

0 comments on commit c03895d

Please sign in to comment.