# Copyright (C) 2015 SignalFx, Inc. All rights reserved.

import json
import logging
import os
import requests

DEFAULT_API_ENDPOINT_URL = 'https://api.signalfx.com/v1'
DEFAULT_INGEST_ENDPOINT_URL = 'https://ingest.signalfx.com/v2'


class __BaseSignalFx(object):

    def __init__(self, api_token=None, api_endpoint=DEFAULT_API_ENDPOINT_URL,
                 ingest_endpoint=DEFAULT_INGEST_ENDPOINT_URL, timeout=1):
        self._api_token = api_token
        self._api_endpoint = api_endpoint
        self._ingest_endpoint = ingest_endpoint
        self._timeout = timeout

    def send(self, gauges=None, counters=None):
        if not gauges and not counters:
            return None

        # TODO: switch to protocol buffers.
        data = {'gauge': gauges, 'counter': counters}
        logging.debug('Sending to SignalFx: %s', data)
        return data


SignalFxLoggingStub = __BaseSignalFx


class SignalFx(__BaseSignalFx):
    """SignalFx API client.

    This class presents a programmatic interface to SignalFx's metadata and
    ingest APIs. At the time being, only ingest is supported; more will come
    later.
    """

    def __init__(self, api_token, api_endpoint=DEFAULT_API_ENDPOINT_URL,
                 ingest_endpoint=DEFAULT_INGEST_ENDPOINT_URL, timeout=1):
        super(SignalFx, self).__init__(
            self, api_token, api_endpoint, ingest_endpoint, timeout)

    def send(self, gauges=None, counters=None):
        """Send the given metrics to SignalFx.

        Args:
            gauges (list): a list of dictionaries representing the gauges to
                report.
            counters (list): a list of dictionaries representing the counters
                to report.
        """
        data = super(SignalFx, self).send(self, gauges, counters)
        if not data:
            return None

        return requests.post(
                os.path.join(self._ingest_endpoint, 'datapoint'),
                headers={'Content-Type': 'application/json',
                         'X-SF-Token': self._api_token},
                data=json.dumps(data),
                verify=True,
                timeout=self._timeout)

@@ -856,16 +856,307 @@
 n data%0A%0A
+    def send_event(self, event_type, dimensions=None, properties=None):%0A        data = %7B'eventType': event_type,%0A                'dimensions': dimensions or %7B%7D,%0A                'properties': properties or %7B%7D%7D%0A        logging.debug('Sending event to SignalFx: %25s', data)%0A        return data%0A%0A
 %0ASignalF
@@ -1623,14 +1623,8 @@
     
- self,
  api
@@ -1670,16 +1670,400 @@
 timeout)
+%0A        self._session = requests.Session()%0A        self._session.headers.update(%7B%0A            'Content-Type': 'application/json',%0A            'X-SF-Token': self._api_token%0A        %7D)%0A        self._session.verify = True%0A%0A    def _post(self, endpoint, data):%0A        return self._session.post(%0A            endpoint,%0A            data=json.dumps(data),%0A            timeout=self._timeout)
 %0A%0A    de
@@ -2422,30 +2422,24 @@
  self).send(
-self, 
 gauges, coun
@@ -2509,27 +2509,20 @@
 urn 
-requests.
+self._
 post(%0A
-    
     
@@ -2595,152 +2595,366 @@
     
-    headers=%7B'Content-Type': 'application/json',%0A                         'X-SF-Token': self._api_token%7D,%0A                data=json.dumps(data),
+data)%0A%0A    def send_event(self, event_type, dimensions=None, properties=None):%0A        %22%22%22Send an event to SignalFx.%0A%0A        Args:%0A            event_type (string): the event type (name of the event time%0A                series).%0A            dimensions (dict): a map of event dimensions.%0A            properties (dict): a map of extra properties on that event.
 %0A   
@@ -2958,24 +2958,28 @@
         
+%22%22%22%0A
         
 verify=T
@@ -2974,56 +2974,239 @@
     
-verify=True,%0A                timeout=self._timeout
+data = super(SignalFx, self).send_event(%0A            event_type, dimensions, properties)%0A        if not data:%0A            return None%0A%0A        return self._post(%0A            os.path.join(self._api_endpoint, 'event'),%0A            data
 )%0A

Implement sending events