ovdashboard/api/ovdashboard_api/routers/v1/text.py

83 lines
1.8 KiB
Python

"""
Router "text" provides:
- listing text files
- finding text files by name prefix
- getting text file raw content by name prefix
- getting text file HTML content by name prefix (using Markdown)
"""
from logging import getLogger
from fastapi import APIRouter, Depends
from markdown import markdown
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
from ...core.webdav import WebDAV
from ._common import LM_TEXT, RP_TEXT
_logger = getLogger(__name__)
_PATH_NAME = "text_dir"
router = APIRouter(prefix="/text", tags=["text"])
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
remote_path = await RP_TEXT()
if not webdav_ensure_path(remote_path):
webdav_ensure_files(
remote_path,
"message.txt",
"title.txt",
"ticker.txt",
)
@router.get(
"/list",
responses=LM_TEXT.lister.responses,
)
async def list_all_texts(
names: list[str] = Depends(LM_TEXT.lister.func),
) -> list[str]:
return names
@router.get(
"/find/{prefix}",
responses=LM_TEXT.filter.responses,
)
async def find_texts_by_prefix(
names: list[str] = Depends(LM_TEXT.filter.func),
) -> list[str]:
return names
async def _get_raw_text_by_prefix(
remote_path: str = Depends(RP_TEXT),
name: str = Depends(LM_TEXT.getter.func),
) -> str:
return await WebDAV.read_str(f"{remote_path}/{name}")
@router.get(
"/get/raw/{prefix}",
responses=LM_TEXT.getter.responses,
)
async def get_raw_text_by_prefix(
text: str = Depends(_get_raw_text_by_prefix),
) -> str:
return text
@router.get(
"/get/html/{prefix}",
responses=LM_TEXT.getter.responses,
)
async def get_html_by_prefix(
text: str = Depends(_get_raw_text_by_prefix),
) -> str:
return markdown(text)