File size: 607 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from __future__ import annotations

from fastapi import APIRouter, HTTPException

from app.services.wikipedia_service import WikipediaService

router = APIRouter(prefix="/api/wiki", tags=["wiki"])
_service = WikipediaService()


@router.get("/search")
async def search_wikipedia(q: str):
    return {"items": [item.model_dump() for item in await _service.search(q)]}


@router.get("/page")
async def get_wikipedia_page(title: str):
    try:
        return (await _service.page(title)).model_dump()
    except Exception as exc:
        raise HTTPException(404, f"Wikipedia page not found: {title}") from exc