kopia lustrzana https://github.com/dgtlmoon/changedetection.io
Notifications - Fixing support for headers in custom post://, posts:// notifications, ability to include HTTP headers when making custom notifications (#2018)
rodzic
98f56736c1
commit
f0823126c8
|
@ -46,6 +46,9 @@ from apprise.decorators import notify
|
||||||
@notify(on="puts")
|
@notify(on="puts")
|
||||||
def apprise_custom_api_call_wrapper(body, title, notify_type, *args, **kwargs):
|
def apprise_custom_api_call_wrapper(body, title, notify_type, *args, **kwargs):
|
||||||
import requests
|
import requests
|
||||||
|
from apprise.utils import parse_url as apprise_parse_url
|
||||||
|
from apprise.URLBase import URLBase
|
||||||
|
|
||||||
url = kwargs['meta'].get('url')
|
url = kwargs['meta'].get('url')
|
||||||
|
|
||||||
if url.startswith('post'):
|
if url.startswith('post'):
|
||||||
|
@ -68,16 +71,45 @@ def apprise_custom_api_call_wrapper(body, title, notify_type, *args, **kwargs):
|
||||||
url = url.replace('delete://', 'http://')
|
url = url.replace('delete://', 'http://')
|
||||||
url = url.replace('deletes://', 'https://')
|
url = url.replace('deletes://', 'https://')
|
||||||
|
|
||||||
# Try to auto-guess if it's JSON
|
|
||||||
headers = {}
|
headers = {}
|
||||||
|
params = {}
|
||||||
|
auth = None
|
||||||
|
|
||||||
|
# Convert /foobar?+some-header=hello to proper header dictionary
|
||||||
|
results = apprise_parse_url(url)
|
||||||
|
if results:
|
||||||
|
# Add our headers that the user can potentially over-ride if they wish
|
||||||
|
# to to our returned result set and tidy entries by unquoting them
|
||||||
|
headers = {URLBase.unquote(x): URLBase.unquote(y)
|
||||||
|
for x, y in results['qsd+'].items()}
|
||||||
|
|
||||||
|
# https://github.com/caronc/apprise/wiki/Notify_Custom_JSON#get-parameter-manipulation
|
||||||
|
# In Apprise, it relies on prefixing each request arg with "-", because it uses say &method=update as a flag for apprise
|
||||||
|
# but here we are making straight requests, so we need todo convert this against apprise's logic
|
||||||
|
for k, v in results['qsd'].items():
|
||||||
|
if not k.strip('+-') in results['qsd+'].keys():
|
||||||
|
params[URLBase.unquote(k)] = URLBase.unquote(v)
|
||||||
|
|
||||||
|
# Determine Authentication
|
||||||
|
auth = ''
|
||||||
|
if results.get('user') and results.get('password'):
|
||||||
|
auth = (URLBase.unquote(results.get('user')), URLBase.unquote(results.get('user')))
|
||||||
|
elif results.get('user'):
|
||||||
|
auth = (URLBase.unquote(results.get('user')))
|
||||||
|
|
||||||
|
# Try to auto-guess if it's JSON
|
||||||
try:
|
try:
|
||||||
json.loads(body)
|
json.loads(body)
|
||||||
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
r(results.get('url'),
|
||||||
r(url, headers=headers, data=body)
|
auth=auth,
|
||||||
|
data=body,
|
||||||
|
headers=headers,
|
||||||
|
params=params
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_notification(n_object, datastore):
|
def process_notification(n_object, datastore):
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_discord">discord://</a></code> (or <code>https://discord.com/api/webhooks...</code>)) only supports a maximum <strong>2,000 characters</strong> of notification text, including the title.</li>
|
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_discord">discord://</a></code> (or <code>https://discord.com/api/webhooks...</code>)) only supports a maximum <strong>2,000 characters</strong> of notification text, including the title.</li>
|
||||||
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_telegram">tgram://</a></code> bots can't send messages to other bots, so you should specify chat ID of non-bot user.</li>
|
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_telegram">tgram://</a></code> bots can't send messages to other bots, so you should specify chat ID of non-bot user.</li>
|
||||||
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_telegram">tgram://</a></code> only supports very limited HTML and can fail when extra tags are sent, <a href="https://core.telegram.org/bots/api#html-style">read more here</a> (or use plaintext/markdown format)</li>
|
<li><code><a target=_new href="https://github.com/caronc/apprise/wiki/Notify_telegram">tgram://</a></code> only supports very limited HTML and can fail when extra tags are sent, <a href="https://core.telegram.org/bots/api#html-style">read more here</a> (or use plaintext/markdown format)</li>
|
||||||
<li><code>gets://</code>, <code>posts://</code>, <code>puts://</code>, <code>deletes://</code> for direct API calls (or omit the "<code>s</code>" for non-SSL ie <code>get://</code>)</li>
|
<li><code>gets://</code>, <code>posts://</code>, <code>puts://</code>, <code>deletes://</code> for direct API calls (or omit the "<code>s</code>" for non-SSL ie <code>get://</code>) <a href="https://github.com/dgtlmoon/changedetection.io/wiki/Notification-configuration-notes#postposts">more help here</a></li>
|
||||||
<li>Accepts the <code>{{ '{{token}}' }}</code> placeholders listed below</li>
|
<li>Accepts the <code>{{ '{{token}}' }}</code> placeholders listed below</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,22 +13,17 @@ global app
|
||||||
|
|
||||||
|
|
||||||
def cleanup(datastore_path):
|
def cleanup(datastore_path):
|
||||||
|
import glob
|
||||||
# Unlink test output files
|
# Unlink test output files
|
||||||
files = [
|
|
||||||
'count.txt',
|
for g in ["*.txt", "*.json", "*.pdf"]:
|
||||||
'endpoint-content.txt'
|
files = glob.glob(os.path.join(datastore_path, g))
|
||||||
'headers.txt',
|
for f in files:
|
||||||
'headers-testtag.txt',
|
if 'proxies.json' in f:
|
||||||
'notification.txt',
|
# Usually mounted by docker container during test time
|
||||||
'secret.txt',
|
continue
|
||||||
'url-watches.json',
|
if os.path.isfile(f):
|
||||||
'output.txt',
|
os.unlink(f)
|
||||||
]
|
|
||||||
for file in files:
|
|
||||||
try:
|
|
||||||
os.unlink("{}/{}".format(datastore_path, file))
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def app(request):
|
def app(request):
|
||||||
|
|
|
@ -281,7 +281,8 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server):
|
||||||
|
|
||||||
# CUSTOM JSON BODY CHECK for POST://
|
# CUSTOM JSON BODY CHECK for POST://
|
||||||
set_original_response()
|
set_original_response()
|
||||||
test_notification_url = url_for('test_notification_endpoint', _external=True).replace('http://', 'post://')+"?xxx={{ watch_url }}"
|
# https://github.com/caronc/apprise/wiki/Notify_Custom_JSON#header-manipulation
|
||||||
|
test_notification_url = url_for('test_notification_endpoint', _external=True).replace('http://', 'post://')+"?xxx={{ watch_url }}&+custom-header=123"
|
||||||
|
|
||||||
res = client.post(
|
res = client.post(
|
||||||
url_for("settings_page"),
|
url_for("settings_page"),
|
||||||
|
@ -297,10 +298,7 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server):
|
||||||
follow_redirects=True
|
follow_redirects=True
|
||||||
)
|
)
|
||||||
assert b'Settings updated' in res.data
|
assert b'Settings updated' in res.data
|
||||||
client.get(
|
|
||||||
url_for("form_delete", uuid="all"),
|
|
||||||
follow_redirects=True
|
|
||||||
)
|
|
||||||
# Add a watch and trigger a HTTP POST
|
# Add a watch and trigger a HTTP POST
|
||||||
test_url = url_for('test_endpoint', _external=True)
|
test_url = url_for('test_endpoint', _external=True)
|
||||||
res = client.post(
|
res = client.post(
|
||||||
|
@ -315,7 +313,9 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server):
|
||||||
set_modified_response()
|
set_modified_response()
|
||||||
|
|
||||||
client.get(url_for("form_watch_checknow"), follow_redirects=True)
|
client.get(url_for("form_watch_checknow"), follow_redirects=True)
|
||||||
time.sleep(2)
|
wait_for_all_checks(client)
|
||||||
|
|
||||||
|
time.sleep(2) # plus extra delay for notifications to fire
|
||||||
|
|
||||||
with open("test-datastore/notification.txt", 'r') as f:
|
with open("test-datastore/notification.txt", 'r') as f:
|
||||||
x = f.read()
|
x = f.read()
|
||||||
|
@ -328,6 +328,13 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server):
|
||||||
with open("test-datastore/notification-url.txt", 'r') as f:
|
with open("test-datastore/notification-url.txt", 'r') as f:
|
||||||
notification_url = f.read()
|
notification_url = f.read()
|
||||||
assert 'xxx=http' in notification_url
|
assert 'xxx=http' in notification_url
|
||||||
|
# apprise style headers should be stripped
|
||||||
|
assert 'custom-header' not in notification_url
|
||||||
|
|
||||||
|
with open("test-datastore/notification-headers.txt", 'r') as f:
|
||||||
|
notification_headers = f.read()
|
||||||
|
assert 'custom-header: 123' in notification_headers.lower()
|
||||||
|
|
||||||
|
|
||||||
# Should always be automatically detected as JSON content type even when we set it as 'Text' (default)
|
# Should always be automatically detected as JSON content type even when we set it as 'Text' (default)
|
||||||
assert os.path.isfile("test-datastore/notification-content-type.txt")
|
assert os.path.isfile("test-datastore/notification-content-type.txt")
|
||||||
|
@ -335,3 +342,8 @@ def test_notification_custom_endpoint_and_jinja2(client, live_server):
|
||||||
assert 'application/json' in f.read()
|
assert 'application/json' in f.read()
|
||||||
|
|
||||||
os.unlink("test-datastore/notification-url.txt")
|
os.unlink("test-datastore/notification-url.txt")
|
||||||
|
|
||||||
|
client.get(
|
||||||
|
url_for("form_delete", uuid="all"),
|
||||||
|
follow_redirects=True
|
||||||
|
)
|
||||||
|
|
|
@ -205,6 +205,9 @@ def live_server_setup(live_server):
|
||||||
with open("test-datastore/notification-url.txt", "w") as f:
|
with open("test-datastore/notification-url.txt", "w") as f:
|
||||||
f.write(request.url)
|
f.write(request.url)
|
||||||
|
|
||||||
|
with open("test-datastore/notification-headers.txt", "w") as f:
|
||||||
|
f.write(str(request.headers))
|
||||||
|
|
||||||
if request.content_type:
|
if request.content_type:
|
||||||
with open("test-datastore/notification-content-type.txt", "w") as f:
|
with open("test-datastore/notification-content-type.txt", "w") as f:
|
||||||
f.write(request.content_type)
|
f.write(request.content_type)
|
||||||
|
|
Ładowanie…
Reference in New Issue