[Python] Second-order decorators with access in functions arguments

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    So I am a bit confused with this and I have tried a lot of things but nothing worked so far. I have seen the similar questions but still couldn't make it work.

    My function

    import azure.functions as func
    from pydantic import ValidationError
    from utils.auth import with_auth, decor2
    import logging

    project_bp = func.Blueprint()


    @with_auth
    @project_bp.route(route="project", auth_level=func.AuthLevel.ANONYMOUS, methods=["GET"])
    def project(req: func.HttpRequest) -> func.HttpResponse:
    try:
    return func.HttpResponse(f"Hello all possible worlds! ---- --- -- *", status_code=200)

    except ValidationError as e:
    return func.HttpResponse(f"Validation error: {e}", status_code=400)



    def with_auth(func):
    # Function runs until here
    # @wraps(func)
    def wrapper(*args, **kwargs):
    # Access the request object (req) here
    req: az.HttpRequest = kwargs["req"]
    for k in req.headers.values():
    print(k) # Prints nothing...

    # If authenticated, call the original function
    return func(*args, **kwargs)

    return wrapper



    Only if I change the decorator order it works, but I think it's better to have it above the router as Flask endpoints. What am I missing?

    Tried a lots of examples from here, yet, none of them seemed to work properly.

    Continue reading...

Compartilhe esta Página