Module: check_mk
Branch: master
Commit: 3dc46d0e068b9c5fb954a6fed827395e9c1a3e9f
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3dc46d0e068b9c…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Dec 12 11:37:00 2018 +0100
Allow XML output for our web API.
CMK-1403
Change-Id: Ic4cad7e686b58cece59b321d10bbb40cd7699368
---
cmk/gui/webapi.py | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/cmk/gui/webapi.py b/cmk/gui/webapi.py
index 8eefaf7..14a1070 100644
--- a/cmk/gui/webapi.py
+++ b/cmk/gui/webapi.py
@@ -27,6 +27,8 @@
import traceback
import json
+import dicttoxml # type: ignore
+
import cmk
import cmk.gui.pages
@@ -76,15 +78,21 @@ def load_plugins(force):
config.builtin_role_ids)
+FORMATTERS = {
+ "json": json.dumps,
+ "python": repr,
+ "xml": dicttoxml.dicttoxml,
+}
+
+
@cmk.gui.pages.register("webapi")
def page_api():
try:
- # The API uses JSON format by default and python as optional alternative
- output_format = html.var("output_format", "json")
- if output_format not in ["json", "python"]:
- raise MKUserError(None, "Only \"json\" and
\"python\" are supported as output formats")
- else:
- html.set_output_format(output_format)
+ 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))
if not config.user.get_attribute("automation_secret"):
raise MKAuthException("The WATO API is only available for automation
users")
@@ -161,7 +169,4 @@ def page_api():
"result": _("Unhandled exception: %s") %
traceback.format_exc(),
}
- if html.output_format == "json":
- html.write(json.dumps(response))
- else:
- html.write(repr(response))
+ html.write(FORMATTERS[html.output_format](response))