summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristofer Hallin <kristofer@sunet.se>2021-10-06 13:44:35 +0200
committerKristofer Hallin <kristofer@sunet.se>2021-10-06 13:44:35 +0200
commitba8ec01914aa7d8f122f9a968199ffc04b08ec69 (patch)
tree4ffc0831f138402d6f695dd832402ba724b151e8
parent994d0293da811d443f2803bc49c14568835de3f1 (diff)
Send back proper JSON to the user.
-rwxr-xr-xsrc/wsgi.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/wsgi.py b/src/wsgi.py
index b7de59f..e1f645c 100755
--- a/src/wsgi.py
+++ b/src/wsgi.py
@@ -53,7 +53,8 @@ class EPGet(CollectorResource):
if not orgs:
resp.status = falcon.HTTP_401
- resp.text = 'Invalid username or password\n'
+ resp.text = json.dumps(
+ {'status': 'error', 'message': 'Invalid username or password\n'})
return
# We really shouldn't rely on req.params in its pure form since
@@ -62,9 +63,11 @@ class EPGet(CollectorResource):
for org in orgs:
selectors['domain'] = org
- out.append(self._db.search(**selectors))
+ data = self._db.search(**selectors)
+ if data:
+ out.append(data)
- resp.text = json.dumps(out) + '\n'
+ resp.text = json.dumps({'status': 'success', 'data': out})
class EPAdd(CollectorResource):
@@ -76,7 +79,8 @@ class EPAdd(CollectorResource):
orgs = self.user_auth(req.auth, self._users.write_perms)
if not orgs:
resp.status = falcon.HTTP_401
- resp.text = 'Invalid user or password\n'
+ resp.text = json.dumps(
+ {'status': 'error', 'message': 'Invalid user or password\n'})
return
# NOTE: Allowing writing to _any_ org!
@@ -93,7 +97,8 @@ class EPAdd(CollectorResource):
decodedin = rawin.decode('UTF-8')
except Exception:
resp.status = falcon.HTTP_400
- resp.text = 'Need UTF-8\n'
+ resp.text = json.dumps(
+ {'status': 'error', 'message': 'Need UTF-8\n'})
return
try:
@@ -101,16 +106,19 @@ class EPAdd(CollectorResource):
except TypeError:
print('DEBUG: type error')
resp.status = falcon.HTTP_400
- resp.text = CollectorResource.parse_error(decodedin)
+ resp.text = json.dumps(
+ {'status': 'error', 'message': CollectorResource.parse_error(decodedin)})
+
return
except json.decoder.JSONDecodeError:
print('DEBUG: json decode error')
resp.status = falcon.HTTP_400
- resp.text = CollectorResource.parse_error(decodedin)
+ resp.text = json.dumps(
+ {'status': 'error', 'message': CollectorResource.parse_error(decodedin)})
return
keys = self._db.add(json_data)
- resp.text = repr(keys) + '\n'
+ resp.text = json.dumps({'status': 'success', 'key': keys})
def main(port=8000, wsgi_helper=False):