From cf963db31d603abbd93ee56271a16603b525dad5 Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Tue, 12 Oct 2021 15:59:24 +0200 Subject: Flatten GET output --- src/wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/wsgi.py') diff --git a/src/wsgi.py b/src/wsgi.py index 54d18d3..2728d23 100755 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -70,7 +70,7 @@ class EPGet(CollectorResource): selectors['domain'] = org data = self._db.search(**selectors) if data: - out.append(data) + out += data resp.text = json.dumps({'status': 'success', 'data': out}) -- cgit v1.1 From 9b42192e54b409b7d6c1a12a8d802bc1edbaf37c Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Wed, 13 Oct 2021 08:49:34 +0200 Subject: Now possible to get data based on a single key. --- src/wsgi.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/wsgi.py') diff --git a/src/wsgi.py b/src/wsgi.py index 54d18d3..97965e1 100755 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -46,7 +46,7 @@ class CollectorResource(): class EPGet(CollectorResource): - def on_get(self, req, resp): + def on_get(self, req, resp, key=None): out = list() selectors = dict() @@ -60,6 +60,11 @@ class EPGet(CollectorResource): {'status': 'error', 'message': 'Invalid username or password\n'}) return + if key: + out = self._db.get(key) + resp.text = json.dumps({'status': 'success', 'data': out}) + return + for param in req.params: for i in index.indexes: for j in i['index']['fields']: @@ -131,7 +136,8 @@ def main(port=8000, wsgi_helper=False): users = authn.UserDB('wsgi_demo_users.yaml') resources_map = [ ('/sc/v0/add', EPAdd(db, users)), - ('/sc/v0/get', EPGet(db, users)) + ('/sc/v0/get', EPGet(db, users)), + ('/sc/v0/get/{key}', EPGet(db, users)) ] app = falcon.App(cors_enable=True) -- cgit v1.1 From 583542443adb2d66208dacb71581bc2936c59201 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Thu, 14 Oct 2021 09:48:17 +0200 Subject: Find, now with pagination! Use limit and skip to get the results you want. --- src/wsgi.py | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'src/wsgi.py') diff --git a/src/wsgi.py b/src/wsgi.py index 62db3c9..b547ffa 100755 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -33,9 +33,11 @@ class CollectorResource(): def user_auth(self, auth_header, authfun): if not auth_header: return None, None # Fail. + BAlit, b64 = auth_header.split() if BAlit != "Basic": return None, None # Fail + userbytes, pwbytes = b64decode(b64).split(b':') try: user = userbytes.decode('utf-8') @@ -50,14 +52,19 @@ class EPGet(CollectorResource): out = list() selectors = dict() + limit = 25 + skip = 0 + resp.status = falcon.HTTP_200 resp.content_type = falcon.MEDIA_JSON orgs = self.user_auth(req.auth, self._users.read_perms) if not orgs: resp.status = falcon.HTTP_401 - resp.text = json.dumps( - {'status': 'error', 'message': 'Invalid username or password\n'}) + resp.text = json.dumps({ + 'status': 'error', + 'message': 'Invalid username or password\n' + }) return if key: @@ -66,6 +73,10 @@ class EPGet(CollectorResource): return for param in req.params: + if param == 'limit': + limit = req.params['limit'] + elif param == 'skip': + skip = req.params['skip'] for i in index.indexes: for j in i['index']['fields']: if j == param: @@ -73,7 +84,7 @@ class EPGet(CollectorResource): for org in orgs: selectors['domain'] = org - data = self._db.search(**selectors) + data = self._db.search(**selectors, limit=limit, skip=skip) if data: out += data @@ -103,6 +114,7 @@ class EPAdd(CollectorResource): # NOTE: Reading the whole body in one go instead of streaming # it nicely. rawin = req.bounded_stream.read() + try: decodedin = rawin.decode('UTF-8') except Exception: @@ -134,25 +146,24 @@ class EPAdd(CollectorResource): def main(port=8000, wsgi_helper=False): db = DictDB(database, hostname, username, password) users = authn.UserDB('wsgi_demo_users.yaml') - resources_map = [ - ('/sc/v0/add', EPAdd(db, users)), - ('/sc/v0/get', EPGet(db, users)), - ('/sc/v0/get/{key}', EPGet(db, users)) - ] - app = falcon.App(cors_enable=True) - for url, res in resources_map: - app.add_route(url, res) + app = falcon.App(cors_enable=True) + app.add_route('/sc/v0/add', EPAdd(db, users)) + app.add_route('/sc/v0/get', EPGet(db, users)) + app.add_route('/sc/v0/get/{key}', EPGet(db, users)) - if not wsgi_helper: - print('Serving on port 8000...') - httpd = make_server('', port, app) - httpd.serve_forever() + if wsgi_helper: + return app - return app + print('Serving on port 8000...') + httpd = make_server('', port, app) + httpd.serve_forever() if __name__ == '__main__': - sys.exit(main()) + try: + sys.exit(main()) + except KeyboardInterrupt: + print('\nBye!') else: app = main(port=8000, wsgi_helper=True) -- cgit v1.1 From 34a353a539f71b6a87413b58ea483b36f94e3516 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Mon, 18 Oct 2021 15:15:09 +0200 Subject: Remove unused variables. --- src/wsgi.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/wsgi.py') diff --git a/src/wsgi.py b/src/wsgi.py index b547ffa..8ab178a 100755 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -55,8 +55,6 @@ class EPGet(CollectorResource): limit = 25 skip = 0 - resp.status = falcon.HTTP_200 - resp.content_type = falcon.MEDIA_JSON orgs = self.user_auth(req.auth, self._users.read_perms) if not orgs: -- cgit v1.1