forked from mtusi2026/mtusi2026lab2
Lab2: PostgreSQL, Bearer sessions and Redis for grade 4
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Messenger API lab2: PostgreSQL + Bearer sessions + Redis."""
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
"""HTTP-слой: тонкие обработчики (SRP: только транспорт)."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, Query, Request, Response
|
||||
|
||||
from app.auth import require_actor
|
||||
from app.db import Database
|
||||
from app.errors import unavailable, unsupported_media
|
||||
from app.schemas import CreateChatBody, CreateMessageBody, CreateUserBody
|
||||
from app.services import AuthService, ChatService, MessageService, UserService
|
||||
from app.sessions import SessionStore
|
||||
|
||||
logger = logging.getLogger("messenger")
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _state(request: Request):
|
||||
db: Database = request.app.state.db
|
||||
sessions: SessionStore = request.app.state.sessions
|
||||
return db, sessions
|
||||
|
||||
|
||||
def require_json_content_type(request: Request) -> None:
|
||||
content_type = request.headers.get("content-type", "")
|
||||
media = content_type.split(";")[0].strip().lower()
|
||||
if media != "application/json":
|
||||
raise unsupported_media()
|
||||
|
||||
|
||||
def _require_accepting(db: Database) -> None:
|
||||
if not db.accepting:
|
||||
raise unavailable("Сервер завершает работу")
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
def live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
def ready(request: Request):
|
||||
db, sessions = _state(request)
|
||||
if not db.ready or not db.accepting:
|
||||
raise unavailable("Не готов принимать трафик")
|
||||
if not db.ping() or not sessions.ping():
|
||||
raise unavailable("Зависимость недоступна")
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.post("/api/v1/users", status_code=201)
|
||||
def create_user(
|
||||
request: Request,
|
||||
body: CreateUserBody,
|
||||
_: None = Depends(require_json_content_type),
|
||||
):
|
||||
db, _sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
return UserService(db).create(body.model_dump())
|
||||
|
||||
|
||||
@router.post("/api/v1/auth/sessions", status_code=201)
|
||||
def create_session(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
return AuthService(db, sessions).create_session(authorization)
|
||||
|
||||
|
||||
@router.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
def revoke_session(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
_actor, token = require_actor(db, sessions, authorization)
|
||||
AuthService(db, sessions).logout(token)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
def current_user(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
return actor.to_dict()
|
||||
|
||||
|
||||
@router.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(
|
||||
request: Request,
|
||||
body: CreateChatBody,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
_: None = Depends(require_json_content_type),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
return ChatService(db).create(actor, body.model_dump())
|
||||
|
||||
|
||||
@router.get("/api/v1/chats")
|
||||
def list_chats(
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
limit: Optional[str] = Query(default=None),
|
||||
cursor: Optional[str] = Query(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
return ChatService(db).list_for_user(actor, limit, cursor)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
return ChatService(db).get_for_member(actor, chat_id).to_dict()
|
||||
|
||||
|
||||
@router.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
def create_message(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
body: CreateMessageBody,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
_: None = Depends(require_json_content_type),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
chats = ChatService(db)
|
||||
chat = chats.get_for_member(actor, chat_id)
|
||||
result = MessageService(db).create(actor, chat, body.model_dump())
|
||||
logger.info(
|
||||
"message created chat_id=%s sender_id=%s message_id=%s",
|
||||
chat.id,
|
||||
actor.id,
|
||||
result["id"],
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
authorization: Optional[str] = Header(default=None),
|
||||
limit: Optional[str] = Query(default=None),
|
||||
cursor: Optional[str] = Query(default=None),
|
||||
):
|
||||
db, sessions = _state(request)
|
||||
_require_accepting(db)
|
||||
actor, _token = require_actor(db, sessions, authorization)
|
||||
chat = ChatService(db).get_for_member(actor, chat_id)
|
||||
return MessageService(db).list_for_chat(chat, limit, cursor)
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
"""Разбор Basic/Bearer и проверка сессии."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from app.db import Database
|
||||
from app.errors import unauthorized
|
||||
from app.models import User
|
||||
from app.sessions import SessionStore
|
||||
from app.validation import USERNAME_RE
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
|
||||
|
||||
def parse_basic(authorization: Optional[str]) -> Tuple[str, str]:
|
||||
if not authorization or not authorization.startswith("Basic "):
|
||||
raise unauthorized("Требуется Basic-аутентификация", challenge=BASIC_CHALLENGE)
|
||||
raw = authorization[6:].strip()
|
||||
if not raw:
|
||||
raise unauthorized("Требуется Basic-аутентификация", challenge=BASIC_CHALLENGE)
|
||||
try:
|
||||
decoded = base64.b64decode(raw, validate=True).decode("utf-8")
|
||||
except (binascii.Error, UnicodeDecodeError, ValueError) as exc:
|
||||
raise unauthorized("Неверные учётные данные", challenge=BASIC_CHALLENGE) from exc
|
||||
if ":" not in decoded:
|
||||
raise unauthorized("Неверные учётные данные", challenge=BASIC_CHALLENGE)
|
||||
username, password = decoded.split(":", 1)
|
||||
if not USERNAME_RE.fullmatch(username):
|
||||
raise unauthorized("Неверные учётные данные", challenge=BASIC_CHALLENGE)
|
||||
n = len(password)
|
||||
if n < 12 or n > 128:
|
||||
raise unauthorized("Неверные учётные данные", challenge=BASIC_CHALLENGE)
|
||||
return username, password
|
||||
|
||||
|
||||
def parse_bearer(authorization: Optional[str]) -> str:
|
||||
if not authorization:
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
if authorization.startswith("Basic "):
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
if not authorization.startswith("Bearer "):
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
token = authorization[7:].strip()
|
||||
if not token:
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
return token
|
||||
|
||||
|
||||
def require_actor(
|
||||
db: Database,
|
||||
sessions: SessionStore,
|
||||
authorization: Optional[str],
|
||||
) -> Tuple[User, str]:
|
||||
token = parse_bearer(authorization)
|
||||
user_id = sessions.resolve(token)
|
||||
if user_id is None:
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
user = db.get_user(user_id)
|
||||
if user is None:
|
||||
raise unauthorized(challenge=BEARER_CHALLENGE)
|
||||
return user, token
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Конфигурация из окружения и secret-файлов."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _read_secret(file_env: str, direct_env: str) -> str:
|
||||
path = os.environ.get(file_env, "").strip()
|
||||
if path:
|
||||
return Path(path).read_text(encoding="utf-8").strip()
|
||||
value = os.environ.get(direct_env, "").strip()
|
||||
if value:
|
||||
return value
|
||||
raise RuntimeError(f"Задайте {file_env} или {direct_env}")
|
||||
|
||||
|
||||
class Settings:
|
||||
def __init__(self) -> None:
|
||||
self.host = os.environ.get("APP_HOST", "0.0.0.0")
|
||||
self.port = int(os.environ.get("APP_PORT", "8080"))
|
||||
self.env = os.environ.get("APP_ENV", "lab")
|
||||
self.session_ttl_seconds = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||
if self.session_ttl_seconds < 1:
|
||||
raise RuntimeError("SESSION_TTL_SECONDS должен быть >= 1")
|
||||
self.session_store = os.environ.get("SESSION_STORE", "redis").strip().lower()
|
||||
self.database_url = _read_secret("DATABASE_URL_FILE", "DATABASE_URL")
|
||||
self.cache_url = _read_secret("CACHE_URL_FILE", "CACHE_URL")
|
||||
self.db_connect_timeout = float(os.environ.get("DB_CONNECT_TIMEOUT", "5"))
|
||||
self.db_pool_min = int(os.environ.get("DB_POOL_MIN", "1"))
|
||||
self.db_pool_max = int(os.environ.get("DB_POOL_MAX", "10"))
|
||||
self.redis_socket_timeout = float(os.environ.get("REDIS_SOCKET_TIMEOUT", "3"))
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Непрозрачный keyset-cursor: (created_at, id), привязан к ресурсу."""
|
||||
import base64
|
||||
import json
|
||||
|
||||
from app.errors import invalid_request
|
||||
|
||||
|
||||
def encode(resource_id: str, created_at: str, item_id: str) -> str:
|
||||
payload = {"r": resource_id, "t": created_at, "i": item_id}
|
||||
raw = json.dumps(payload, separators=(",", ":")).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii")
|
||||
|
||||
|
||||
def decode(cursor: str, expected_resource_id: str) -> tuple:
|
||||
try:
|
||||
raw = base64.urlsafe_b64decode(cursor.encode("ascii"))
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
resource_id = payload["r"]
|
||||
created_at = payload["t"]
|
||||
item_id = payload["i"]
|
||||
if not isinstance(resource_id, str) or not isinstance(created_at, str) or not isinstance(item_id, str):
|
||||
raise ValueError("bad types")
|
||||
except Exception as exc:
|
||||
raise invalid_request("Неверный cursor") from exc
|
||||
if resource_id != expected_resource_id:
|
||||
raise invalid_request("Cursor относится к другому ресурсу")
|
||||
return created_at, item_id
|
||||
@@ -0,0 +1,376 @@
|
||||
"""PostgreSQL pool, миграции и параметризованные запросы."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import List, Optional, Sequence
|
||||
|
||||
from psycopg import OperationalError
|
||||
from psycopg.errors import UniqueViolation
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
from app.config import settings
|
||||
from app.errors import unavailable
|
||||
from app.models import Chat, Message, User
|
||||
from app.timefmt import format_utc
|
||||
|
||||
logger = logging.getLogger("messenger")
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations"
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self) -> None:
|
||||
self.pool: Optional[ConnectionPool] = None
|
||||
self.accepting = True
|
||||
self.ready = False
|
||||
|
||||
def open(self) -> None:
|
||||
self.pool = ConnectionPool(
|
||||
conninfo=settings.database_url,
|
||||
min_size=settings.db_pool_min,
|
||||
max_size=settings.db_pool_max,
|
||||
timeout=settings.db_connect_timeout,
|
||||
kwargs={
|
||||
"connect_timeout": max(1, int(settings.db_connect_timeout)),
|
||||
"row_factory": dict_row,
|
||||
},
|
||||
open=True,
|
||||
)
|
||||
self.apply_migrations()
|
||||
self.ready = True
|
||||
logger.info("postgres pool ready; migrations applied")
|
||||
|
||||
def close(self) -> None:
|
||||
self.ready = False
|
||||
self.accepting = False
|
||||
if self.pool is not None:
|
||||
self.pool.close()
|
||||
self.pool = None
|
||||
|
||||
def begin_shutdown(self) -> None:
|
||||
self.accepting = False
|
||||
self.ready = False
|
||||
|
||||
def _require_pool(self) -> ConnectionPool:
|
||||
if self.pool is None:
|
||||
raise unavailable("База данных недоступна")
|
||||
return self.pool
|
||||
|
||||
def ping(self) -> bool:
|
||||
try:
|
||||
pool = self._require_pool()
|
||||
with pool.connection() as conn:
|
||||
conn.execute("SELECT 1")
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _split_sql(script: str) -> List[str]:
|
||||
statements: List[str] = []
|
||||
buf: List[str] = []
|
||||
for line in script.splitlines():
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith("--"):
|
||||
continue
|
||||
buf.append(line)
|
||||
if stripped.endswith(";"):
|
||||
stmt = "\n".join(buf).strip()
|
||||
if stmt:
|
||||
statements.append(stmt)
|
||||
buf = []
|
||||
tail = "\n".join(buf).strip()
|
||||
if tail:
|
||||
statements.append(tail)
|
||||
return statements
|
||||
|
||||
def apply_migrations(self) -> None:
|
||||
pool = self._require_pool()
|
||||
files = sorted(MIGRATIONS_DIR.glob("*.sql"))
|
||||
with pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version TEXT PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
)
|
||||
for path in files:
|
||||
version = path.name
|
||||
row = conn.execute(
|
||||
"SELECT 1 FROM schema_migrations WHERE version = %s",
|
||||
(version,),
|
||||
).fetchone()
|
||||
if row:
|
||||
continue
|
||||
statements = self._split_sql(path.read_text(encoding="utf-8"))
|
||||
with conn.transaction():
|
||||
for stmt in statements:
|
||||
conn.execute(stmt)
|
||||
conn.execute(
|
||||
"INSERT INTO schema_migrations (version) VALUES (%s)",
|
||||
(version,),
|
||||
)
|
||||
logger.info("applied migration %s", version)
|
||||
|
||||
def _user_from_row(self, row: dict) -> User:
|
||||
return User(
|
||||
id=str(row["id"]),
|
||||
username=row["username"],
|
||||
display_name=row["display_name"],
|
||||
created_at=format_utc(row["created_at"]),
|
||||
password_hash=row.get("password_hash", ""),
|
||||
)
|
||||
|
||||
def add_user(self, user: User) -> None:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES (%s, %s, %s, %s, %s::timestamptz)
|
||||
""",
|
||||
(
|
||||
user.id,
|
||||
user.username,
|
||||
user.display_name,
|
||||
user.password_hash,
|
||||
user.created_at,
|
||||
),
|
||||
)
|
||||
except UniqueViolation as exc:
|
||||
raise KeyError("username") from exc
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
|
||||
def get_user(self, user_id: str) -> Optional[User]:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users WHERE id = %s
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return self._user_from_row(row) if row else None
|
||||
|
||||
def get_user_by_username(self, username: str) -> Optional[User]:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users WHERE username = %s
|
||||
""",
|
||||
(username,),
|
||||
).fetchone()
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return self._user_from_row(row) if row else None
|
||||
|
||||
def users_exist(self, user_ids: Sequence[str]) -> bool:
|
||||
if not user_ids:
|
||||
return True
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) AS c FROM users WHERE id = ANY(%s::uuid[])",
|
||||
(list(user_ids),),
|
||||
).fetchone()
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return int(row["c"]) == len(set(user_ids))
|
||||
|
||||
def add_chat(self, chat: Chat) -> None:
|
||||
"""Атомарно: чат + участники в одной транзакции."""
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
# Повторная проверка участников внутри транзакции.
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) AS c FROM users WHERE id = ANY(%s::uuid[])",
|
||||
(list(chat.member_ids),),
|
||||
).fetchone()
|
||||
if int(row["c"]) != len(set(chat.member_ids)):
|
||||
raise ValueError("unknown member")
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
VALUES (%s, %s, %s::timestamptz)
|
||||
""",
|
||||
(chat.id, chat.title, chat.created_at),
|
||||
)
|
||||
for mid in chat.member_ids:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO chat_members (chat_id, user_id)
|
||||
VALUES (%s, %s)
|
||||
""",
|
||||
(chat.id, mid),
|
||||
)
|
||||
except ValueError:
|
||||
raise
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
|
||||
def get_chat(self, chat_id: str) -> Optional[Chat]:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT id, title, created_at FROM chats WHERE id = %s",
|
||||
(chat_id,),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
members = conn.execute(
|
||||
"""
|
||||
SELECT user_id FROM chat_members
|
||||
WHERE chat_id = %s
|
||||
ORDER BY user_id
|
||||
""",
|
||||
(chat_id,),
|
||||
).fetchall()
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return Chat(
|
||||
id=str(row["id"]),
|
||||
title=row["title"],
|
||||
member_ids=[str(m["user_id"]) for m in members],
|
||||
created_at=format_utc(row["created_at"]),
|
||||
)
|
||||
|
||||
def list_chats_for(self, user_id: str) -> List[Chat]:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = %s
|
||||
ORDER BY c.created_at ASC, c.id ASC
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchall()
|
||||
result: List[Chat] = []
|
||||
for row in rows:
|
||||
members = conn.execute(
|
||||
"""
|
||||
SELECT user_id FROM chat_members
|
||||
WHERE chat_id = %s
|
||||
ORDER BY user_id
|
||||
""",
|
||||
(row["id"],),
|
||||
).fetchall()
|
||||
result.append(
|
||||
Chat(
|
||||
id=str(row["id"]),
|
||||
title=row["title"],
|
||||
member_ids=[str(m["user_id"]) for m in members],
|
||||
created_at=format_utc(row["created_at"]),
|
||||
)
|
||||
)
|
||||
return result
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
|
||||
def add_message(self, message: Message) -> Message:
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES (%s, %s, %s, %s, %s::timestamptz)
|
||||
""",
|
||||
(
|
||||
message.id,
|
||||
message.chat_id,
|
||||
message.sender_id,
|
||||
message.text,
|
||||
message.created_at,
|
||||
),
|
||||
)
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return message
|
||||
|
||||
def list_messages_page(
|
||||
self,
|
||||
chat_id: str,
|
||||
limit: int,
|
||||
after_created_at: Optional[str] = None,
|
||||
after_id: Optional[str] = None,
|
||||
) -> List[Message]:
|
||||
"""Keyset page using index (chat_id, created_at, id)."""
|
||||
pool = self._require_pool()
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
if after_created_at is None:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = %s
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(chat_id, limit),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = %s
|
||||
AND (created_at, id) > (%s::timestamptz, %s::uuid)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(chat_id, after_created_at, after_id, limit),
|
||||
).fetchall()
|
||||
except OperationalError as exc:
|
||||
raise unavailable("База данных недоступна") from exc
|
||||
return [
|
||||
Message(
|
||||
id=str(r["id"]),
|
||||
chat_id=str(r["chat_id"]),
|
||||
sender_id=str(r["sender_id"]),
|
||||
text=r["text"],
|
||||
created_at=format_utc(r["created_at"]),
|
||||
)
|
||||
for r in rows
|
||||
]
|
||||
|
||||
def explain_messages_page(self, chat_id: str, limit: int = 50) -> str:
|
||||
pool = self._require_pool()
|
||||
with pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = %s
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(chat_id, limit),
|
||||
).fetchall()
|
||||
# EXPLAIN text rows come as single-column dicts
|
||||
lines = []
|
||||
for r in rows:
|
||||
lines.append(next(iter(r.values())))
|
||||
return "\n".join(lines)
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Единый формат ошибок API."""
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
headers: Optional[dict] = None,
|
||||
) -> None:
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
def invalid_request(message: str = "Неверный запрос") -> AppError:
|
||||
return AppError(400, "invalid_request", message)
|
||||
|
||||
|
||||
def unauthorized(
|
||||
message: str = "Требуется идентификация",
|
||||
*,
|
||||
challenge: str = 'Bearer realm="messenger"',
|
||||
) -> AppError:
|
||||
return AppError(
|
||||
401,
|
||||
"unauthorized",
|
||||
message,
|
||||
headers={"WWW-Authenticate": challenge},
|
||||
)
|
||||
|
||||
|
||||
def not_found(message: str = "Объект не найден") -> AppError:
|
||||
return AppError(404, "not_found", message)
|
||||
|
||||
|
||||
def conflict(message: str = "Конфликт") -> AppError:
|
||||
return AppError(409, "conflict", message)
|
||||
|
||||
|
||||
def unsupported_media(message: str = "Неподдерживаемый Content-Type") -> AppError:
|
||||
return AppError(415, "unsupported_media_type", message)
|
||||
|
||||
|
||||
def unavailable(message: str = "Сервис недоступен") -> AppError:
|
||||
return AppError(503, "unavailable", message)
|
||||
|
||||
|
||||
def error_body(code: str, message: str, request_id: str) -> dict:
|
||||
return {"error": {"code": code, "message": message, "request_id": request_id}}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
"""Точка входа HTTP-приложения lab2."""
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from app.api import router
|
||||
from app.db import Database
|
||||
from app.errors import AppError, error_body
|
||||
from app.sessions import SessionStore
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("messenger")
|
||||
|
||||
db = Database()
|
||||
sessions = SessionStore()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
logger.info("server starting; connecting to postgres/redis")
|
||||
db.open()
|
||||
sessions.open()
|
||||
_app.state.db = db
|
||||
_app.state.sessions = sessions
|
||||
logger.info("dependencies ready")
|
||||
yield
|
||||
db.begin_shutdown()
|
||||
sessions.close()
|
||||
db.close()
|
||||
logger.info("SIGTERM/shutdown: stop accepting, ready=false")
|
||||
|
||||
|
||||
app = FastAPI(title="MTUSI Messenger lab2", lifespan=lifespan)
|
||||
app.state.db = db
|
||||
app.state.sessions = sessions
|
||||
app.include_router(router)
|
||||
|
||||
|
||||
class RequestIdMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
request_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
request.state.request_id = request_id
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
logger.info(
|
||||
"%s %s -> %s request_id=%s",
|
||||
request.method,
|
||||
request.url.path,
|
||||
response.status_code,
|
||||
request_id,
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
app.add_middleware(RequestIdMiddleware)
|
||||
|
||||
|
||||
@app.exception_handler(AppError)
|
||||
async def app_error_handler(request: Request, exc: AppError):
|
||||
request_id = getattr(request.state, "request_id", None) or str(uuid.uuid4())
|
||||
headers = {"X-Request-Id": request_id}
|
||||
headers.update(exc.headers)
|
||||
return JSONResponse(
|
||||
status_code=exc.status,
|
||||
content=error_body(exc.code, exc.message, request_id),
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_handler(request: Request, _exc: RequestValidationError):
|
||||
request_id = getattr(request.state, "request_id", None) or str(uuid.uuid4())
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=error_body("invalid_request", "Неверный запрос", request_id),
|
||||
headers={"X-Request-Id": request_id},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def internal_handler(request: Request, exc: Exception):
|
||||
request_id = getattr(request.state, "request_id", None) or str(uuid.uuid4())
|
||||
logger.exception("unhandled error request_id=%s", request_id)
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content=error_body("internal_error", "Внутренняя ошибка", request_id),
|
||||
headers={"X-Request-Id": request_id},
|
||||
)
|
||||
|
||||
|
||||
def run() -> None:
|
||||
import uvicorn
|
||||
from app.config import settings
|
||||
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
log_level="info",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Доменные сущности (данные, без HTTP и без бизнес-правил)."""
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
|
||||
@dataclass
|
||||
class User:
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
password_hash: str = ""
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"username": self.username,
|
||||
"display_name": self.display_name,
|
||||
"created_at": self.created_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Chat:
|
||||
id: str
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
created_at: str
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"title": self.title,
|
||||
"member_ids": list(self.member_ids),
|
||||
"created_at": self.created_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Message:
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"chat_id": self.chat_id,
|
||||
"sender_id": self.sender_id,
|
||||
"text": self.text,
|
||||
"created_at": self.created_at,
|
||||
}
|
||||
|
||||
def sort_key(self):
|
||||
return (self.created_at, uuid.UUID(self.id).int)
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Password KDF: Argon2id (библиотечный)."""
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerifyMismatchError
|
||||
|
||||
_hasher = PasswordHasher()
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return _hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password_hash: str, password: str) -> bool:
|
||||
try:
|
||||
return _hasher.verify(password_hash, password)
|
||||
except VerifyMismatchError:
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Схемы тел запросов для OpenAPI / Swagger (валидация домена — в services)."""
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
class CreateUserBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
username: str = Field(..., examples=["alice"])
|
||||
display_name: str = Field(..., examples=["Алиса"])
|
||||
password: str = Field(..., examples=["Example-only-not-a-real-secret-123"])
|
||||
|
||||
|
||||
class CreateChatBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
title: str = Field(..., examples=["Проект по ВНП"])
|
||||
member_ids: List[str] = Field(
|
||||
...,
|
||||
examples=[["11111111-1111-4111-8111-111111111111"]],
|
||||
)
|
||||
|
||||
|
||||
class CreateMessageBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
text: str = Field(..., examples=["Привет, Боб!"])
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
"""Бизнес-логика lab2 (DIP: зависит от Database/SessionStore)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
|
||||
from app import cursor as cursor_util
|
||||
from app.auth import BASIC_CHALLENGE, parse_basic
|
||||
from app.db import Database
|
||||
from app.errors import conflict, invalid_request, not_found, unauthorized
|
||||
from app.models import Chat, Message, User
|
||||
from app.password import hash_password, verify_password
|
||||
from app.sessions import SessionStore
|
||||
from app.timefmt import now_utc
|
||||
from app.validation import (
|
||||
parse_limit,
|
||||
parse_uuid,
|
||||
reject_unknown,
|
||||
require_display_name,
|
||||
require_keys,
|
||||
require_message_text,
|
||||
require_object,
|
||||
require_password,
|
||||
require_title,
|
||||
require_username,
|
||||
)
|
||||
|
||||
|
||||
class UserService:
|
||||
def __init__(self, db: Database) -> None:
|
||||
self.db = db
|
||||
|
||||
def create(self, body: object) -> dict:
|
||||
data = require_object(body)
|
||||
reject_unknown(data, {"username", "display_name", "password"})
|
||||
require_keys(data, ("username", "display_name", "password"))
|
||||
username = require_username(data["username"])
|
||||
display_name = require_display_name(data["display_name"])
|
||||
password = require_password(data["password"])
|
||||
user = User(
|
||||
id=str(uuid.uuid4()),
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
created_at=now_utc(),
|
||||
password_hash=hash_password(password),
|
||||
)
|
||||
try:
|
||||
self.db.add_user(user)
|
||||
except KeyError as exc:
|
||||
raise conflict("username уже занят") from exc
|
||||
return user.to_dict()
|
||||
|
||||
|
||||
class AuthService:
|
||||
def __init__(self, db: Database, sessions: SessionStore) -> None:
|
||||
self.db = db
|
||||
self.sessions = sessions
|
||||
|
||||
def create_session(self, authorization: Optional[str]) -> dict:
|
||||
username, password = parse_basic(authorization)
|
||||
user = self.db.get_user_by_username(username)
|
||||
if user is None or not verify_password(user.password_hash, password):
|
||||
raise unauthorized("Неверные учётные данные", challenge=BASIC_CHALLENGE)
|
||||
token, expires_at = self.sessions.create(user.id)
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires_at,
|
||||
"user": user.to_dict(),
|
||||
}
|
||||
|
||||
def logout(self, token: str) -> None:
|
||||
self.sessions.revoke(token)
|
||||
|
||||
|
||||
class ChatService:
|
||||
def __init__(self, db: Database) -> None:
|
||||
self.db = db
|
||||
|
||||
def create(self, actor: User, body: object) -> dict:
|
||||
data = require_object(body)
|
||||
reject_unknown(data, {"title", "member_ids"})
|
||||
require_keys(data, ("title", "member_ids"))
|
||||
title = require_title(data["title"])
|
||||
raw_members = data["member_ids"]
|
||||
if not isinstance(raw_members, list):
|
||||
raise invalid_request("member_ids должен быть массивом")
|
||||
if len(raw_members) < 1 or len(raw_members) > 100:
|
||||
raise invalid_request("member_ids: от 1 до 100 участников")
|
||||
member_ids: List[str] = []
|
||||
seen = set()
|
||||
for item in raw_members:
|
||||
mid = parse_uuid(item, "member_ids")
|
||||
if mid in seen:
|
||||
raise invalid_request("member_ids содержит дубликаты")
|
||||
seen.add(mid)
|
||||
member_ids.append(mid)
|
||||
if actor.id not in seen:
|
||||
raise invalid_request("Вызывающий должен быть в member_ids")
|
||||
if not self.db.users_exist(member_ids):
|
||||
raise invalid_request("Неизвестный участник")
|
||||
chat = Chat(
|
||||
id=str(uuid.uuid4()),
|
||||
title=title,
|
||||
member_ids=member_ids,
|
||||
created_at=now_utc(),
|
||||
)
|
||||
try:
|
||||
self.db.add_chat(chat)
|
||||
except ValueError as exc:
|
||||
raise invalid_request("Неизвестный участник") from exc
|
||||
return chat.to_dict()
|
||||
|
||||
def get_for_member(self, actor: User, chat_id: str) -> Chat:
|
||||
cid = parse_uuid(chat_id, "chat_id")
|
||||
chat = self.db.get_chat(cid)
|
||||
if chat is None or actor.id not in chat.member_ids:
|
||||
raise not_found()
|
||||
return chat
|
||||
|
||||
def list_for_user(self, actor: User, limit_raw: Optional[str], cursor_raw: Optional[str]) -> dict:
|
||||
limit = parse_limit(limit_raw)
|
||||
chats = self.db.list_chats_for(actor.id)
|
||||
start = 0
|
||||
if cursor_raw:
|
||||
created_at, item_id = cursor_util.decode(cursor_raw, actor.id)
|
||||
start = 0
|
||||
for i, chat in enumerate(chats):
|
||||
if (chat.created_at, chat.id) > (created_at, item_id):
|
||||
start = i
|
||||
break
|
||||
else:
|
||||
start = len(chats)
|
||||
page = chats[start : start + limit]
|
||||
next_cursor = None
|
||||
if start + limit < len(chats) and page:
|
||||
last = page[-1]
|
||||
next_cursor = cursor_util.encode(actor.id, last.created_at, last.id)
|
||||
return {"items": [c.to_dict() for c in page], "next_cursor": next_cursor}
|
||||
|
||||
|
||||
class MessageService:
|
||||
def __init__(self, db: Database) -> None:
|
||||
self.db = db
|
||||
|
||||
def create(self, actor: User, chat: Chat, body: object) -> dict:
|
||||
data = require_object(body)
|
||||
reject_unknown(data, {"text"})
|
||||
require_keys(data, ("text",))
|
||||
text = require_message_text(data["text"])
|
||||
message = Message(
|
||||
id=str(uuid.uuid4()),
|
||||
chat_id=chat.id,
|
||||
sender_id=actor.id,
|
||||
text=text,
|
||||
created_at=now_utc(),
|
||||
)
|
||||
self.db.add_message(message)
|
||||
return message.to_dict()
|
||||
|
||||
def list_for_chat(
|
||||
self,
|
||||
chat: Chat,
|
||||
limit_raw: Optional[str],
|
||||
cursor_raw: Optional[str],
|
||||
) -> dict:
|
||||
limit = parse_limit(limit_raw)
|
||||
after_created_at = None
|
||||
after_id = None
|
||||
if cursor_raw:
|
||||
created_at, item_id = cursor_util.decode(cursor_raw, chat.id)
|
||||
parse_uuid(item_id, "cursor.id")
|
||||
after_created_at = created_at
|
||||
after_id = item_id
|
||||
# Fetch limit+1 to detect next page without scanning all history.
|
||||
rows = self.db.list_messages_page(
|
||||
chat.id,
|
||||
limit + 1,
|
||||
after_created_at=after_created_at,
|
||||
after_id=after_id,
|
||||
)
|
||||
page = rows[:limit]
|
||||
next_cursor = None
|
||||
if len(rows) > limit and page:
|
||||
last = page[-1]
|
||||
next_cursor = cursor_util.encode(chat.id, last.created_at, last.id)
|
||||
return {"items": [m.to_dict() for m in page], "next_cursor": next_cursor}
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Opaque Bearer-сессии в Redis с TTL и AOF-persistence на стороне Redis."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import redis
|
||||
|
||||
from app.config import settings
|
||||
from app.errors import unavailable
|
||||
from app.timefmt import format_utc, parse_rfc3339
|
||||
|
||||
logger = logging.getLogger("messenger")
|
||||
|
||||
TOKEN_PREFIX = "session:"
|
||||
|
||||
|
||||
class SessionStore:
|
||||
def __init__(self) -> None:
|
||||
self.client: Optional[redis.Redis] = None
|
||||
|
||||
def open(self) -> None:
|
||||
self.client = redis.Redis.from_url(
|
||||
settings.cache_url,
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=settings.redis_socket_timeout,
|
||||
socket_timeout=settings.redis_socket_timeout,
|
||||
)
|
||||
self.client.ping()
|
||||
logger.info("redis session store ready")
|
||||
|
||||
def close(self) -> None:
|
||||
if self.client is not None:
|
||||
self.client.close()
|
||||
self.client = None
|
||||
|
||||
def _require(self) -> redis.Redis:
|
||||
if self.client is None:
|
||||
raise unavailable("Хранилище сессий недоступно")
|
||||
return self.client
|
||||
|
||||
def ping(self) -> bool:
|
||||
try:
|
||||
return bool(self._require().ping())
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def create(self, user_id: str) -> Tuple[str, str]:
|
||||
"""Возвращает (token, expires_at)."""
|
||||
client = self._require()
|
||||
token = secrets.token_urlsafe(32) # ≥256 бит
|
||||
expires = datetime.now(timezone.utc) + timedelta(seconds=settings.session_ttl_seconds)
|
||||
expires_at = format_utc(expires)
|
||||
payload = json.dumps({"user_id": user_id, "expires_at": expires_at}, separators=(",", ":"))
|
||||
try:
|
||||
client.setex(TOKEN_PREFIX + token, settings.session_ttl_seconds, payload)
|
||||
except redis.RedisError as exc:
|
||||
raise unavailable("Хранилище сессий недоступно") from exc
|
||||
return token, expires_at
|
||||
|
||||
def resolve(self, token: str) -> Optional[str]:
|
||||
"""user_id или None, если нет/истекла. 503 при недоступности Redis."""
|
||||
client = self._require()
|
||||
try:
|
||||
raw = client.get(TOKEN_PREFIX + token)
|
||||
except redis.RedisError as exc:
|
||||
raise unavailable("Хранилище сессий недоступно") from exc
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
user_id = data["user_id"]
|
||||
expires_at = parse_rfc3339(data["expires_at"])
|
||||
except Exception:
|
||||
return None
|
||||
if expires_at <= datetime.now(timezone.utc):
|
||||
try:
|
||||
client.delete(TOKEN_PREFIX + token)
|
||||
except redis.RedisError:
|
||||
pass
|
||||
return None
|
||||
return str(user_id)
|
||||
|
||||
def revoke(self, token: str) -> bool:
|
||||
client = self._require()
|
||||
try:
|
||||
deleted = client.delete(TOKEN_PREFIX + token)
|
||||
except redis.RedisError as exc:
|
||||
raise unavailable("Хранилище сессий недоступно") from exc
|
||||
return bool(deleted)
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Форматирование времени в RFC 3339 UTC с миллисекундами и Z."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def now_utc() -> str:
|
||||
return format_utc(datetime.now(timezone.utc))
|
||||
|
||||
|
||||
def format_utc(value: datetime) -> str:
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
else:
|
||||
value = value.astimezone(timezone.utc)
|
||||
return value.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
||||
|
||||
|
||||
def parse_rfc3339(value: str) -> datetime:
|
||||
if value.endswith("Z"):
|
||||
value = value[:-1] + "+00:00"
|
||||
return datetime.fromisoformat(value)
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Проверки входных данных по контракту lab2."""
|
||||
import re
|
||||
import uuid
|
||||
from typing import Any, Iterable, Optional, Set
|
||||
|
||||
from app.errors import invalid_request
|
||||
|
||||
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||
|
||||
|
||||
def require_object(body: Any) -> dict:
|
||||
if not isinstance(body, dict):
|
||||
raise invalid_request("Тело запроса должно быть JSON-объектом")
|
||||
return body
|
||||
|
||||
|
||||
def reject_unknown(body: dict, allowed: Set[str]) -> None:
|
||||
extra = set(body) - allowed
|
||||
if extra:
|
||||
raise invalid_request("Лишние поля в запросе")
|
||||
|
||||
|
||||
def require_keys(body: dict, keys: Iterable[str]) -> None:
|
||||
for key in keys:
|
||||
if key not in body:
|
||||
raise invalid_request(f"Поле {key} обязательно")
|
||||
|
||||
|
||||
def parse_uuid(value: Any, field: str = "id") -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request(f"Поле {field} должно быть UUID-строкой")
|
||||
try:
|
||||
return str(uuid.UUID(value))
|
||||
except ValueError as exc:
|
||||
raise invalid_request(f"Неверный UUID: {field}") from exc
|
||||
|
||||
|
||||
def code_points(text: str) -> int:
|
||||
return len(text)
|
||||
|
||||
|
||||
def require_username(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request("username должен быть строкой")
|
||||
if not USERNAME_RE.fullmatch(value):
|
||||
raise invalid_request("username: только [a-z0-9_] длиной 3–32")
|
||||
return value
|
||||
|
||||
|
||||
def require_display_name(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request("display_name должен быть строкой")
|
||||
if value.strip() == "":
|
||||
raise invalid_request("display_name не может быть пустым")
|
||||
n = code_points(value)
|
||||
if n < 1 or n > 100:
|
||||
raise invalid_request("display_name: 1–100 символов Unicode")
|
||||
return value
|
||||
|
||||
|
||||
def require_password(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request("password должен быть строкой")
|
||||
if value.strip() == "":
|
||||
raise invalid_request("password не может быть пустым")
|
||||
n = code_points(value)
|
||||
if n < 12 or n > 128:
|
||||
raise invalid_request("password: 12–128 символов Unicode")
|
||||
return value
|
||||
|
||||
|
||||
def require_title(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request("title должен быть строкой")
|
||||
if value.strip() == "":
|
||||
raise invalid_request("title не может быть пустым")
|
||||
n = code_points(value)
|
||||
if n < 1 or n > 100:
|
||||
raise invalid_request("title: 1–100 символов Unicode")
|
||||
return value
|
||||
|
||||
|
||||
def require_message_text(value: Any) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise invalid_request("text должен быть строкой")
|
||||
if value.strip() == "":
|
||||
raise invalid_request("text не может быть пустым")
|
||||
if code_points(value) > 4000:
|
||||
raise invalid_request("text: максимум 4000 символов Unicode")
|
||||
return value
|
||||
|
||||
|
||||
def parse_limit(raw: Optional[str], default: int = 50) -> int:
|
||||
if raw is None or raw == "":
|
||||
return default
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError as exc:
|
||||
raise invalid_request("limit должен быть целым числом") from exc
|
||||
if value < 1 or value > 100:
|
||||
raise invalid_request("limit должен быть от 1 до 100")
|
||||
return value
|
||||
Reference in New Issue
Block a user