aboutsummaryrefslogtreecommitdiffstats
path: root/gui/api/__init__.py
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2018-09-24 19:42:01 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2018-09-24 20:47:08 +0200
commit5ad01f6415710521173e7a612b32c982847eada4 (patch)
tree4d4c8ca0a7ce0ff0f81702ee7bfad0b57c899929 /gui/api/__init__.py
parent5bb71419937a4d8919ef33df088f73e6c705b028 (diff)
downloaddabmod-5ad01f6415710521173e7a612b32c982847eada4.tar.gz
dabmod-5ad01f6415710521173e7a612b32c982847eada4.tar.bz2
dabmod-5ad01f6415710521173e7a612b32c982847eada4.zip
gui: use json for all API answers
Diffstat (limited to 'gui/api/__init__.py')
-rwxr-xr-xgui/api/__init__.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/gui/api/__init__.py b/gui/api/__init__.py
index 6c04a64..74f5feb 100755
--- a/gui/api/__init__.py
+++ b/gui/api/__init__.py
@@ -30,6 +30,12 @@ import os
import io
import datetime
+def send_ok(data):
+ return json.dumps({'status' : 'ok', 'data': data}).encode()
+
+def send_error(data, reason=""):
+ return json.dumps({'status' : 'error', 'reason': reason}).encode()
+
class API:
def __init__(self, mod_rc, dpd):
self.mod_rc = mod_rc
@@ -42,7 +48,7 @@ class API:
@cherrypy.expose
def rc_parameters(self):
cherrypy.response.headers["Content-Type"] = "application/json"
- return json.dumps(self.mod_rc.get_modules()).encode()
+ return send_ok(self.mod_rc.get_modules())
@cherrypy.expose
def parameter(self, **kwargs):
@@ -55,24 +61,24 @@ class API:
self.mod_rc.set_param_value(params['controllable'], params['param'], params['value'])
except ValueError as e:
cherrypy.response.status = 400
- return "{}".format(e)
- return json.dumps("ok").encode()
+ return send_error(str(e))
+ return send_ok(None)
else:
cherrypy.response.headers["Content-Type"] = "application/json"
cherrypy.response.status = 400
- return json.dumps("POST only").encode()
+ return send_error("POST only")
@cherrypy.expose
def trigger_capture(self, **kwargs):
if cherrypy.request.method == 'POST':
- cherrypy.response.headers["Content-Type"] = "text/plain"
- return self.dpd.capture_samples()
+ cherrypy.response.headers["Content-Type"] = "application/json"
+ return send_ok(self.dpd.capture_samples())
else:
- cherrypy.response.headers["Content-Type"] = "text/plain"
+ cherrypy.response.headers["Content-Type"] = "application/json"
cherrypy.response.status = 400
- return "POST only"
+ return send_error("POST only")
@cherrypy.expose
def dpd_status(self, **kwargs):
cherrypy.response.headers["Content-Type"] = "application/json"
- return json.dumps(self.dpd.status()).encode()
+ return send_ok(self.dpd.status())