File size: 7,736 Bytes
5980447
1
2
{"repo": "mixpanel/mixpanel-python", "pull_number": 64, "instance_id": "mixpanel__mixpanel-python-64", "issue_numbers": ["63"], "base_commit": "40c98e0b285898384cc4aa6cc803d8d0f46f6218", "patch": "diff --git a/mixpanel/__init__.py b/mixpanel/__init__.py\n--- a/mixpanel/__init__.py\n+++ b/mixpanel/__init__.py\n@@ -345,6 +345,7 @@ def send(self, endpoint, json_message, api_key=None):\n         :param endpoint: the Mixpanel API endpoint appropriate for the message\n         :type endpoint: \"events\" | \"people\" | \"imports\"\n         :param str json_message: a JSON message formatted for the endpoint\n+        :param str api_key: your Mixpanel project's API key\n         :raises MixpanelException: if the endpoint doesn't exist, the server is\n             unreachable, or the message cannot be processed\n         \"\"\"\n@@ -412,6 +413,7 @@ def __init__(self, max_size=50, events_url=None, people_url=None, import_url=Non\n             'imports': [],\n         }\n         self._max_size = min(50, max_size)\n+        self._api_key = None\n \n     def send(self, endpoint, json_message, api_key=None):\n         \"\"\"Record an event or profile update.\n@@ -424,16 +426,22 @@ def send(self, endpoint, json_message, api_key=None):\n         :param endpoint: the Mixpanel API endpoint appropriate for the message\n         :type endpoint: \"events\" | \"people\" | \"imports\"\n         :param str json_message: a JSON message formatted for the endpoint\n+        :param str api_key: your Mixpanel project's API key\n         :raises MixpanelException: if the endpoint doesn't exist, the server is\n             unreachable, or any buffered message cannot be processed\n+\n+        .. versionadded:: 4.3.2\n+            The *api_key* parameter.\n         \"\"\"\n         if endpoint not in self._buffers:\n             raise MixpanelException('No such endpoint \"{0}\". Valid endpoints are one of {1}'.format(endpoint, self._buffers.keys()))\n \n         buf = self._buffers[endpoint]\n         buf.append(json_message)\n+        if api_key is not None:\n+            self._api_key = api_key\n         if len(buf) >= self._max_size:\n-            self._flush_endpoint(endpoint, api_key)\n+            self._flush_endpoint(endpoint)\n \n     def flush(self):\n         \"\"\"Immediately send all buffered messages to Mixpanel.\n@@ -444,13 +452,13 @@ def flush(self):\n         for endpoint in self._buffers.keys():\n             self._flush_endpoint(endpoint)\n \n-    def _flush_endpoint(self, endpoint, api_key=None):\n+    def _flush_endpoint(self, endpoint):\n         buf = self._buffers[endpoint]\n         while buf:\n             batch = buf[:self._max_size]\n             batch_json = '[{0}]'.format(','.join(batch))\n             try:\n-                self._consumer.send(endpoint, batch_json, api_key)\n+                self._consumer.send(endpoint, batch_json, self._api_key)\n             except MixpanelException as orig_e:\n                 mp_e = MixpanelException(orig_e)\n                 mp_e.message = batch_json\n", "test_patch": "diff --git a/test_mixpanel.py b/test_mixpanel.py\n--- a/test_mixpanel.py\n+++ b/test_mixpanel.py\n@@ -353,40 +353,32 @@ class TestBufferedConsumer:\n     def setup_class(cls):\n         cls.MAX_LENGTH = 10\n         cls.consumer = mixpanel.BufferedConsumer(cls.MAX_LENGTH)\n-        cls.mock = Mock()\n-        cls.mock.read.return_value = six.b('{\"status\":1, \"error\": null}')\n+        cls.consumer._consumer = LogConsumer()\n+        cls.log = cls.consumer._consumer.log\n \n-    def test_buffer_hold_and_flush(self):\n-        with patch('six.moves.urllib.request.urlopen', return_value=self.mock) as urlopen:\n-            self.consumer.send('events', '\"Event\"')\n-            assert not self.mock.called\n-            self.consumer.flush()\n+    def setup_method(self):\n+        del self.log[:]\n \n-            assert urlopen.call_count == 1\n-\n-            (call_args, kwargs) = urlopen.call_args\n-            (request,) = call_args\n-            timeout = kwargs.get('timeout', None)\n-\n-            assert request.get_full_url() == 'https://api.mixpanel.com/track'\n-            assert qs(request.data) == qs('ip=0&data=WyJFdmVudCJd&verbose=1')\n-            assert timeout is None\n+    def test_buffer_hold_and_flush(self):\n+        self.consumer.send('events', '\"Event\"')\n+        assert len(self.log) == 0\n+        self.consumer.flush()\n+        assert self.log == [('events', ['Event'])]\n \n     def test_buffer_fills_up(self):\n-        with patch('six.moves.urllib.request.urlopen', return_value=self.mock) as urlopen:\n-            for i in range(self.MAX_LENGTH - 1):\n-                self.consumer.send('events', '\"Event\"')\n-                assert not self.mock.called\n-\n-            self.consumer.send('events', '\"Last Event\"')\n+        for i in range(self.MAX_LENGTH - 1):\n+            self.consumer.send('events', '\"Event\"')\n+        assert len(self.log) == 0\n \n-            assert urlopen.call_count == 1\n-            ((request,), _) = urlopen.call_args\n-            assert request.get_full_url() == 'https://api.mixpanel.com/track'\n-            assert qs(request.data) == \\\n-                qs('ip=0&data=WyJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJFdmVudCIsIkV2ZW50IiwiRXZlbnQiLCJMYXN0IEV2ZW50Il0%3D&verbose=1')\n+        self.consumer.send('events', '\"Last Event\"')\n+        assert len(self.log) == 1\n+        assert self.log == [('events', [\n+            'Event', 'Event', 'Event', 'Event', 'Event',\n+            'Event', 'Event', 'Event', 'Event', 'Last Event',\n+        ])]\n \n-    def test_unknown_endpoint(self):\n+    def test_unknown_endpoint_raises_on_send(self):\n+        # Ensure the exception isn't hidden until a flush.\n         with pytest.raises(mixpanel.MixpanelException):\n             self.consumer.send('unknown', '1')\n \n@@ -394,17 +386,19 @@ def test_useful_reraise_in_flush_endpoint(self):\n         error_mock = Mock()\n         error_mock.read.return_value = six.b('{\"status\": 0, \"error\": \"arbitrary error\"}')\n         broken_json = '{broken JSON'\n+        consumer = mixpanel.BufferedConsumer(2)\n         with patch('six.moves.urllib.request.urlopen', return_value=error_mock):\n-            self.consumer.send('events', broken_json)\n+            consumer.send('events', broken_json)\n             with pytest.raises(mixpanel.MixpanelException) as excinfo:\n-                self.consumer.flush()\n+                consumer.flush()\n             assert excinfo.value.message == '[%s]' % broken_json\n             assert excinfo.value.endpoint == 'events'\n \n-    def test_import_data_receives_api_key(self):\n-        # Ensure BufferedConsumer.send accepts the API_KEY parameter needed for\n-        # import_data; see #62.\n+    def test_send_remembers_api_key(self):\n         self.consumer.send('imports', '\"Event\"', api_key='MY_API_KEY')\n+        assert len(self.log) == 0\n+        self.consumer.flush()\n+        assert self.log == [('imports', ['Event'], 'MY_API_KEY')]\n \n \n class TestFunctional:\n", "problem_statement": "flush function for Buffered Consumer not working\nHi,\nin class BufferedConsumer the flush function in line 338 should change to \ndef flush (self,api_key=None) \n\nand then in line 444-445 should change to:\n        for endpoint in self._buffers.keys():\n            self._flush_endpoint(endpoint,api_key=api_key)\n\n", "hints_text": "+1\n\nI have the same issue. The exception is: \"Mixpanel error: token, missing or empty\" because of this bug.\n\n+1 I also just ran into this. Is it worth submitting a PR for this? I see 3 unmerged PRs that are a few years old.", "created_at": "2016-12-22T00:07:05Z"}