一尘不染

如何用请求编写Flask装饰器?

flask

我不确定为什么跟随decorator [validate_request]无效。编写这种验证装饰器的正确方法是什么?

def validate_request(req_type):
    if req_type is 'json' and not request.json:
        abort(400)
    def decorator(func):
        @functools.wraps(func)
        def wrapped_func(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapped_func
    return decorator

@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
@validate_request('json')
@json
def update_task(task_id):
#    task = filter(lambda t: t['id'] == task_id, tasks)
    task = [task for task in tasks if task['id'] == task_id]
    if len(task) == 0:
        abort(404)

    #update task
    for field in ['title', 'description', 'done']:
        task[0][field] = request.json.get(field, task[0][field])

错误:-

Traceback (most recent call last):
  File "C:\AGR\Programming\LearningPython\FlaskLearning\flask_rest\app.py", line 156, in <module>
    @validate_request('json')
  File "C:\AGR\Programming\LearningPython\FlaskLearning\flask_rest\app.py", line 144, in validate_request
    if req_type is 'json' and not request.json:
  File "C:\Anaconda\lib\site-packages\werkzeug\local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Anaconda\lib\site-packages\werkzeug\local.py", line 297, in _get_current_object
    return self.__local()
  File "C:\Anaconda\lib\site-packages\flask\globals.py", line 20, in _lookup_req_object
    raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context

应该如何以一种更加惯用的方式完成???


阅读 531

收藏
2020-04-07

共1个答案

一尘不染

这是你的装饰器的外观

def validate_request(f):
  @functools.wraps(f)
  def decorated_function(*args, **kwargs):
    # Do something with your request here
    data = flask.request.get_json()
    if not data:
      flask.abort(404)
    return f(*args, **kwargs)
  return decorated_function

你会这样称呼它

@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
@validate_request
def update_task(task_id):
    # The rest of your code..
2020-04-07