refactor: share pagination validation and sort keys

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-01 22:32:17 +03:00
parent 82b2fbe850
commit fbd5ec32f5
2 changed files with 13 additions and 11 deletions
+8 -6
View File
@@ -29,6 +29,12 @@ def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dic
return store.user(x_user_id)
def parse_limit(value: str) -> int:
if not value.isdecimal() or not 1 <= int(value) <= 100:
raise HTTPException(400)
return int(value)
@router.get("/api/v1/users/me")
def user_me(actor: dict = Depends(current_user)):
return actor
@@ -43,9 +49,7 @@ def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
def list_chats(
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
):
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
raise HTTPException(400)
return store.list_chats(actor["id"], int(limit), cursor)
return store.list_chats(actor["id"], parse_limit(limit), cursor)
@router.get("/api/v1/chats/{chat_id}")
@@ -65,6 +69,4 @@ def list_messages(
cursor: str | None = Query(None, min_length=1),
actor: dict = Depends(current_user),
):
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
raise HTTPException(400)
return store.list_messages(actor["id"], str(chat_id), int(limit), cursor)
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)