DB-GPT/dbgpt/serve/file/api/endpoints.py
Fangyin Cheng 9502251c08
feat(core): AWEL flow 2.0 backend code (#1879)
Co-authored-by: yhjun1026 <460342015@qq.com>
2024-08-23 14:57:54 +08:00

170 lines
4.9 KiB
Python

import logging
from functools import cache
from typing import List, Optional
from urllib.parse import quote
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer
from starlette.responses import StreamingResponse
from dbgpt.component import SystemApp
from dbgpt.serve.core import Result, blocking_func_to_async
from dbgpt.util import PaginationResult
from ..config import APP_NAME, SERVE_APP_NAME, SERVE_SERVICE_COMPONENT_NAME, ServeConfig
from ..service.service import Service
from .schemas import ServeRequest, ServerResponse, UploadFileResponse
router = APIRouter()
logger = logging.getLogger(__name__)
# Add your API endpoints here
global_system_app: Optional[SystemApp] = None
def get_service() -> Service:
"""Get the service instance"""
return global_system_app.get_component(SERVE_SERVICE_COMPONENT_NAME, Service)
get_bearer_token = HTTPBearer(auto_error=False)
@cache
def _parse_api_keys(api_keys: str) -> List[str]:
"""Parse the string api keys to a list
Args:
api_keys (str): The string api keys
Returns:
List[str]: The list of api keys
"""
if not api_keys:
return []
return [key.strip() for key in api_keys.split(",")]
async def check_api_key(
auth: Optional[HTTPAuthorizationCredentials] = Depends(get_bearer_token),
service: Service = Depends(get_service),
) -> Optional[str]:
"""Check the api key
If the api key is not set, allow all.
Your can pass the token in you request header like this:
.. code-block:: python
import requests
client_api_key = "your_api_key"
headers = {"Authorization": "Bearer " + client_api_key}
res = requests.get("http://test/hello", headers=headers)
assert res.status_code == 200
"""
if service.config.api_keys:
api_keys = _parse_api_keys(service.config.api_keys)
if auth is None or (token := auth.credentials) not in api_keys:
raise HTTPException(
status_code=401,
detail={
"error": {
"message": "",
"type": "invalid_request_error",
"param": None,
"code": "invalid_api_key",
}
},
)
return token
else:
# api_keys not set; allow all
return None
@router.get("/health")
async def health():
"""Health check endpoint"""
return {"status": "ok"}
@router.get("/test_auth", dependencies=[Depends(check_api_key)])
async def test_auth():
"""Test auth endpoint"""
return {"status": "ok"}
@router.post(
"/files/{bucket}",
response_model=Result[List[UploadFileResponse]],
dependencies=[Depends(check_api_key)],
)
async def upload_files(
bucket: str,
files: List[UploadFile],
user_name: Optional[str] = Query(default=None, description="user name"),
sys_code: Optional[str] = Query(default=None, description="system code"),
service: Service = Depends(get_service),
) -> Result[List[UploadFileResponse]]:
"""Upload files by a list of UploadFile."""
logger.info(f"upload_files: bucket={bucket}, files={files}")
results = await blocking_func_to_async(
global_system_app,
service.upload_files,
bucket,
"distributed",
files,
user_name,
sys_code,
)
return Result.succ(results)
@router.get("/files/{bucket}/{file_id}", dependencies=[Depends(check_api_key)])
async def download_file(
bucket: str, file_id: str, service: Service = Depends(get_service)
):
"""Download a file by file_id."""
logger.info(f"download_file: bucket={bucket}, file_id={file_id}")
file_data, file_metadata = await blocking_func_to_async(
global_system_app, service.download_file, bucket, file_id
)
file_name_encoded = quote(file_metadata.file_name)
def file_iterator(raw_iter):
with raw_iter:
while chunk := raw_iter.read(
service.config.file_server_download_chunk_size
):
yield chunk
response = StreamingResponse(
file_iterator(file_data), media_type="application/octet-stream"
)
response.headers[
"Content-Disposition"
] = f"attachment; filename={file_name_encoded}"
return response
@router.delete("/files/{bucket}/{file_id}", dependencies=[Depends(check_api_key)])
async def delete_file(
bucket: str, file_id: str, service: Service = Depends(get_service)
):
"""Delete a file by file_id."""
await blocking_func_to_async(
global_system_app, service.delete_file, bucket, file_id
)
return Result.succ(None)
def init_endpoints(system_app: SystemApp) -> None:
"""Initialize the endpoints"""
global global_system_app
system_app.register(Service)
global_system_app = system_app