Files
mtusi2026lab2/src/messenger/messaging/entrypoints/http.py
T
ArtyomandClaude Opus 5.5 540fcb35ba Carry over the lab 4 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and
scenario scripts and the CI job from the lab 4 solution commit d2744bc,
merge the lab 4 Makefile targets into the lab 5 skeleton Makefile
without touching its targets, and append the lab 4 solution variables
to the skeleton .env.example. The package and the application follow
the contract version 1.5.0, and the image and the throwaway test
containers are tagged lab5 so they do not collide with a lab 4 stand on
the same host.

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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 00:34:03 +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)