def error_handler(err): headers = err.data.get('headers', None) messages = err.data.get('messages', ['Invalid customer request...']) logger.warning(f'Invalid input params: {messages}') if headers: return jsonify({'message': messages}), 400, headers else: return jsonify({'message': messages}), 400
def get_list(): try: user_n = get_jwt_identity() n_elements = NW_Elements.getElementList(user_id=user_n) except Exception as e: logger.warning( f'user:{user_n} elements - read action failed with errors: {e}') return {'message': str(e)}, 400 return n_elements
def get(self): user_id = get_jwt_identity() try: user = User.query.get(user_id) if not user: raise Exception('User not found.') except Exception as e: logger.warning( f'user: {user_id} - failed read profile with errors: {e}') return user
def delete_element(element_id): try: user_n = get_jwt_identity() item = NW_Elements.get(element_id, user_n) item.delete() except Exception as e: logger.warning( f'user: {user_n}, element id: {element_id} - delete action failed with errors: {e}' ) return {'message': str(e)}, 400 return '', 204
def update_list(**kwargs): try: user_n = get_jwt_identity() new_one = NW_Elements(user_id=user_n, **kwargs) new_one.save() except Exception as e: logger.warning( f'user: {user_n}, elements - create action failed with errors: {e}' ) return {'message': str(e)}, 400 return new_one
def login(**kwargs): try: user = User.authenticate(**kwargs) token = user.get_token() logger.warning(f'user: {user.email} - login user action success') except Exception as e: logger.warning( f'user: {kwargs["email"]} - login user action failed with errors: {e}' ) return {'message': str(e)}, 400 return {'access_token': token}
def register(**kwargs): try: user = User(**kwargs) session.add(user) session.commit() token = user.get_token() logger.warning( f'user: {kwargs["email"]} - register user action success') except Exception as e: logger.warning( f'user: {kwargs["email"]} - register user action failed with errors: {e}' ) return {'message': str(e)}, 400 return {'access_token': token}
from api_rest_serv import app from api_rest_serv import logger if __name__ == '__main__': logger.warning(f'Start server.') app.run(host='127.0.0.1', port='5000')