From ba8ec01914aa7d8f122f9a968199ffc04b08ec69 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Wed, 6 Oct 2021 13:44:35 +0200 Subject: Send back proper JSON to the user. --- src/wsgi.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/wsgi.py') 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): -- cgit v1.1