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

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

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

83 lines
2.0 KiB
Python

from collections.abc import Callable
from datetime import datetime
from hashlib import sha256
from typing import Annotated, Self
from uuid import UUID
from fastapi import Header
from pydantic import BaseModel, ConfigDict, Field
from messenger.messaging.application.queries import Page
from messenger.messaging.domain.models import Chat, Message
STRICT = ConfigDict(extra="forbid", strict=True)
type IdempotencyKey = Annotated[
str,
Header(alias="Idempotency-Key", min_length=1, max_length=128, pattern=r"^[A-Za-z0-9._:-]+$"),
]
class CreateChatRequest(BaseModel):
model_config = STRICT
title: str
member_ids: list[Annotated[UUID, Field(strict=False)]]
class CreateMessageRequest(BaseModel):
model_config = STRICT
text: str
def fingerprint(self) -> bytes:
return sha256(self.model_dump_json().encode()).digest()
class PageQuery(BaseModel):
limit: int = Field(default=50, ge=1, le=100)
cursor: str | None = None
class ChatOut(BaseModel):
id: UUID
title: str
member_ids: tuple[UUID, ...]
created_at: datetime
@classmethod
def of(cls, chat: Chat) -> ChatOut:
return cls(
id=chat.id,
title=chat.title.value,
member_ids=chat.member_ids,
created_at=chat.created_at,
)
class MessageOut(BaseModel):
id: UUID
chat_id: UUID
sender_id: UUID
text: str
created_at: datetime
@classmethod
def of(cls, message: Message) -> MessageOut:
return cls(
id=message.id,
chat_id=message.chat_id,
sender_id=message.sender_id,
text=message.text.value,
created_at=message.created_at,
)
class PageOut[ItemT](BaseModel):
items: list[ItemT]
next_cursor: str | None
@classmethod
def of[SourceT](cls, page: Page[SourceT], view: Callable[[SourceT], ItemT]) -> Self:
return cls(items=[view(item) for item in page.items], next_cursor=page.next_cursor)