Module: check_mk
Branch: master
Commit: d62adc2a06a593a9c6fcb587e5fbdf7d654aad3a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d62adc2a06a593…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Dec 12 14:06:33 2018 +0100
Added pretty_print parameter to web API.
CMK-1403
Change-Id: If895e506399af13950a8433ce809284060f732a5
---
cmk/gui/webapi.py | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/cmk/gui/webapi.py b/cmk/gui/webapi.py
index 14a1070..89cd3c3 100644
--- a/cmk/gui/webapi.py
+++ b/cmk/gui/webapi.py
@@ -26,6 +26,8 @@
import traceback
import json
+import pprint
+import xml.dom.minidom # type: ignore
import dicttoxml # type: ignore
@@ -79,21 +81,32 @@ def load_plugins(force):
FORMATTERS = {
- "json": json.dumps,
- "python": repr,
- "xml": dicttoxml.dicttoxml,
+ "json": (
+ json.dumps,
+ lambda response: json.dumps(response, sort_keys=True, indent=4,
separators=(',', ': '))),
+ "python": (repr, pprint.pformat),
+ "xml": (
+ dicttoxml.dicttoxml,
+ lambda response:
xml.dom.minidom.parseString(dicttoxml.dicttoxml(response)).toprettyxml()),
}
@cmk.gui.pages.register("webapi")
def page_api():
try:
+ pretty_print = False
if html.output_format not in FORMATTERS:
html.set_output_format("python")
raise MKUserError(
None, "Only %s are supported as output formats" % " and
".join(
'"%s"' % f for f in FORMATTERS))
+ # TODO: Add some kind of helper for boolean-valued variables?
+ pretty_print_var = html.request.var("pretty_print",
"no").lower()
+ if pretty_print_var not in ("yes", "no"):
+ raise MKUserError(None, 'pretty_print must be "yes" or
"no"')
+ pretty_print = pretty_print_var == "yes"
+
if not config.user.get_attribute("automation_secret"):
raise MKAuthException("The WATO API is only available for automation
users")
@@ -121,7 +134,7 @@ def page_api():
if html.var("request"):
request_object = html.var("request")
else:
- request_object = html.get_request(exclude_vars=["action"])
+ request_object = html.get_request(exclude_vars=["action",
"pretty_print"])
# Check if the data was sent with the correct data format
# Some API calls only allow python code
@@ -169,4 +182,4 @@ def page_api():
"result": _("Unhandled exception: %s") %
traceback.format_exc(),
}
- html.write(FORMATTERS[html.output_format](response))
+ html.write(FORMATTERS[html.output_format][1 if pretty_print else 0](response))