def test_should_timeout(self): with self.assertRaises(requests.ReadTimeout): batch_post('key', batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' }], timeout=0.0001)
def test_should_timeout(self): with self.assertRaises(requests.ReadTimeout): batch_post("key", batch=[{ "distinct_id": "distinct_id", "event": "python event", "type": "track" }], timeout=0.0001)
def test_should_not_timeout(self): res = batch_post(TEST_API_KEY, batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' }], timeout=15) self.assertEqual(res.status_code, 200)
def test_valid_request(self): res = batch_post(TEST_API_KEY, batch=[{ 'distinct_id': 'distinct_id', 'event': 'python event', 'type': 'track' }]) self.assertEqual(res.status_code, 200)
def _enqueue(self, msg): """Push a new `msg` onto the queue, return `(success, msg)`""" timestamp = msg["timestamp"] if timestamp is None: timestamp = datetime.utcnow().replace(tzinfo=tzutc()) message_id = msg.get("messageId") if message_id is None: message_id = uuid4() require("timestamp", timestamp, datetime) require("context", msg["context"], dict) # add common timestamp = guess_timezone(timestamp) msg["timestamp"] = timestamp.isoformat() msg["messageId"] = stringify_id(message_id) if not msg.get("properties"): msg["properties"] = {} msg["properties"]["$lib"] = "posthog-python" msg["properties"]["$lib_version"] = VERSION msg["distinct_id"] = stringify_id(msg.get("distinct_id", None)) msg = clean(msg) self.log.debug("queueing: %s", msg) # if send is False, return msg as if it was successfully queued if not self.send: return True, msg if self.sync_mode: self.log.debug("enqueued with blocking %s.", msg["event"]) batch_post(self.api_key, self.host, gzip=self.gzip, timeout=self.timeout, batch=[msg]) return True, msg try: self.queue.put(msg, block=False) self.log.debug("enqueued %s.", msg["event"]) return True, msg except queue.Full: self.log.warning("analytics-python queue is full") return False, msg
def _enqueue(self, msg): """Push a new `msg` onto the queue, return `(success, msg)`""" timestamp = msg['timestamp'] if timestamp is None: timestamp = datetime.utcnow().replace(tzinfo=tzutc()) message_id = msg.get('messageId') if message_id is None: message_id = uuid4() require('timestamp', timestamp, datetime) require('context', msg['context'], dict) # add common timestamp = guess_timezone(timestamp) msg['timestamp'] = timestamp.isoformat() msg['messageId'] = stringify_id(message_id) if not msg.get('properties'): msg['properties'] = {} msg['properties']['$lib'] = 'posthog-python' msg['properties']['$lib_version'] = VERSION msg['distinct_id'] = stringify_id(msg.get('distinct_id', None)) msg = clean(msg) self.log.debug('queueing: %s', msg) # if send is False, return msg as if it was successfully queued if not self.send: return True, msg if self.sync_mode: self.log.debug('enqueued with blocking %s.', msg['event']) batch_post(self.api_key, self.host, gzip=self.gzip, timeout=self.timeout, batch=[msg]) return True, msg try: self.queue.put(msg, block=False) self.log.debug('enqueued %s.', msg['event']) return True, msg except queue.Full: self.log.warning('analytics-python queue is full') return False, msg
def test_valid_request(self): res = batch_post(TEST_API_KEY, batch=[{ "distinct_id": "distinct_id", "event": "python event", "type": "track" }]) self.assertEqual(res.status_code, 200)
def test_should_not_timeout(self): res = batch_post(TEST_API_KEY, batch=[{ "distinct_id": "distinct_id", "event": "python event", "type": "track" }], timeout=15) self.assertEqual(res.status_code, 200)
def send_request(): batch_post(self.api_key, self.host, gzip=self.gzip, timeout=self.timeout, batch=batch)