How can I get additional Flask url parameters in a decorator?

I have Flask APIs that use phone as optional url parameters as shown below and I want to use a decorator to check if the phone number is correct. Can I get the "phone" parameter somewhere without parsing the request url?

@user_api.route("/<phone>/login")
@check_phone
def login(phone):
    f = OrderedDict()
    f['error'] = 0
    return jsonify(f)

@user_api.route("/<phone>/logout")
@check_phone
def logout(phone):
    f = OrderedDict()
    f['error'] = 0
    return jsonify(f)

      

+3


source to share


1 answer


There is a better mechanism for checking url values ​​built into Werkzeug (and Flask). Define converter and use it as if you were using any other converter along the route (for example <int:id>

).

from werkzeug.routing import BaseConverter, ValidationError

class PhoneConverter(BaseConverter):
    regex = r'\d{7,10}'  # this validates the basic form of the value

    def to_python(self, value):
        # do more complicated validation
        if not complicated_phone_validation(value):
            raise ValidationError('not a valid phone number')

        return value

app.url_map.converters['phone'] = PhoneConverter

@app.route('/<phone:phone>')
def get_phone(phone):
    # phone is valid

      


You can also use a function before_request

to test all routes with a phone argument without having to decorate them.



from flask import request, abort

@app.before_request
def valid_phone():
    if 'phone' not in request.view_args:
        return  # view has no phone arg

    if not complicated_phone_validation(request.view_args['phone']):
        abort(404)

@app.route('/<phone>')
def get_phone(phone):
    # phone is valid

@app.route('/<other>')
def get_other(other):
    # no phone arg, no validation

      


If you really want to use a decorator, the decorated function is called with arguments.

from functools import wraps

def check_phone(f):
    @wraps(f)
    def inner(**kwargs):
        phone = kwargs['phone']
        # do some validation
        return f(**kwargs)

    return inner

@app.route('/<phone>')
@check_phone
def get_phone(phone):
    # phone is valid

      

+4


source







All Articles