Exemplo n.º 1
0
def get_client(secret):
    '''Returns or creates a new client by the secret'''

    if secret not in clients:
        clients[secret] = Client(secret)

    return clients[secret]
Exemplo n.º 2
0
    def test_synchronous(self):
        client = Client('testsecret', sync_mode=True)

        success, _ = client.identify('userId')
        self.assertFalse(client.consumers)
        self.assertTrue(client.queue.empty())
        self.assertTrue(success)
Exemplo n.º 3
0
def init(secret, **kwargs):
    """Create a default instance of a analytics-python client

    :param str secret: The Segment.io API Secret

    Kwargs:

    :param logging.LOG_LEVEL log_level: The logging log level for the client
    talks to. Use log_level=logging.DEBUG to troubleshoot
    : param bool log: False to turn off logging completely, True by default
    : param int flush_at: Specicies after how many messages the client will flush
    to the server. Use flush_at=1 to disable batching
    : param datetime.timedelta flush_after: Specifies after how much time
    of no flushing that the server will flush. Used in conjunction with
    the flush_at size policy
    : param bool async: True to have the client flush to the server on another
    thread, therefore not blocking code (this is the default). False to
    enable blocking and making the request on the calling thread.

    """
    from analytics.client import Client

    # if we have already initialized, no-op
    if hasattr(this_module, 'default_client'):
        return

    default_client = Client(secret=secret, stats=stats, **kwargs)

    setattr(this_module, 'default_client', default_client)
Exemplo n.º 4
0
def _proxy(method, *args, **kwargs):
    """Create an analytics client if one doesn't exist and send to it."""
    global default_client
    if not default_client:
        default_client = Client(write_key, host=host, debug=debug, on_error=on_error,
                                send=send)

    fn = getattr(default_client, method)
    fn(*args, **kwargs)
Exemplo n.º 5
0
    def test_synchronous(self):
        # Max queue size at 0 -> synchronous mode
        client = Client('testsecret', max_queue_size=0)
        # Ensure the consumer thread is not running
        client.consumer.pause()

        success, message = client.identify('userId')

        self.assertTrue(client.queue.empty())
        self.assertTrue(success)
Exemplo n.º 6
0
    def test_overflow(self):
        client = Client('testsecret', max_queue_size=1)
        client.consumer.pause()
        time.sleep(5.1)  # allow time for consumer to exit

        for i in range(10):
            client.identify('userId')

        success, msg = client.identify('userId')
        self.assertFalse(success)
Exemplo n.º 7
0
    def test_overflow(self):
        client = Client('testsecret', max_queue_size=1)
        # Ensure consumer thread is no longer uploading
        client.join()

        for _ in range(10):
            client.identify('userId')

        success, _ = client.identify('userId')
        # Make sure we are informed that the queue is at capacity
        self.assertFalse(success)
Exemplo n.º 8
0
def _proxy(method, *args, **kwargs):
    """Create an analytics client if one doesn't exist and send to it."""
    global default_client
    if not default_client:
        default_client = Client(write_key,
                                host=host,
                                debug=debug,
                                max_queue_size=max_queue_size,
                                send=send,
                                on_error=on_error,
                                gzip=gzip,
                                max_retries=max_retries,
                                sync_mode=sync_mode,
                                timeout=timeout)

    fn = getattr(default_client, method)
    fn(*args, **kwargs)
Exemplo n.º 9
0
    def test_user_defined_flush_at(self):
        client = Client('testsecret',
                        on_error=self.fail,
                        flush_at=10,
                        flush_interval=3)

        def mock_post_fn(*args, **kwargs):
            self.assertEqual(len(kwargs['batch']), 10)

        # the post function should be called 2 times, with a batch size of 10
        # each time.
        with mock.patch('analytics.consumer.post', side_effect=mock_post_fn) \
                as mock_post:
            for _ in range(20):
                client.identify('userId', {'trait': 'value'})
            time.sleep(1)
            self.assertEqual(mock_post.call_count, 2)
Exemplo n.º 10
0
 def test_gzip(self):
     client = Client('testsecret', on_error=self.fail, gzip=True)
     for _ in range(10):
         client.identify('userId', {'trait': 'value'})
     client.flush()
     self.assertFalse(self.failed)
Exemplo n.º 11
0
 def test_debug(self):
     Client('bad_key', debug=True)
Exemplo n.º 12
0
 def test_unicode(self):
     Client(six.u('unicode_key'))
Exemplo n.º 13
0
 def test_success_on_invalid_write_key(self):
     client = Client('bad_key', on_error=self.fail)
     client.track('userId', 'event')
     client.flush()
     self.assertFalse(self.failed)
Exemplo n.º 14
0
 def test_default_timeout_15(self):
     client = Client('testsecret')
     self.assertEquals(client.consumer.timeout, 15)
Exemplo n.º 15
0
 def test_default_timeout_15(self):
     client = Client('testsecret')
     for consumer in client.consumers:
         self.assertEqual(consumer.timeout, 15)
Exemplo n.º 16
0
 def test_max_retries(self):
     self.assertIsNone(analytics.default_client)
     client = Client('testsecret', max_retries=42)
     for consumer in client.consumers:
         self.assertEqual(consumer.retries, 42)
Exemplo n.º 17
0
 def test_unicode(self):
     Client('unicode_key')
Exemplo n.º 18
0
 def test_user_defined_timeout(self):
     client = Client('testsecret', timeout=10)
     for consumer in client.consumers:
         self.assertEqual(consumer.timeout, 10)
Exemplo n.º 19
0
 def setUp(self):
     self.failed = False
     self.client = Client('testsecret', on_error=self.fail)
Exemplo n.º 20
0
 def test_proxies(self):
     client = Client('testsecret', proxies='203.243.63.16:80')
     success, msg = client.identify('userId', {'trait': 'value'})
     self.assertTrue(success)
Exemplo n.º 21
0
 def test_user_defined_timeout(self):
     client = Client('testsecret', timeout=10)
     self.assertEquals(client.consumer.timeout, 10)