Murtuza Z
Murtuza Z

Reputation: 6007

Decorator in Python-Flask

#==========================================
# Current API
#==========================================

@blueprint.route('/list/<int:type_id>/', methods=["GET"])
@blueprint.route('/list/<int:type_id>/<int:object_id>', methods=["GET"])
@blueprint.route('/list/<int:type_id>/<int:object_id>/<int:cost_id>', methods=["GET"])
@login_required
def get_list(type_id, object_id=None, cost_id=None):
    # Do something 
    pass

We are using blueprints for API grouping in our Flask project.

Now requirement here is to write a decorator which validates the API parameter passed in URL, like type_id, object_id,cost_id etc

#==========================================
# New Requested feature in API
#==========================================
from functools import wraps

def request_validator():
"""
This function will validates requests and it's parameters if necessary
"""

    def wrap(f):
        @wraps(f)
        def wrapped(self, *args, **kwargs):

            # TODO -- Here I want to validate before actual handler
            # 1) type_id, 
            # 2) object_id, 
            # 3) cost_id
            # And allow handler to process only if validation passes Here

            if type_id not in [ 1,2,3,4,5 ]:
                return internal_server_error(errormsg="Invalid Type ID")

        return f(self, *args, **kwargs)
    return wrapped
return wrap


@blueprint.route('/list/<int:type_id>/', methods=["GET"])
@blueprint.route('/list/<int:type_id>/<int:object_id>', methods=["GET"])
@blueprint.route('/list/<int:type_id>/<int:object_id>/<int:cost_id>', methods=["GET"])
@login_required
@request_validator
def get_list(type_id, object_id=None, cost_id=None):
    # Do something 
    pass

But I am getting beow error and not able to run the application, Am I missing anything?

TypeError: request_validator() takes 0 positional arguments but 1 was given

Upvotes: 0

Views: 144

Answers (1)

Sergey Shubin
Sergey Shubin

Reputation: 3257

Your request_validator decorator should accept function as argument. When you write:

@request_validator
def get_list():
    pass

it means the same as:

def get_list():
    pass
get_list = request_validator(get_list)

So your decorator should look like this (a bit simpler than in your example):

def request_validator(f):
    @wraps(f)
    def wrapped(*args, **kwargs):
        if type_id not in [ 1,2,3,4,5 ]:
            return internal_server_error(errormsg="Invalid Type ID")
        return f(*args, **kwargs)
    return wrapped

Upvotes: 1

Related Questions