54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""Lab 1 HTTP routes."""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
|
|
|
from .models import CreateChat, CreateUser
|
|
from .store import store
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/health/live")
|
|
def live():
|
|
return {"status": "ok"}
|
|
|
|
|
|
@router.get("/health/ready")
|
|
def ready():
|
|
return {"status": "ok"}
|
|
|
|
|
|
@router.post("/api/v1/users", status_code=201)
|
|
def register_user(body: CreateUser):
|
|
return store.register(body.username, body.display_name)
|
|
|
|
|
|
def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dict:
|
|
return store.user(x_user_id)
|
|
|
|
|
|
@router.get("/api/v1/users/me")
|
|
def user_me(actor: dict = Depends(current_user)):
|
|
return actor
|
|
|
|
|
|
@router.post("/api/v1/chats", status_code=201)
|
|
def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
|
return store.create_chat(actor["id"], body.title, body.member_ids)
|
|
|
|
|
|
@router.get("/api/v1/chats")
|
|
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)
|
|
|
|
|
|
@router.get("/api/v1/chats/{chat_id}")
|
|
def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
|
|
return store.chat(actor["id"], str(chat_id))
|