diff options
| author | leonoverweel <l.overweel@gmail.com> | 2017-04-01 21:07:40 +0200 |
|---|---|---|
| committer | leonoverweel <l.overweel@gmail.com> | 2017-04-01 21:07:40 +0200 |
| commit | e6ced176b8bcfd4fb2aa9ca96cfe956d04067fe0 (patch) | |
| tree | 9ca4c0def4244d98a00d03ab3c9373cb5ccffe32 /main.py | |
| parent | 5b88d7f1af2ba3d3506fbb3c6319ccf3df0d4380 (diff) | |
Pull message processing into method
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 56 |
1 files changed, 26 insertions, 30 deletions
@@ -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) |
