webhooks: Remove "method" parameter from webhook requests

Don't require or use the "method" parameter of requests.  This
simplifies the interface.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-08-11 16:54:17 -04:00
parent 7289af6560
commit 568393c941
4 changed files with 31 additions and 67 deletions

View File

@ -19,27 +19,20 @@ class PauseResume:
self.gcode.register_command("CLEAR_PAUSE", self.cmd_CLEAR_PAUSE)
self.gcode.register_command("CANCEL_PRINT", self.cmd_CANCEL_PRINT)
webhooks = self.printer.lookup_object('webhooks')
webhooks.register_endpoint(
"pause_resume/cancel", self._handle_web_request)
webhooks.register_endpoint(
"pause_resume/pause", self._handle_web_request)
webhooks.register_endpoint(
"pause_resume/resume", self._handle_web_request)
webhooks.register_endpoint("pause_resume/cancel",
self._handle_cancel_request)
webhooks.register_endpoint("pause_resume/pause",
self._handle_pause_request)
webhooks.register_endpoint("pause_resume/resume",
self._handle_resume_request)
def handle_ready(self):
self.v_sd = self.printer.lookup_object('virtual_sdcard', None)
def _handle_web_request(self, web_request):
if web_request.get_method() != 'POST':
raise web_request.error("Invalid Request Method")
path = web_request.get_path()
if path == "pause_resume/cancel":
script = "CANCEL_PRINT"
elif path == "pause_resume/pause":
script = "PAUSE"
elif path == "pause_resume/resume":
script = "RESUME"
else:
raise web_request.error("Invalid Path")
self.gcode.run_script(script)
def _handle_cancel_request(self, web_request):
self.gcode.run_script("CANCEL_PRINT")
def _handle_pause_request(self, web_request):
self.gcode.run_script("PAUSE")
def _handle_resume_request(self, web_request):
self.gcode.run_script("RESUME")
def get_status(self, eventtime):
return {
'is_paused': self.is_paused

View File

@ -22,8 +22,6 @@ class QueryEndstops:
def get_status(self, eventtime):
return {'last_query': {name: value for name, value in self.last_state}}
def _handle_web_request(self, web_request):
if web_request.get_method() != 'GET':
raise web_request.error("Invalid Request Method")
gc_mutex = self.printer.lookup_object('gcode').get_mutex()
toolhead = self.printer.lookup_object('toolhead')
with gc_mutex:

View File

@ -87,7 +87,7 @@ class GCodeParser:
webhooks.register_endpoint(
"gcode/restart", self._handle_remote_restart)
webhooks.register_endpoint(
"gcode/firmware_restart", self._handle_remote_restart)
"gcode/firmware_restart", self._handle_remote_firmware_restart)
# Command handling
self.is_printer_ready = False
self.mutex = printer.get_reactor().mutex()
@ -619,22 +619,13 @@ class GCodeParser:
gcmd.respond_info("\n".join(cmdhelp), log=False)
# Webhooks
def _handle_remote_help(self, web_request):
if web_request.get_method() != 'GET':
raise web_request.error("Invalid Request Method")
web_request.send(dict(self.gcode_help))
def _handle_remote_restart(self, web_request):
if web_request.get_method() != 'POST':
raise web_request.error("Invalid Request Method")
path = web_request.get_path()
if path == "gcode/restart":
self.run_script('restart')
elif path == "gcode/firmware_restart":
self.run_script('firmware_restart')
self.run_script('restart')
def _handle_remote_firmware_restart(self, web_request):
self.run_script('firmware_restart')
def _handle_remote_script(self, web_request):
if web_request.get_method() != 'POST':
raise web_request.error("Invalid Request Method")
script = web_request.get('script')
self.run_script(script)
self.run_script(web_request.get('script'))
# Support reading gcode from a pseudo-tty interface
class GCodeIO:

View File

@ -48,7 +48,6 @@ class WebRequest:
def __init__(self, base_request):
self.id = base_request['id']
self.path = base_request['path']
self.method = base_request['method']
self.args = base_request['args']
self.response = None
@ -71,9 +70,6 @@ class WebRequest:
def get_path(self):
return self.path
def get_method(self):
return self.method
def set_error(self, error):
self.response = error.to_dict()
@ -285,8 +281,6 @@ class WebHooks:
web_request.send({'endpoints': self._endpoints.keys()})
def _handle_info_request(self, web_request):
if web_request.get_method() != 'GET':
raise web_request.error("Invalid Request Method")
state_message, state = self.printer.get_state_message()
klipper_path = os.path.normpath(os.path.join(
os.path.dirname(__file__), ".."))
@ -299,8 +293,6 @@ class WebHooks:
web_request.send(response)
def _handle_estop_request(self, web_request):
if web_request.get_method() != 'POST':
raise web_request.error("Invalid Request Method")
self.printer.invoke_shutdown("Shutdown due to webhooks request")
def get_connection(self):
@ -346,15 +338,13 @@ class StatusHandler:
"gcode:request_restart", self._handle_restart)
# Register webhooks
webhooks.register_endpoint(
"objects/list",
self._handle_object_request)
webhooks.register_endpoint(
"objects/status",
self._handle_status_request)
webhooks.register_endpoint(
"objects/subscription",
self._handle_subscription_request)
webhooks.register_endpoint("objects/list", self._handle_object_request)
webhooks.register_endpoint("objects/status",
self._handle_status_request)
webhooks.register_endpoint("objects/subscription",
self._handle_subscription_request)
webhooks.register_endpoint("objects/list_subscription",
self._handle_list_subscription_request)
def _handle_ready(self):
eventtime = self.reactor.monotonic()
@ -396,31 +386,23 @@ class StatusHandler:
return result
def _handle_object_request(self, web_request):
if web_request.get_method() != 'GET':
raise web_request.error("Invalid Request Method")
web_request.send(dict(self.available_objects))
def _handle_status_request(self, web_request):
if web_request.get_method() != 'GET':
raise web_request.error("Invalid Request Method")
args = web_request.get_args()
eventtime = self.reactor.monotonic()
result = self._process_status_request(args, eventtime)
web_request.send(result)
def _handle_subscription_request(self, web_request):
method = web_request.get_method()
if method == 'POST':
# add a subscription
args = web_request.get_args()
if args:
self.add_subscripton(args)
else:
raise web_request.error("Invalid argument")
args = web_request.get_args()
if args:
self.add_subscripton(args)
else:
# get subscription info
result = dict(self.subscriptions)
web_request.send(result)
raise web_request.error("Invalid argument")
def _handle_list_subscription_request(self, web_request):
web_request.send(dict(self.subscriptions))
def add_subscripton(self, new_sub):
if not new_sub: