30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
"""编辑态数据源取数:登录用户传 URL,服务端替浏览器请求,responsePath / transform 仍由前端做"""
|
|
|
|
from typing import Annotated, Any, Literal
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel, Field
|
|
|
|
from app.common.response import ApiResponse, success_response
|
|
from app.core.auth import get_current_user
|
|
from app.schemas.user import CurrentUser
|
|
from app.services.datasource_proxy import fetch_json
|
|
|
|
router = APIRouter(prefix="/datasource", tags=["DataSource"])
|
|
|
|
|
|
class DataSourceFetch(BaseModel):
|
|
url: str = Field(min_length=1, max_length=4096)
|
|
method: Literal["GET", "POST"] = "GET"
|
|
params: dict[str, Any] | None = None
|
|
headers: dict[str, Any] | None = None
|
|
|
|
|
|
@router.post("/fetch")
|
|
async def fetch_datasource(
|
|
body: DataSourceFetch,
|
|
user: Annotated[CurrentUser, Depends(get_current_user)],
|
|
) -> ApiResponse[dict[str, Any] | list[Any]]:
|
|
data = await fetch_json(body.url, body.method, body.params, body.headers)
|
|
return success_response(data)
|