# Path: duct/objects.py
# class Output(object):
#     """Output parent class
# 
#     Outputs can inherit this object which provides a construct
#     for a working output
# 
#     :param config: Dictionary config for this queue (usually read from the
#              yaml configuration)
#     :param duct: A DuctService object for interacting with the queue manager
#     """
#     def __init__(self, config, duct):
#         self.config = config
#         self.duct = duct
#         self.events = []
#         self.maxsize = 0
# 
#     def createClient(self):
#         """Deferred which sets up the output
#         """
#         pass
# 
#     def eventsReceived(self, events):
#         """Receives a list of events and queues them
# 
#         Arguments:
#         events -- list of `duct.objects.Event`
#         """
#         # Make sure queue isn't oversized
#         if self.maxsize > 0:
#             if (self.maxsize < 1) or (len(self.events) < self.maxsize):
#                 self.events.extend(events)
#         else:
#             self.events.extend(events)
# 
#     def stop(self):
#         """Called when the service shuts down
#         """
#         pass
# 
# Path: duct/protocol/opentsdb.py
# class OpenTSDBClient(object):
#     """Twisted ElasticSearch API
#     """
#     def __init__(self, url='http://localhost:4242', user=None, password=None):
#         self.url = url.rstrip('/')
#         self.user = user
#         self.password = password
# 
#     def _request(self, path, data=None, method='POST'):
#         headers = {}
#         if self.user:
#             authorization = b64encode('%s:%s' % (self.user, self.password)
#                                      ).decode()
#             headers['Authorization'] = ['Basic ' + authorization]
# 
#         return utils.HTTPRequest().getJson(
#             self.url + path, method, headers=headers, data=data.encode())
# 
#     def put(self, data):
#         """Put one or more metrics
#         """
#         return self._request('/api/put', json.dumps(data))


from twisted.internet import defer, task
from twisted.python import log
from duct.objects import Output
from duct.protocol.opentsdb import OpenTSDBClient
    :param password: Optional basic auth password
    :type password: str
    :param debug: Log tracebacks from OpenTSDB
    :type debug: str
    """
    def __init__(self, *a):
        Output.__init__(self, *a)
        self.events = []
        self.timer = task.LoopingCall(self.tick)

        self.inter = float(self.config.get('interval', 1.0))  # tick interval
        self.maxsize = int(self.config.get('maxsize', 250000))

        self.user = self.config.get('user')
        self.password = self.config.get('password')
        self.client = None

        self.url = self.config.get('url', 'http://localhost:4242')

        maxrate = int(self.config.get('maxrate', 100))

        if maxrate > 0:
            self.queueDepth = int(maxrate * self.inter)
        else:
            self.queueDepth = None

    def createClient(self):
        """Sets up HTTP connector and starts queue timer
        """


# Path: duct/objects.py
# class Output(object):
#     """Output parent class
# 
#     Outputs can inherit this object which provides a construct
#     for a working output
# 
#     :param config: Dictionary config for this queue (usually read from the
#              yaml configuration)
#     :param duct: A DuctService object for interacting with the queue manager
#     """
#     def __init__(self, config, duct):
#         self.config = config
#         self.duct = duct
#         self.events = []
#         self.maxsize = 0
# 
#     def createClient(self):
#         """Deferred which sets up the output
#         """
#         pass
# 
#     def eventsReceived(self, events):
#         """Receives a list of events and queues them
# 
#         Arguments:
#         events -- list of `duct.objects.Event`
#         """
#         # Make sure queue isn't oversized
#         if self.maxsize > 0:
#             if (self.maxsize < 1) or (len(self.events) < self.maxsize):
#                 self.events.extend(events)
#         else:
#             self.events.extend(events)
# 
#     def stop(self):
#         """Called when the service shuts down
#         """
#         pass
# 
# Path: duct/protocol/opentsdb.py
# class OpenTSDBClient(object):
#     """Twisted ElasticSearch API
#     """
#     def __init__(self, url='http://localhost:4242', user=None, password=None):
#         self.url = url.rstrip('/')
#         self.user = user
#         self.password = password
# 
#     def _request(self, path, data=None, method='POST'):
#         headers = {}
#         if self.user:
#             authorization = b64encode('%s:%s' % (self.user, self.password)
#                                      ).decode()
#             headers['Authorization'] = ['Basic ' + authorization]
# 
#         return utils.HTTPRequest().getJson(
#             self.url + path, method, headers=headers, data=data.encode())
# 
#     def put(self, data):
#         """Put one or more metrics
#         """
#         return self._request('/api/put', json.dumps(data))


# Path: duct/outputs/opentsdb.py
from twisted.internet import defer, task
from twisted.python import log
from duct.objects import Output
from duct.protocol.opentsdb import OpenTSDBClient

    :param password: Optional basic auth password
    :type password: str
    :param debug: Log tracebacks from OpenTSDB
    :type debug: str
    """
    def __init__(self, *a):
        Output.__init__(self, *a)
        self.events = []
        self.timer = task.LoopingCall(self.tick)

        self.inter = float(self.config.get('interval', 1.0))  # tick interval
        self.maxsize = int(self.config.get('maxsize', 250000))

        self.user = self.config.get('user')
        self.password = self.config.get('password')
        self.client = None

        self.url = self.config.get('url', 'http://localhost:4242')

        maxrate = int(self.config.get('maxrate', 100))

        if maxrate > 0:
            self.queueDepth = int(maxrate * self.inter)
        else:
            self.queueDepth = None

    def createClient(self):
        """Sets up HTTP connector and starts queue timer
        """


        self.client = OpenTSDBClient(self.url, self.user, self.password)