def test_should_timeout(self): with self.assertRaises(requests.ReadTimeout): post('testsecret', batch=[{ 'userId': 'userId', 'event': 'python event', 'type': 'track' }], timeout=0.0001)
def send_request(): post(self.write_key, self.host, gzip=self.gzip, timeout=self.timeout, batch=batch, proxies=self.proxies)
def request(self, batch, attempt=0): """Attempt to upload the batch and retry before raising an error """ try: post(self.write_key, batch=batch) except: if attempt > self.retries: raise self.request(batch, attempt + 1)
def request(self, batch, attempt=0): """Attempt to upload the batch and retry before raising an error """ try: post(self.write_key, batch=batch) except: if attempt > self.retries: raise self.request(batch, attempt+1)
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('integrations', msg['integrations'], dict) require('type', msg['type'], string_types) 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) msg['context']['library'] = { 'name': 'analytics-python', 'version': VERSION } msg['userId'] = stringify_id(msg.get('userId', None)) msg['anonymousId'] = stringify_id(msg.get('anonymousId', 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['type']) post(self.write_key, self.host, gzip=self.gzip, timeout=self.timeout, proxies=self.proxies, batch=[msg]) return True, msg try: self.queue.put(msg, block=False) self.log.debug('enqueued %s.', msg['type']) return True, msg except queue.Full: self.log.warning('analytics-python queue is full') return False, msg
def test_should_not_timeout(self): res = post('testsecret', batch=[{ 'userId': 'userId', 'event': 'python event', 'type': 'track' }], timeout=15) self.assertEqual(res.status_code, 200)
def test_valid_request(self): res = post('testsecret', batch=[{ 'userId': 'userId', '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()) require('integrations', msg['integrations'], dict) require('type', msg['type'], string_types) require('timestamp', timestamp, datetime) require('context', msg['context'], dict) # add common timestamp = guess_timezone(timestamp) msg['timestamp'] = timestamp.isoformat() msg['messageId'] = str(uuid4()) msg['context']['library'] = { 'name': 'analytics-python', 'version': VERSION } msg['userId'] = stringify_id(msg.get('userId', None)) msg['anonymousId'] = stringify_id(msg.get('anonymousId', 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.max_queue_size == 0: # If we're not using a queue, send immediately (synchronously) post(self.write_key, self.host, batch=[msg]) return True, msg else: try: self.queue.put(msg, block=False) self.log.debug('enqueued %s.', msg['type']) return True, msg except queue.Full: self.log.warn('analytics-python queue is full') return False, msg
def request(self, batch, attempt=0): """Attempt to upload the batch and retry before raising an error """ try: post(self.write_key, self.host, batch=batch) except Exception as exc: def maybe_retry(): if attempt >= self.retries: raise self.request(batch, attempt+1) if isinstance(exc, APIError): if exc.status >= 500 or exc.status == 429: # retry on server errors and client errors with 429 status code (rate limited) maybe_retry() elif exc.status >= 400: # don't retry on other client errors self.log.error('API error: %s', exc) raise else: self.log.debug('Unexpected APIError: %s', exc) else: # retry on all other errors (eg. network) maybe_retry()
def send_request(): post(self.write_key, self.host, gzip=self.gzip, batch=batch)