diff --git a/README.md b/README.md index 6097ec2..49ee147 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ The Tilt supports writing to a google doc which you could use with something lik * stdout * Webhooks + * Generic (Send to any endpoint with any type) + * Brewstat.us (Example below) + * BrewersFriend (Example below) * InfluxDB * Datadog (dogstatsd) * SQLite @@ -56,9 +59,7 @@ file = /etc/tilty/tilt.sqlite [webhook] url = http://www.foo.com headers = {"Content-Type": "application/json"} -payload_template = {"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{ temp }}, "timestamp": "{{ timestamp }}"} -# optional payload to include mac address -# payload_template = {"color": "{{ color }}", "gravity": {{ gravity }}, "mac": {{ mac }}, "temp": {{ temp }}, "timestamp": "{{ timestamp }}"} +payload_template = {"color": "{{ color }}", "gravity": {{ gravity }}, "mac": {{ mac }}, "temp": {{ temp }}, "timestamp": "{{ timestamp }}"} method = POST # Brewstat.us example @@ -68,15 +69,19 @@ headers = {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"} payload_template = {"Color": "{{ color }}", "SG": {{ gravity }}, "Temp": {{ temp }}, "Timepoint": "{{ timestamp }}"} method = POST +# Brewers Friend example +[webhook] +url = https://log.brewersfriend.com/stream/3009ec67c6d81276185c90824951bd32bg +headers = {"Content-Type": "application/json"} +payload_template = {"device_source": "Tilt","temp": {{ temp }}, "name": "{{ color }} ","temp_unit": "F","gravity": {{ gravity }},"gravity_unit": "G"} +method = POST + [influxdb] url = influxdb.corp.com port = 80 database = tilty -gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}} -temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}} -# optional payload to include mac address -# gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ gravity }}}} -# temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ temp }}}} +gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ gravity }}}} +temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ temp }}}} # `% curl -s -G 'http://influxdb.service.consul:8086/query?pretty=true' --data-urlencode "db=tilty" --data-urlencode 'q=SELECT "value", "mac", "color" FROM "autogen"."gravity"' | jq '.results[].series[].values[0]'` [datadog] diff --git a/tests/test_webhook.py b/tests/test_webhook.py index 3a8fe63..0bb972c 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -17,7 +17,7 @@ def test_webhook_get( ): config = { 'url': 'http://www.google.com', - 'headers': {'Content-Type': 'application/json'}, + 'headers': '{"Content-Type": "application/json"}', 'payload_template': '{"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{ temp }}}', # noqa 'method': 'GET', } @@ -43,7 +43,7 @@ def test_webhook_post_json( ): config = { 'url': 'http://www.google.com', - 'headers': {'Content-Type': 'application/json'}, + 'headers': '{"Content-Type": "application/json"}', 'payload_template': '{"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{ temp }}}', # noqa 'method': 'POST', } @@ -71,7 +71,7 @@ def test_webhook_post_data( ): config = { 'url': 'http://www.google.com', - 'headers': {'Content-Type': 'text/plain'}, + 'headers': '{"Content-Type": "text/plain"}', 'payload_template': '{"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{ temp }}, "timestamp": "{{ timestamp }}"}', # noqa 'method': 'POST', } diff --git a/tilty/emitters/webhook.py b/tilty/emitters/webhook.py index ec5baaf..387be49 100644 --- a/tilty/emitters/webhook.py +++ b/tilty/emitters/webhook.py @@ -36,7 +36,7 @@ def __init__(self, config: dict) -> None: self.method = METHODS.get(config['method']) if self.method is None: raise KeyError - self.headers: dict = config['headers'] + self.headers: dict = json.loads(config['headers']) self.template: Template = Template(config['payload_template']) def emit(self, tilt_data: dict) -> requests.Response: @@ -53,6 +53,7 @@ def emit(self, tilt_data: dict) -> requests.Response: temp=tilt_data['temp'], timestamp=tilt_data['timestamp'], )) + LOGGER.debug( '[webhook] %s to %s with %s', self.method.__str__().split(' ')[1], @@ -67,6 +68,7 @@ def emit(self, tilt_data: dict) -> requests.Response: headers=self.headers, json=payload, ) + LOGGER.debug('[webhook] sending as non-json') return self.method( # type: ignore url=self.url, headers=self.headers,