Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Flask decorator with request?

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???

like image 725
Rahuketu86 Avatar asked Nov 04 '14 13:11

Rahuketu86


2 Answers

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..
like image 183
topless Avatar answered Oct 22 '22 21:10

topless


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.

like image 40
Leo Skhrnkv Avatar answered Oct 22 '22 20:10

Leo Skhrnkv