How to write Flask decorator with request?

2020-02-28 06:40发布

I am not sure why following decorator[validate_request] doesn't work. What is correct way to write such validation decorator?

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])

Error :-

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

How should this be done in a more idiomatic way???

2条回答
三岁会撩人
2楼-- · 2020-02-28 07:01

It's kind of an old post, but I think that it may benefit from a little correction: decorated_function needs to return f(*args, **kws)

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

Otherwise you will encounter TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

查看更多
家丑人穷心不美
3楼-- · 2020-02-28 07:03

This is how your decorator should look like

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

and you will call it like this

@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
@validate_request
def update_task(task_id):
    # The rest of your code..
查看更多
登录 后发表回答