def wrapper(*args, **kwargs): json_data = request.get_json() if not json_data: return error(status=400, detail='Bad request') event, errors = EventSchema().load(json_data) if errors: return error(status=400, detail=_errors_to_string(errors)) kwargs['event'] = event return func(*args, **kwargs)
def wrapper(*args, **kwargs): json_data = request.get_json() if not json_data: return error(status=400, detail='Bad request') user, errors = UserSchema().load(json_data) if errors: return error(status=400, detail=_errors_to_string(errors)) kwargs['user'] = user return func(*args, **kwargs)
def wrapper(*args, **kwargs): json_data = request.get_json() if not json_data or 'n_stocks' not in json_data: return error(status=400, detail='Bad request') n_stocks = json_data['n_stocks'] if not type(n_stocks) is int: return error(status=400, detail='Bad request') kwargs['n_stocks'] = n_stocks return func(*args, **kwargs)
def wrapper(*args, **kwargs): json_data = request.get_json() if not json_data: return error(status=400, detail='Bad request') news, errors = NewsSchema().load(json_data) if errors: return error(status=400, detail=_errors_to_string(errors)) kwargs['news'] = news return func(*args, **kwargs)
def wrapper(*args, **kwargs): json_data = request.get_json() if not json_data: return error(status=400, detail='Bad request') if not 'role' in json_data: return error(status=400, detail='Role missing') if json_data.get('role') not in ('USER', 'ADMIN'): return error(status=400, detail='Role not valid') kwargs['new_user'] = json_data return func(*args, **kwargs)
def wrapper(*args, **kwargs): auth_user = kwargs['auth_user'] del kwargs['auth_user'] try: user = UserService.get_user_by_email(auth_user.get('email')) except UserNotFound: return error(status=404, detail='Not Found') if user.role != 'ADMIN': return error(status=403, detail='Forbidden') return func(*args, **kwargs)
def wrapper(*args, **kwargs): # Verify Firebase auth. if not request.headers.get('Authorization', None): return error(status=401, detail='Unauthorized') if len(request.headers.get('Authorization', None).split(' ')) > 2: return error(status=401, detail='Unauthorized') id_token = request.headers['Authorization'].split(' ').pop() try: decoded_token = auth.verify_id_token(id_token) except Exception as e: return error(status=401, detail='Unauthorized') if not decoded_token: return error(status=401, detail='Unauthorized') kwargs['auth_user'] = decoded_token return func(*args, **kwargs)
def update_user(user_id, new_user): """-""" logging.info('[ROUTER]: Updating user') try: # Update user = UserService.update_user(user_id, new_user) except UserNotFound as e: logging.error('[ROUTER]: ' + e.message) return error(status=404, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize user = UserSchema().dump(user).data response = UserResponder(user).serialize return jsonify(data=response), 200
def create_user(user): """-""" logging.info('[ROUTER]: Creating user') try: # Creating user = UserService.create_user(user) except UserDuplicated as e: logging.error('[ROUTER]: ' + e.message) return error(status=400, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize user = UserSchema().dump(user).data response = UserResponder(user).serialize return jsonify(data=response), 200
def add_stocks_to_user(user_id, n_stocks): """-""" logging.info('[ROUTER]: Adding stocks to user') try: # Adding stocks user = UserService.add_stocks_to_user(user_id, n_stocks) except GenericStockError as e: logging.error('[ROUTER]: ' + e.message) return error(status=400, detail=e.message) except UserNotFound as e: logging.error('[ROUTER]: ' + e.message) return error(status=404, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize user = UserSchema().dump(user).data response = UserResponder(user).serialize return jsonify(data=response), 200
def create_event(event): """-""" logging.info('[ROUTER]: Creating event') try: # Creating event = EventService.create_event(event) except Exception as e: logging.error('[ROUTER]: '+str(e)) return error(status=500, detail='Generic Error') # Serialize event = EventSchema().dump(event).data response = EventResponder(event).serialize return jsonify(data=response), 200
def create_news(news): """-""" logging.info('[ROUTER]: Creating news') try: # Creating news = NewsService.create_news(news) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize news = NewsSchema().dump(news).data response = NewsResponder(news).serialize return jsonify(data=response), 200
def add_stocks(n_stocks): """-""" logging.info('[ROUTER]: Init or modify stock info') try: # Getting stock_info = StockService.add_stocks(n_stocks) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize, schema is not required response = StockInfoResponder({ 'last_stockid': stock_info.last_stockid, 'total_stocks': stock_info.total_stocks }).serialize return jsonify(data=response), 200
def get_me(auth_user): """-""" logging.info('[ROUTER]: Getting me') try: # Getting user = UserService.get_user_by_email(auth_user.get('email')) except UserNotFound as e: logging.error('[ROUTER]: Creating for the first time') user, errors = UserSchema().load({ 'first_name': auth_user.get('name').split(' ')[0], 'last_name': auth_user.get('name').split(' ')[1], 'email': auth_user.get('email') }) user = UserService.create_user(user) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') # Serialize user = UserSchema().dump(user).data response = UserResponder(user).serialize return jsonify(data=response), 200