Skip to content

Instantly share code, notes, and snippets.

@gchiam
Created September 9, 2015 00:02
Show Gist options
  • Save gchiam/1d2c32434ecf0c966922 to your computer and use it in GitHub Desktop.
Save gchiam/1d2c32434ecf0c966922 to your computer and use it in GitHub Desktop.
Python module to send data to Dashing widget
"""Dashing module"""
import logging
import urllib2
import simplejson as json
DEFAULT_DASHING_AUTH_TOKEN = 'YOUR_AUTH_TOKEN'
class Dashing(object):
"""Send data to dashing widget
Usage:
dashing = Dashing('http://localhost:3030')
dashing.send_data('welcome', {'text': 'Hello from python!'})
"""
def __init__(self, host_url, auth_token=None):
self.logger = logging.getLogger(__name__)
self._host_url = host_url
self._auth_token = auth_token or DEFAULT_DASHING_AUTH_TOKEN
def _get_widget_url(self, widget_id):
"""Return dashing widget url for the given widget id
Args:
widget_id(str): widget id
"""
return '{host_url}/widgets/{widget_id}'.format(
host_url=self._host_url,
widget_id=widget_id,
)
def _get_auth_token_data(self):
"""Return a dict of 'auth_token' -> `auth_token`
"""
return {
'auth_token': self._auth_token
}
def _get_request(self, widget_id, data):
"""Retrun a urllib2.Request object
Args:
widget_id(str): widget id
data(dict): data to be sent to dashing widget
"""
url = self._get_widget_url(widget_id)
request_data = self._get_auth_token_data()
request_data.update(data)
encoded_data = json.dumps(request_data)
return urllib2.Request(url=url, data=encoded_data)
def send_data(self, widget_id, data):
"""Send data to a dashing widget
Args:
widget_id(str): widget id
data(dict): data to be sent to dashing widget
"""
request = self._get_request(widget_id, data)
self.logger.info(
'Send data to Dashing server[%s]: data[%s]',
request.get_full_url(),
request.get_data()
)
urllib2.urlopen(request).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment