FastAPI access Context #1039
-
|
Is there currently a way to access the RabbitMessage in the handler when using the FastAPI plugin? If I do it like in the code snippet below, I get some Pydantic validation errors. I need it to manually acknowledge or reject messages in certain cases. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
@sidjakinsboriss we already have the Issue to make FastStream But, for now you can get access to it manually from the from faststream import context
from faststream.rabbit import RabbitMessage
@router.subscriber("test")
async def handler(msg_body):
msg: RabbitMessage = context.get_local("message")
await msg.ack()Also, if you like the original way to get access to raw message, you can use the following snippet - it works the same way from typing import Annotated
from fastapi import Depends
from faststream.rabbit.message import RabbitMessage as RawRabbitMessage
def get_msg():
return context.get_local("message")
RabbitMessage = Annotated[RawRabbitMessage, Depends(get_msg)]
@router.subscriber("test")
async def handler(msg_body, msg: RabbitMessage):
await msg.ack()Btw, all FastStream |
Beta Was this translation helpful? Give feedback.
@sidjakinsboriss we already have the Issue to make FastStream
ContextFastAPI-compatibleBut, for now you can get access to it manually from the
contextobject:Also, if you like the original way to get access to raw message, you can use the following snippet - it works the same way