#==========================================
# 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
Wir verwenden Blaupausen für die API-Gruppierung in unserem Flask-Projekt.Dekorateur in Python-Flasche
Jetzt Voraussetzung ist ein Dekorateur zu schreiben, die die API-Parameter in URL übergeben bestätigt, wie 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
Aber ich bin immer Beow Fehler und nicht in der Lage, die Anwendung auszuführen, Am Ich vermisse etwas?
TypeError: request_validator() takes 0 positional arguments but 1 was given