Files
mtusi2026lab6/src/messenger/messaging/entrypoints/http.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

91 lines
2.4 KiB
Python

from typing import Annotated
from uuid import UUID
from dishka.integrations.fastapi import FromDishka, inject
from fastapi import APIRouter, Query, status
from messenger.messaging.application.bus import MessagingCommandBus
from messenger.messaging.application.commands import CreateChat, PostMessage
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
from messenger.messaging.entrypoints.schemas import (
ChatOut,
CreateChatRequest,
CreateMessageRequest,
IdempotencyKey,
MessageOut,
PageOut,
PageQuery,
)
from messenger.shared.entrypoints.actor import Actor
chats_router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
messages_router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["messages"])
@chats_router.post("", status_code=status.HTTP_201_CREATED)
@inject
async def create_chat(
actor: Actor,
body: CreateChatRequest,
bus: FromDishka[MessagingCommandBus],
) -> ChatOut:
chat = await bus.execute(
CreateChat(creator_id=actor, title=body.title, member_ids=body.member_ids)
)
return ChatOut.of(chat)
@chats_router.get("")
@inject
async def list_chats(
actor: Actor,
page: Annotated[PageQuery, Query()],
query: FromDishka[ListChats],
) -> PageOut[ChatOut]:
chats = await query(actor, limit=page.limit, cursor=page.cursor)
return PageOut[ChatOut].of(chats, ChatOut.of)
@chats_router.get("/{chat_id}")
@inject
async def read_chat(
chat_id: UUID,
actor: Actor,
query: FromDishka[GetChat],
) -> ChatOut:
return ChatOut.of(await query(chat_id, actor))
@messages_router.post("", status_code=status.HTTP_201_CREATED)
@inject
async def post_message(
chat_id: UUID,
actor: Actor,
body: CreateMessageRequest,
key: IdempotencyKey,
bus: FromDishka[MessagingCommandBus],
) -> MessageOut:
message = await bus.execute(
PostMessage(
sender_id=actor,
chat_id=chat_id,
text=body.text,
key=key,
fingerprint=body.fingerprint(),
attachment_ids=tuple(body.attachment_ids),
)
)
return MessageOut.of(message)
@messages_router.get("")
@inject
async def read_history(
chat_id: UUID,
actor: Actor,
page: Annotated[PageQuery, Query()],
query: FromDishka[ReadHistory],
) -> PageOut[MessageOut]:
messages = await query(chat_id, actor, limit=page.limit, cursor=page.cursor)
return PageOut[MessageOut].of(messages, MessageOut.of)