1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Decorator in fastapi which is able to capture path param

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 09:42.

  1. Stack

    Stack Membro Participativo

    In python, lets say I have fastapi API like so:

    @router.put("/{workflowID}", response_model=WorkflowResponse)
    async def update_workflow_endpoint(
    workflowID: int, workflow: WorkflowUpdateRequest, db: AsyncSession = Depends(get_db)
    ):
    ... // Remaining code


    I want to write a decorator which gets the path parameter workflowID. Like so:

    @allowed_perm('WF_ADMIN', {workflowID})

    So it gets two parameters - A string WF_ADMIN and the value of workflowID path param.

    For ex: if API was called like so: /5, then I want the decorator to be internally get the value of 5.

    How can this be done? I'm trying to build my permission module, and although I know this can be done using dependencies, I wanted to know if the same can be done via decorators.

    Continue reading...

Compartilhe esta Página