From 26b34801ca54cbeca78c6807a14ed7822ebe9c3f Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 16:14:03 +0200 Subject: Add /api path to Flask app --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index 56f8e7c1..600aea92 100644 --- a/main.py +++ b/main.py @@ -102,6 +102,12 @@ def sign_in(): return jsonify(**data) +@FLASK_CORE_APP.route('/api/') +def api_call(endpoint_path): + """Call an API endpoint directly over HTTP""" + + return endpoint_path + @SOCKET_IO_CORE.on('request') def receive_message(message): """"Receive a SocketIO request""" @@ -149,4 +155,3 @@ def receive_message(message): SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) - -- cgit v1.2.3 From f240f3534e2db7d88242e05662fbedda1c2b4306 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:19:04 +0200 Subject: Map HTTP endpoint calls to API paths --- main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 600aea92..404665b0 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ import flask_socketio from oauth2client import client, crypt from opendc.models.user import User -from opendc.util import exceptions, rest +from opendc.util import exceptions, rest, path_parser if len(sys.argv) < 2: print "config file path not given as argument" @@ -102,11 +102,13 @@ def sign_in(): return jsonify(**data) -@FLASK_CORE_APP.route('/api/') -def api_call(endpoint_path): +@FLASK_CORE_APP.route('/api//') +def api_call(version, endpoint_path): """Call an API endpoint directly over HTTP""" - return endpoint_path + path = path_parser.parse(version, endpoint_path) + + return jsonify(path) @SOCKET_IO_CORE.on('request') def receive_message(message): -- cgit v1.2.3 From 0b3a3f4a62744fdc5b2f7282b0bcb8886f1eeacf Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:42:43 +0200 Subject: Add path parameter extraction --- main.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 404665b0..1db07b41 100644 --- a/main.py +++ b/main.py @@ -106,9 +106,18 @@ def sign_in(): def api_call(version, endpoint_path): """Call an API endpoint directly over HTTP""" - path = path_parser.parse(version, endpoint_path) - - return jsonify(path) + (path, path_parameters) = path_parser.parse(version, endpoint_path) + + message = { + 'id': 0, + 'method': request.method, + 'path': path, + 'parameters': { + 'path': path_parameters + } + } + + return jsonify(message) @SOCKET_IO_CORE.on('request') def receive_message(message): -- cgit v1.2.3 From 3cd761d8c4c803e67c5b72be0bd65b997b265a6a Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 18:49:54 +0200 Subject: Add query parameter parsing --- main.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 1db07b41..a8048312 100644 --- a/main.py +++ b/main.py @@ -108,13 +108,21 @@ def api_call(version, endpoint_path): (path, path_parameters) = path_parser.parse(version, endpoint_path) + query_parameters = request.args.to_dict() + for param in query_parameters: + try: + query_parameters[param] = int(query_parameters[param]) + except: + pass + message = { 'id': 0, 'method': request.method, - 'path': path, 'parameters': { - 'path': path_parameters - } + 'path': path_parameters, + 'query': query_parameters + }, + 'path': path } return jsonify(message) -- cgit v1.2.3 From 4ab2280c7282944997a7d68414761399e5955e89 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 20:30:43 +0200 Subject: Allow GET, POST, PUT and DELETE for API calls --- main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index a8048312..50ded468 100644 --- a/main.py +++ b/main.py @@ -102,10 +102,11 @@ def sign_in(): return jsonify(**data) -@FLASK_CORE_APP.route('/api//') +@FLASK_CORE_APP.route('/api//', methods = ['GET', 'POST', 'PUT', 'DELETE']) def api_call(version, endpoint_path): """Call an API endpoint directly over HTTP""" + # Get path and parameters (path, path_parameters) = path_parser.parse(version, endpoint_path) query_parameters = request.args.to_dict() @@ -114,7 +115,8 @@ def api_call(version, endpoint_path): query_parameters[param] = int(query_parameters[param]) except: pass - + + # Create and call request message = { 'id': 0, 'method': request.method, -- cgit v1.2.3 From 5b88d7f1af2ba3d3506fbb3c6319ccf3df0d4380 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 20:40:33 +0200 Subject: Add body parameter parsing --- main.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'main.py') diff --git a/main.py b/main.py index 50ded468..203d0229 100644 --- a/main.py +++ b/main.py @@ -115,12 +115,15 @@ def api_call(version, endpoint_path): query_parameters[param] = int(query_parameters[param]) except: pass + + body_parameters = json.loads(request.get_data()) # Create and call request message = { 'id': 0, 'method': request.method, 'parameters': { + 'body': body_parameters, 'path': path_parameters, 'query': query_parameters }, -- cgit v1.2.3 From e6ced176b8bcfd4fb2aa9ca96cfe956d04067fe0 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 21:07:40 +0200 Subject: Pull message processing into method --- main.py | 56 ++++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 203d0229..d30dac64 100644 --- a/main.py +++ b/main.py @@ -136,46 +136,42 @@ def api_call(version, endpoint_path): def receive_message(message): """"Receive a SocketIO request""" - try: - request = rest.Request(message) - response = request.process() - - flask_socketio.emit('response', response.to_JSON(), json=True) + (request, response) = _process_message(message) - print 'Socket: {} to `/{}` resulted in {}: {}'.format( - request.method, - request.path, - response.status['code'], - response.status['description'] - ) + print 'Socket: {} to `/{}` resulted in {}: {}'.format( + request.method, + request.path, + response.status['code'], + response.status['description'] + ) + sys.stdout.flush() - return + flask_socketio.emit('response', response.to_JSON(), json=True) - except exceptions.AuthorizationTokenError as e: - response = rest.Response(401, 'Authorization error') - response.id = message['id'] +def _process_message(message): + """Process a request message and return the response.""" - flask_socketio.emit('response', response.to_JSON(), json=True) + try: + request = rest.Request(message) + response = request.process() + return (request, response) + + except exceptions.AuthorizationTokenError as e: + response = rest.Response(401, 'Authorization error') + except exceptions.RequestInitializationError as e: response = rest.Response(400, e.message) - response.id = message['id'] - - flask_socketio.emit('response', response.to_JSON(), json=True) + if not 'method' in message: + message['method'] = 'UNSPECIFIED' + if not 'path' in message: + message['path'] = 'UNSPECIFIED' + except Exception as e: response = rest.Response(500, 'Internal server error') - response.id = message['id'] - - flask_socketio.emit('response', response.to_JSON(), json=True) traceback.print_exc() - - print 'Socket: {} to `{}` resulted in {}: {}'.format( - message['method'], - message['path'], - response.status['code'], - response.status['description'] - ) - + + return ({'method': message['method'], 'path': message['path']}, response) SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) -- cgit v1.2.3 From bb4a12a0cf9c412c57879229226762f06c3390e7 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 21:31:44 +0200 Subject: Process requests over HTTP --- main.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index d30dac64..1546a397 100644 --- a/main.py +++ b/main.py @@ -119,7 +119,7 @@ def api_call(version, endpoint_path): body_parameters = json.loads(request.get_data()) # Create and call request - message = { + (req, response) = _process_message({ 'id': 0, 'method': request.method, 'parameters': { @@ -127,10 +127,19 @@ def api_call(version, endpoint_path): 'path': path_parameters, 'query': query_parameters }, - 'path': path - } + 'path': path, + 'token': request.headers.get('auth-token') + }) + + print 'HTTP:\t{} to `/{}` resulted in {}: {}'.format( + req.method, + req.path, + response.status['code'], + response.status['description'] + ) + sys.stdout.flush() - return jsonify(message) + return jsonify(json.loads(response.to_JSON())) @SOCKET_IO_CORE.on('request') def receive_message(message): @@ -138,7 +147,7 @@ def receive_message(message): (request, response) = _process_message(message) - print 'Socket: {} to `/{}` resulted in {}: {}'.format( + print 'Socket:\t{} to `/{}` resulted in {}: {}'.format( request.method, request.path, response.status['code'], @@ -159,9 +168,11 @@ def _process_message(message): except exceptions.AuthorizationTokenError as e: response = rest.Response(401, 'Authorization error') - + response.id = message['id'] + except exceptions.RequestInitializationError as e: response = rest.Response(400, e.message) + response.id = message['id'] if not 'method' in message: message['method'] = 'UNSPECIFIED' @@ -170,6 +181,8 @@ def _process_message(message): except Exception as e: response = rest.Response(500, 'Internal server error') + if 'id' in message: + response.id = message['id'] traceback.print_exc() return ({'method': message['method'], 'path': message['path']}, response) -- cgit v1.2.3 From a06c408964dab6069b894c8fe11d09afdd7f9b81 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 21:36:22 +0200 Subject: Replace web server test w/ auth token page --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index 1546a397..d49e4191 100644 --- a/main.py +++ b/main.py @@ -55,7 +55,7 @@ def serve_projects(): return send_from_directory(STATIC_ROOT, 'projects.html') -@FLASK_CORE_APP.route('/web-server-test') +@FLASK_CORE_APP.route('/my-auth-token') def serve_web_server_test(): """Serve the web server test.""" -- cgit v1.2.3 From 2162154a5152ea2acba40279befd58656846766a Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Sat, 1 Apr 2017 21:40:06 +0200 Subject: Make HTTP API calls robust to bad JSON --- main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index d49e4191..41799dae 100644 --- a/main.py +++ b/main.py @@ -116,7 +116,10 @@ def api_call(version, endpoint_path): except: pass - body_parameters = json.loads(request.get_data()) + try: + body_parameters = json.loads(request.get_data()) + except: + body_parameters = {} # Create and call request (req, response) = _process_message({ -- cgit v1.2.3 From 8801bbd2f3d1339f5a5ecad48e8f51f68b146604 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Mon, 3 Apr 2017 09:55:18 +0200 Subject: Fix error handling in HTTP requests Now return the proper status code instead of 500 --- main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 41799dae..ec404239 100644 --- a/main.py +++ b/main.py @@ -168,7 +168,7 @@ def _process_message(message): response = request.process() return (request, response) - + except exceptions.AuthorizationTokenError as e: response = rest.Response(401, 'Authorization error') response.id = message['id'] @@ -188,6 +188,10 @@ def _process_message(message): response.id = message['id'] traceback.print_exc() - return ({'method': message['method'], 'path': message['path']}, response) + request = rest.Request() + request.method = message['method'] + request.path = message['path'] + + return (request, response) SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) -- cgit v1.2.3 From fd0376c5a8f018457a11bb0f9602bda366571955 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Tue, 4 Apr 2017 15:23:00 +0200 Subject: Return proper status codes of HTTP Instead of always 200 --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index ec404239..3dbb2a53 100644 --- a/main.py +++ b/main.py @@ -142,7 +142,9 @@ def api_call(version, endpoint_path): ) sys.stdout.flush() - return jsonify(json.loads(response.to_JSON())) + flask_response = jsonify(json.loads(response.to_JSON())) + flask_response.status_code = response.status['code'] + return flask_response @SOCKET_IO_CORE.on('request') def receive_message(message): -- cgit v1.2.3