webhooks: Convert to a json-rpc inspired message format
Convert to a new json message format. Requests look like: {"id":14, "method": "info", "params": {}} and responses look like: {"id": 14, "result": {}} Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
2b0b30ccd5
commit
7ec2ec30e3
|
@ -45,22 +45,24 @@ class Sentinel:
|
||||||
|
|
||||||
class WebRequest:
|
class WebRequest:
|
||||||
error = WebRequestError
|
error = WebRequestError
|
||||||
def __init__(self, client_conn, base_request):
|
def __init__(self, client_conn, request):
|
||||||
self.client_conn = client_conn
|
self.client_conn = client_conn
|
||||||
self.id = base_request['id']
|
base_request = json_loads_byteified(request)
|
||||||
self.path = base_request['path']
|
self.id = base_request.get('id', None)
|
||||||
self.args = base_request['args']
|
self.method = base_request['method']
|
||||||
|
self.params = base_request.get('params', {})
|
||||||
self.response = None
|
self.response = None
|
||||||
|
self.is_error = False
|
||||||
|
|
||||||
def get_client_connection(self):
|
def get_client_connection(self):
|
||||||
return self.client_conn
|
return self.client_conn
|
||||||
|
|
||||||
def get(self, item, default=Sentinel):
|
def get(self, item, default=Sentinel):
|
||||||
if item not in self.args:
|
if item not in self.params:
|
||||||
if default == Sentinel:
|
if default == Sentinel:
|
||||||
raise WebRequestError("Invalid Argument [%s]" % item)
|
raise WebRequestError("Invalid Argument [%s]" % item)
|
||||||
return default
|
return default
|
||||||
return self.args[item]
|
return self.params[item]
|
||||||
|
|
||||||
def get_int(self, item):
|
def get_int(self, item):
|
||||||
return int(self.get(item))
|
return int(self.get(item))
|
||||||
|
@ -68,13 +70,11 @@ class WebRequest:
|
||||||
def get_float(self, item):
|
def get_float(self, item):
|
||||||
return float(self.get(item))
|
return float(self.get(item))
|
||||||
|
|
||||||
def get_args(self):
|
def get_method(self):
|
||||||
return self.args
|
return self.method
|
||||||
|
|
||||||
def get_path(self):
|
|
||||||
return self.path
|
|
||||||
|
|
||||||
def set_error(self, error):
|
def set_error(self, error):
|
||||||
|
self.is_error = True
|
||||||
self.response = error.to_dict()
|
self.response = error.to_dict()
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
|
@ -83,11 +83,16 @@ class WebRequest:
|
||||||
self.response = data
|
self.response = data
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
|
if self.id is None:
|
||||||
|
return None
|
||||||
|
rtype = "result"
|
||||||
|
if self.is_error:
|
||||||
|
rtype = "error"
|
||||||
if self.response is None:
|
if self.response is None:
|
||||||
# No error was set and the user never executed
|
# No error was set and the user never executed
|
||||||
# send, default response is "ok"
|
# send, default response is {}
|
||||||
self.response = "ok"
|
self.response = {}
|
||||||
return {"request_id": self.id, "response": self.response}
|
return {"id": self.id, rtype: self.response}
|
||||||
|
|
||||||
class ServerSocket:
|
class ServerSocket:
|
||||||
def __init__(self, webhooks, printer):
|
def __init__(self, webhooks, printer):
|
||||||
|
@ -194,7 +199,7 @@ class ClientConnection:
|
||||||
logging.debug(
|
logging.debug(
|
||||||
"webhooks: Request received: %s" % (req))
|
"webhooks: Request received: %s" % (req))
|
||||||
try:
|
try:
|
||||||
web_request = WebRequest(self, json_loads_byteified(req))
|
web_request = WebRequest(self, req)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(
|
logging.exception(
|
||||||
"webhooks: Error decoding Server Request %s"
|
"webhooks: Error decoding Server Request %s"
|
||||||
|
@ -205,20 +210,20 @@ class ClientConnection:
|
||||||
|
|
||||||
def _process_request(self, web_request):
|
def _process_request(self, web_request):
|
||||||
try:
|
try:
|
||||||
func = self.webhooks.get_callback(
|
func = self.webhooks.get_callback(web_request.get_method())
|
||||||
web_request.get_path())
|
|
||||||
func(web_request)
|
func(web_request)
|
||||||
except homing.CommandError as e:
|
except homing.CommandError as e:
|
||||||
web_request.set_error(WebRequestError(e.message))
|
web_request.set_error(WebRequestError(e.message))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
msg = "Internal Error on WebRequest: %s" % (web_request.get_path())
|
msg = ("Internal Error on WebRequest: %s"
|
||||||
|
% (web_request.get_method()))
|
||||||
logging.exception(msg)
|
logging.exception(msg)
|
||||||
web_request.set_error(WebRequestError(e.message))
|
web_request.set_error(WebRequestError(e.message))
|
||||||
self.printer.invoke_shutdown(msg)
|
self.printer.invoke_shutdown(msg)
|
||||||
result = web_request.finish()
|
result = web_request.finish()
|
||||||
logging.debug(
|
if result is None:
|
||||||
"webhooks: Sending response - %s" % (str(result)))
|
return
|
||||||
self.send({'method': "response", 'params': result})
|
self.send(result)
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
self.send_buffer += json.dumps(data) + "\x03"
|
self.send_buffer += json.dumps(data) + "\x03"
|
||||||
|
|
Loading…
Reference in New Issue