app: add support for query string type hints
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
900345e2d9
commit
aa4c13742e
|
@ -7,6 +7,7 @@
|
||||||
import os
|
import os
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
import tornado
|
import tornado
|
||||||
from inspect import isclass
|
from inspect import isclass
|
||||||
from tornado.escape import url_unescape
|
from tornado.escape import url_unescape
|
||||||
|
@ -26,6 +27,24 @@ RESERVED_ENDPOINTS = [
|
||||||
EXCLUDED_ARGS = ["_", "token", "connection_id"]
|
EXCLUDED_ARGS = ["_", "token", "connection_id"]
|
||||||
DEFAULT_KLIPPY_LOG_PATH = "/tmp/klippy.log"
|
DEFAULT_KLIPPY_LOG_PATH = "/tmp/klippy.log"
|
||||||
|
|
||||||
|
# Converts query string values with type hints
|
||||||
|
def _convert_type(value, hint):
|
||||||
|
type_funcs = {
|
||||||
|
"int": int, "float": float,
|
||||||
|
"bool": lambda x: x.lower() == "true",
|
||||||
|
"json": json.loads}
|
||||||
|
if hint not in type_funcs:
|
||||||
|
logging.info(f"No conversion method for type hint {hint}")
|
||||||
|
return value
|
||||||
|
func = type_funcs[hint]
|
||||||
|
try:
|
||||||
|
converted = func(value)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Argument conversion error: Hint: "
|
||||||
|
f"{hint}, Arg: {value}")
|
||||||
|
return value
|
||||||
|
return converted
|
||||||
|
|
||||||
# Status objects require special parsing
|
# Status objects require special parsing
|
||||||
def _status_parser(request_handler):
|
def _status_parser(request_handler):
|
||||||
request = request_handler.request
|
request = request_handler.request
|
||||||
|
@ -50,7 +69,12 @@ def _default_parser(request_handler):
|
||||||
for key in arg_list:
|
for key in arg_list:
|
||||||
if key in EXCLUDED_ARGS:
|
if key in EXCLUDED_ARGS:
|
||||||
continue
|
continue
|
||||||
args[key] = request_handler.get_argument(key)
|
key_parts = key.rsplit(":", 1)
|
||||||
|
val = request_handler.get_argument(key)
|
||||||
|
if len(key_parts) == 1:
|
||||||
|
args[key] = val
|
||||||
|
else:
|
||||||
|
args[key_parts[0]] = _convert_type(val, key_parts[1])
|
||||||
return args
|
return args
|
||||||
|
|
||||||
class MutableRouter(tornado.web.ReversibleRuleRouter):
|
class MutableRouter(tornado.web.ReversibleRuleRouter):
|
||||||
|
|
Loading…
Reference in New Issue