Changed webhook arguments and add exception handling
This commit is contained in:
parent
15fa7039e8
commit
b7380018f1
@ -16,26 +16,15 @@ class Example(plugins.Plugin):
|
|||||||
logging.debug("example plugin created")
|
logging.debug("example plugin created")
|
||||||
|
|
||||||
# called when http://<host>:<port>/plugins/<plugin>/ is called
|
# called when http://<host>:<port>/plugins/<plugin>/ is called
|
||||||
# must return a response
|
# must return a html page
|
||||||
def on_webhook(self, path, args, req_method):
|
# IMPORTANT: If you use "POST"s, add a csrf-token (via csrf_token() and render_template_string)
|
||||||
|
def on_webhook(self, path, request):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# called when the plugin is loaded
|
# called when the plugin is loaded
|
||||||
def on_loaded(self):
|
def on_loaded(self):
|
||||||
logging.warning("WARNING: this plugin should be disabled! options = " % self.options)
|
logging.warning("WARNING: this plugin should be disabled! options = " % self.options)
|
||||||
|
|
||||||
# called when <host>:<port>/plugins/<pluginname> is opened
|
|
||||||
def on_webhook(self, response, path):
|
|
||||||
res = "<html><body><a>Hook triggered</a></body></html>"
|
|
||||||
response.send_response(200)
|
|
||||||
response.send_header('Content-type', 'text/html')
|
|
||||||
response.end_headers()
|
|
||||||
|
|
||||||
try:
|
|
||||||
response.wfile.write(bytes(res, "utf-8"))
|
|
||||||
except Exception as ex:
|
|
||||||
logging.error(ex)
|
|
||||||
|
|
||||||
# called in manual mode when there's internet connectivity
|
# called in manual mode when there's internet connectivity
|
||||||
def on_internet_available(self, agent):
|
def on_internet_available(self, agent):
|
||||||
pass
|
pass
|
||||||
|
@ -40,17 +40,13 @@ class Handler:
|
|||||||
# show plugins overview
|
# show plugins overview
|
||||||
abort(404)
|
abort(404)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# call plugin on_webhook
|
|
||||||
arguments = request.args
|
|
||||||
req_method = request.method
|
|
||||||
|
|
||||||
# need to return something here
|
|
||||||
if name in plugins.loaded and hasattr(plugins.loaded[name], 'on_webhook'):
|
if name in plugins.loaded and hasattr(plugins.loaded[name], 'on_webhook'):
|
||||||
return render_template_string(
|
try:
|
||||||
plugins.loaded[name].on_webhook(subpath, args=arguments, req_method=req_method))
|
return plugins.loaded[name].on_webhook(subpath, request)
|
||||||
|
except Exception:
|
||||||
abort(500)
|
abort(500)
|
||||||
|
else:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
# serve a message and shuts down the unit
|
# serve a message and shuts down the unit
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user