dashing.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import logging
  5. import requests
  6. class DashingClient:
  7. base_url = None
  8. def __init__(self, base_url, auth_token):
  9. self.base_url = base_url
  10. self.auth_token = auth_token
  11. self.logger = logging.getLogger('dashing')
  12. def __str__(self):
  13. return 'Dashing at \'{0}\''.format(self.base_url)
  14. def send(self, metric, current, previous=None):
  15. info = {
  16. 'auth_token': self.auth_token,
  17. 'current': int(current),
  18. }
  19. if previous is not None:
  20. info['previous'] = previous
  21. url = self.base_url + metric
  22. resp = requests.post(url, data=json.dumps(info)).read()
  23. self.logger.debug('Sent metric "{0}" = "{1}"'.format(metric, current))
  24. return resp
  25. def push(self, data):
  26. self.logger.warn('push() not implemented yet')
  27. if __name__ == "__main__":
  28. client = DashingClient(
  29. 'http://dashing.krombel.de:3030/widgets/',
  30. 'bitnhmlj47hamrftxkiug')
  31. client.send('testNumber', 42)