This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for configurable webhooks
- Loading branch information
Showing
21 changed files
with
584 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ __pycache__ | |
*.xml | ||
.coverage | ||
*.sw[o-p] | ||
config.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/bin/sh | ||
exec tilty -r | ||
exec tilty "${@}" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,11 @@ authors = ["Marcus Young <[email protected]>"] | |
license = "MIT" | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.6,<3.8" | ||
click = "^7.0" | ||
pybluez = "^0.22.0" | ||
requests = "^2.22" | ||
jinja2 = "^2.11.1" | ||
|
||
[tool.poetry.dev-dependencies] | ||
flake8 = "^3.7" | ||
|
@@ -37,6 +40,3 @@ whitelist_externals = make | |
bash | ||
pylint | ||
""" | ||
[build-system] | ||
requires = ["poetry>=0.12"] | ||
build-backend = "poetry.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,13 @@ | |
author='Marcus Young', | ||
author_email='[email protected]', | ||
py_modules=['tilty', 'blescan'], | ||
version='0.0.3', | ||
version='0.1.0', | ||
packages=find_packages(exclude=['tests*']), | ||
install_requires=[ | ||
'Click', | ||
'Jinja2', | ||
'pybluez', | ||
'requests', | ||
], | ||
entry_points={ | ||
'console_scripts': [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
class MockConfigParser: | ||
def __init__(self): | ||
pass | ||
|
||
def __getitem__(self, key): | ||
return { | ||
'url': 'http://www.google.com', | ||
'headers': {'Content-Type': 'application/json'}, | ||
'payload_template': '{"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{ temp }}, "timestamp": "{{ timestamp }}"}', | ||
'method': 'GET' | ||
} | ||
|
||
def has_section(*args, **kwargs): | ||
return True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from tilty.emitters import webhook | ||
|
||
|
||
@mock.patch('tilty.emitters.webhook.METHODS') | ||
def test_webhook_get( | ||
mock_requests, | ||
): | ||
config = { | ||
'url': 'http://www.google.com', | ||
'headers': {'Content-Type': 'application/json'}, | ||
'payload': {'b': 'b1'}, 'method': 'GET' | ||
} | ||
webhook.Webhook(config=config).emit() | ||
assert mock_requests.mock_calls == [ | ||
mock.call.get('GET'), | ||
mock.call.get()( | ||
json={'b': 'b1'}, | ||
headers={'Content-Type': 'application/json'}, | ||
url='http://www.google.com' | ||
) | ||
] | ||
|
||
|
||
def test_webhook_invalid_method(): | ||
config = { | ||
'url': 'http://www.google.com', | ||
'headers': {'Content-Type': 'application/json'}, | ||
'payload': {'b': 'b1'}, 'method': 'bad' | ||
} | ||
with pytest.raises(TypeError): | ||
webhook.Webhook(config=config).emit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Webhook emitter """ | ||
import requests | ||
|
||
METHODS = { | ||
"GET": requests.get, | ||
"POST": requests.post, | ||
} | ||
|
||
|
||
class Webhook: # pylint: disable=too-few-public-methods | ||
""" Class to represent the actual device """ | ||
def __init__(self, config): | ||
""" Initializer | ||
Args: | ||
config: (dict) represents the configuration for the emitter | ||
""" | ||
self.url = config['url'] | ||
self.method = METHODS.get(config['method']) | ||
self.headers = config['headers'] | ||
self.payload = config['payload'] | ||
|
||
def emit(self, **kwargs): # pylint: disable=no-self-use,unused-argument | ||
""" Initializer | ||
Args: | ||
""" | ||
self.method( | ||
url=self.url, | ||
headers=self.headers, | ||
json=self.payload, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Class to encapsulate all the emitter logic """ | ||
import json | ||
|
||
from jinja2 import Template | ||
|
||
from tilty.emitters import webhook | ||
|
||
|
||
def emit(config, tilt_data): | ||
""" Find and call emitters from config | ||
config (dict): configuration file loaded from disk | ||
tilt_data (dict): data returned from valid tilt device scan | ||
""" | ||
if tilt_data is None: | ||
return | ||
|
||
if config.has_section('webhook'): | ||
_template = Template(config['webhook']['payload_template']) | ||
_config = { | ||
'url': config['webhook']['url'], | ||
'headers': config['webhook'].get('headers'), | ||
'method': config['webhook']['method'], | ||
'payload': json.loads(_template.render( | ||
color=tilt_data['color'], | ||
gravity=tilt_data['gravity'], | ||
temp=tilt_data['temp'], | ||
timestamp=tilt_data['timestamp'], | ||
)), | ||
} | ||
_webhook = webhook.Webhook(config=_config) | ||
_webhook.emit() |