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:
parent
7289af6560
commit
568393c941
|
@ -19,27 +19,20 @@ class PauseResume:
|
||||||
self.gcode.register_command("CLEAR_PAUSE", self.cmd_CLEAR_PAUSE)
|
self.gcode.register_command("CLEAR_PAUSE", self.cmd_CLEAR_PAUSE)
|
||||||
self.gcode.register_command("CANCEL_PRINT", self.cmd_CANCEL_PRINT)
|
self.gcode.register_command("CANCEL_PRINT", self.cmd_CANCEL_PRINT)
|
||||||
webhooks = self.printer.lookup_object('webhooks')
|
webhooks = self.printer.lookup_object('webhooks')
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint("pause_resume/cancel",
|
||||||
"pause_resume/cancel", self._handle_web_request)
|
self._handle_cancel_request)
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint("pause_resume/pause",
|
||||||
"pause_resume/pause", self._handle_web_request)
|
self._handle_pause_request)
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint("pause_resume/resume",
|
||||||
"pause_resume/resume", self._handle_web_request)
|
self._handle_resume_request)
|
||||||
def handle_ready(self):
|
def handle_ready(self):
|
||||||
self.v_sd = self.printer.lookup_object('virtual_sdcard', None)
|
self.v_sd = self.printer.lookup_object('virtual_sdcard', None)
|
||||||
def _handle_web_request(self, web_request):
|
def _handle_cancel_request(self, web_request):
|
||||||
if web_request.get_method() != 'POST':
|
self.gcode.run_script("CANCEL_PRINT")
|
||||||
raise web_request.error("Invalid Request Method")
|
def _handle_pause_request(self, web_request):
|
||||||
path = web_request.get_path()
|
self.gcode.run_script("PAUSE")
|
||||||
if path == "pause_resume/cancel":
|
def _handle_resume_request(self, web_request):
|
||||||
script = "CANCEL_PRINT"
|
self.gcode.run_script("RESUME")
|
||||||
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 get_status(self, eventtime):
|
def get_status(self, eventtime):
|
||||||
return {
|
return {
|
||||||
'is_paused': self.is_paused
|
'is_paused': self.is_paused
|
||||||
|
|
|
@ -22,8 +22,6 @@ class QueryEndstops:
|
||||||
def get_status(self, eventtime):
|
def get_status(self, eventtime):
|
||||||
return {'last_query': {name: value for name, value in self.last_state}}
|
return {'last_query': {name: value for name, value in self.last_state}}
|
||||||
def _handle_web_request(self, web_request):
|
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()
|
gc_mutex = self.printer.lookup_object('gcode').get_mutex()
|
||||||
toolhead = self.printer.lookup_object('toolhead')
|
toolhead = self.printer.lookup_object('toolhead')
|
||||||
with gc_mutex:
|
with gc_mutex:
|
||||||
|
|
|
@ -87,7 +87,7 @@ class GCodeParser:
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint(
|
||||||
"gcode/restart", self._handle_remote_restart)
|
"gcode/restart", self._handle_remote_restart)
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint(
|
||||||
"gcode/firmware_restart", self._handle_remote_restart)
|
"gcode/firmware_restart", self._handle_remote_firmware_restart)
|
||||||
# Command handling
|
# Command handling
|
||||||
self.is_printer_ready = False
|
self.is_printer_ready = False
|
||||||
self.mutex = printer.get_reactor().mutex()
|
self.mutex = printer.get_reactor().mutex()
|
||||||
|
@ -619,22 +619,13 @@ class GCodeParser:
|
||||||
gcmd.respond_info("\n".join(cmdhelp), log=False)
|
gcmd.respond_info("\n".join(cmdhelp), log=False)
|
||||||
# Webhooks
|
# Webhooks
|
||||||
def _handle_remote_help(self, web_request):
|
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))
|
web_request.send(dict(self.gcode_help))
|
||||||
def _handle_remote_restart(self, web_request):
|
def _handle_remote_restart(self, web_request):
|
||||||
if web_request.get_method() != 'POST':
|
self.run_script('restart')
|
||||||
raise web_request.error("Invalid Request Method")
|
def _handle_remote_firmware_restart(self, web_request):
|
||||||
path = web_request.get_path()
|
self.run_script('firmware_restart')
|
||||||
if path == "gcode/restart":
|
|
||||||
self.run_script('restart')
|
|
||||||
elif path == "gcode/firmware_restart":
|
|
||||||
self.run_script('firmware_restart')
|
|
||||||
def _handle_remote_script(self, web_request):
|
def _handle_remote_script(self, web_request):
|
||||||
if web_request.get_method() != 'POST':
|
self.run_script(web_request.get('script'))
|
||||||
raise web_request.error("Invalid Request Method")
|
|
||||||
script = web_request.get('script')
|
|
||||||
self.run_script(script)
|
|
||||||
|
|
||||||
# Support reading gcode from a pseudo-tty interface
|
# Support reading gcode from a pseudo-tty interface
|
||||||
class GCodeIO:
|
class GCodeIO:
|
||||||
|
|
|
@ -48,7 +48,6 @@ class WebRequest:
|
||||||
def __init__(self, base_request):
|
def __init__(self, base_request):
|
||||||
self.id = base_request['id']
|
self.id = base_request['id']
|
||||||
self.path = base_request['path']
|
self.path = base_request['path']
|
||||||
self.method = base_request['method']
|
|
||||||
self.args = base_request['args']
|
self.args = base_request['args']
|
||||||
self.response = None
|
self.response = None
|
||||||
|
|
||||||
|
@ -71,9 +70,6 @@ class WebRequest:
|
||||||
def get_path(self):
|
def get_path(self):
|
||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
def get_method(self):
|
|
||||||
return self.method
|
|
||||||
|
|
||||||
def set_error(self, error):
|
def set_error(self, error):
|
||||||
self.response = error.to_dict()
|
self.response = error.to_dict()
|
||||||
|
|
||||||
|
@ -285,8 +281,6 @@ class WebHooks:
|
||||||
web_request.send({'endpoints': self._endpoints.keys()})
|
web_request.send({'endpoints': self._endpoints.keys()})
|
||||||
|
|
||||||
def _handle_info_request(self, web_request):
|
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()
|
state_message, state = self.printer.get_state_message()
|
||||||
klipper_path = os.path.normpath(os.path.join(
|
klipper_path = os.path.normpath(os.path.join(
|
||||||
os.path.dirname(__file__), ".."))
|
os.path.dirname(__file__), ".."))
|
||||||
|
@ -299,8 +293,6 @@ class WebHooks:
|
||||||
web_request.send(response)
|
web_request.send(response)
|
||||||
|
|
||||||
def _handle_estop_request(self, web_request):
|
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")
|
self.printer.invoke_shutdown("Shutdown due to webhooks request")
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
|
@ -346,15 +338,13 @@ class StatusHandler:
|
||||||
"gcode:request_restart", self._handle_restart)
|
"gcode:request_restart", self._handle_restart)
|
||||||
|
|
||||||
# Register webhooks
|
# Register webhooks
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint("objects/list", self._handle_object_request)
|
||||||
"objects/list",
|
webhooks.register_endpoint("objects/status",
|
||||||
self._handle_object_request)
|
self._handle_status_request)
|
||||||
webhooks.register_endpoint(
|
webhooks.register_endpoint("objects/subscription",
|
||||||
"objects/status",
|
self._handle_subscription_request)
|
||||||
self._handle_status_request)
|
webhooks.register_endpoint("objects/list_subscription",
|
||||||
webhooks.register_endpoint(
|
self._handle_list_subscription_request)
|
||||||
"objects/subscription",
|
|
||||||
self._handle_subscription_request)
|
|
||||||
|
|
||||||
def _handle_ready(self):
|
def _handle_ready(self):
|
||||||
eventtime = self.reactor.monotonic()
|
eventtime = self.reactor.monotonic()
|
||||||
|
@ -396,31 +386,23 @@ class StatusHandler:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _handle_object_request(self, web_request):
|
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))
|
web_request.send(dict(self.available_objects))
|
||||||
|
|
||||||
def _handle_status_request(self, web_request):
|
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()
|
args = web_request.get_args()
|
||||||
eventtime = self.reactor.monotonic()
|
eventtime = self.reactor.monotonic()
|
||||||
result = self._process_status_request(args, eventtime)
|
result = self._process_status_request(args, eventtime)
|
||||||
web_request.send(result)
|
web_request.send(result)
|
||||||
|
|
||||||
def _handle_subscription_request(self, web_request):
|
def _handle_subscription_request(self, web_request):
|
||||||
method = web_request.get_method()
|
args = web_request.get_args()
|
||||||
if method == 'POST':
|
if args:
|
||||||
# add a subscription
|
self.add_subscripton(args)
|
||||||
args = web_request.get_args()
|
|
||||||
if args:
|
|
||||||
self.add_subscripton(args)
|
|
||||||
else:
|
|
||||||
raise web_request.error("Invalid argument")
|
|
||||||
else:
|
else:
|
||||||
# get subscription info
|
raise web_request.error("Invalid argument")
|
||||||
result = dict(self.subscriptions)
|
|
||||||
web_request.send(result)
|
def _handle_list_subscription_request(self, web_request):
|
||||||
|
web_request.send(dict(self.subscriptions))
|
||||||
|
|
||||||
def add_subscripton(self, new_sub):
|
def add_subscripton(self, new_sub):
|
||||||
if not new_sub:
|
if not new_sub:
|
||||||
|
|
Loading…
Reference in New Issue