[Python] Can i execute a "dependency" after successful endpoint response?

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I looking for a way to handle by who operations was made and another tracking data. I want to handle these insertion through my AuditService, but no as a part of the end of the endpoint, so as part of "Dependency" aftter the endpoint responses successfully, is there a way to achieve this?.

    A way to reach that is this, but im sure is not the best:

    @router.post("/", dependencies=[Depends(require_admin_user)], response_model=ProductResponseSchema)
    async def create_product(
    product_in: ProductCreateSchema,
    current_user: currentUser,
    request: Request,
    db: AsyncSession = Depends(get_db)
    ) -> ProductResponseSchema:
    """Add new product if doesn't exist.\n
    :param product_in: ProductCreateSchema schema input.\n
    :return: ProductResponseSchema response."""
    try:
    if await product_crud.get_by_sku(sku=product_in.sku, db=db):
    raise AlreadyExistException(message="SKU already exists.")

    product = await product_crud.create(db=db, obj_in=product_in.model_dump())
    await AuditService.register(
    current_user=current_user, db=db, request=request,
    action=product_crud.create, data=product_in.model_dump()
    )

    return ProductResponseSchema.model_validate(product)

    except Exception as exc:
    raise exc


    Help adding some extra final functionalities to my endpoint

    Continue reading...

Compartilhe esta Página