async def _write_request(self, request_url: str, json_message: str, api_key: str = None): b64message = base64.b64encode( json_message.encode(_encoding)).decode(_encoding) data = { 'data': b64message, 'verbose': 1, 'ip': 0, } if api_key: data.update(dict(api_key=api_key)) try: response = await self._client.get(request_url, params=data, raise_for_status=True) json = await response.json() except Exception as e: raise MixpanelException(e) if json.get('status') != 1: raise MixpanelException('Mixpanel error: {0}'.format( json.get('error'))) return True
def send(self, endpoint, json_message): ''' Record an event or a profile update. Calls to send() will store the given message in memory, and (when enough messages have been stored) trigger an async request to Mixpanel's servers. Calls to send() may throw an exception, but the exception may be associated with the message given in an earlier call. If this is the case, the resulting MixpanelException e will have members e.message and e.endpoint :param endpoint: One of 'events' or 'people', the Mixpanel endpoint for sending the data :type endpoint: str (one of 'events' or 'people') :param json_message: A json message formatted for the endpoint. :type json_message: str :raises: MixpanelException ''' if endpoint not in self._async_buffers: raise MixpanelException( 'No such endpoint "{0}". Valid endpoints are one of {1}'. format(endpoint, self._async_buffers.keys())) buf = self._async_buffers[endpoint] buf.append(json_message) should_flush = self._should_flush(endpoint) if should_flush == self.ALL: self.flush() elif should_flush == self.ENDPOINT: self._flush_endpoint(endpoint)
def send(self, endpoint: str, json_message: str, api_key: str = None): """ Record an event or a profile update. :param endpoint: the Mixpanel API endpoint appropriate for the message :type endpoint: "events" | "people" | "imports" :param str json_message: a JSON message formatted for the endpoint :param str api_key: your Mixpanel project's API key :raises MixpanelException: if the endpoint doesn't exist, the server is unreachable, or the message cannot be processed """ if endpoint in self._endpoints: self._loop.create_task( 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()))