init
This commit is contained in:
44
pt_gen/api/endpoints.py
Normal file
44
pt_gen/api/endpoints.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
from pt_gen.core.config import get_settings, Settings
|
||||
from pt_gen.core.orchestrator import InfoOrchestrator
|
||||
|
||||
router = APIRouter()
|
||||
from functools import lru_cache
|
||||
@lru_cache()
|
||||
def get_orchestrator():
|
||||
# 注意这里 get_settings() 会被自动调用和缓存
|
||||
return InfoOrchestrator(settings=get_settings())
|
||||
# API Key 验证依赖,现在它也依赖于 get_settings
|
||||
async def verify_api_key(
|
||||
x_api_key: str = Header(...),
|
||||
settings: Settings = Depends(get_settings)
|
||||
):
|
||||
if x_api_key != settings.api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid API Key",
|
||||
)
|
||||
class GenRequest(BaseModel):
|
||||
url: str
|
||||
@router.post(
|
||||
"/gen",
|
||||
response_model=str,
|
||||
dependencies=[Depends(verify_api_key)]
|
||||
)
|
||||
async def generate_description(
|
||||
request: GenRequest,
|
||||
orchestrator: InfoOrchestrator = Depends(get_orchestrator)
|
||||
):
|
||||
"""
|
||||
根据传入的豆瓣链接,生成格式化的介绍文本。
|
||||
"""
|
||||
result = await orchestrator.generate_info(request.url)
|
||||
if not result or "无效" in result:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=result or "无法生成信息,请检查链接。",
|
||||
)
|
||||
return result
|
||||
Reference in New Issue
Block a user