Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Hotfix: fix webhook headers not parsed as dict
Browse files Browse the repository at this point in the history
  • Loading branch information
myoung34 committed Aug 24, 2020
1 parent e9e26c9 commit d30160e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand All @@ -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',
}
Expand Down Expand Up @@ -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',
}
Expand Down
4 changes: 3 additions & 1 deletion tilty/emitters/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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],
Expand All @@ -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,
Expand Down

0 comments on commit d30160e

Please sign in to comment.