Files
llm-gateway-sub-backup/workspace/routers/api_key_router.py
2025-08-11 18:56:38 +09:00

43 lines
1.3 KiB
Python

from fastapi import APIRouter, Body, HTTPException
from services import api_key_service
router = APIRouter(prefix="/manage", tags=["API Key Management"])
@router.post("/keys", summary="Create a new API Key")
def create_key(
client_name: str = Body(
...,
embed=True,
description="Name of the client or service that will use this key.",
),
):
"""
새로운 API 키를 생성하고 시스템에 등록합니다.
"""
if not client_name:
raise HTTPException(status_code=400, detail="Client name is required.")
new_key_info = api_key_service.create_api_key(client_name)
return {"message": "API Key created successfully", "key_info": new_key_info}
@router.get("/keys", summary="List all API Keys")
def list_keys():
"""
현재 시스템에 등록된 모든 API 키의 정보를 조회합니다.
"""
keys = api_key_service.list_api_keys()
return {"keys": keys}
@router.delete("/keys/{api_key}", summary="Revoke an API Key")
def revoke_key(api_key: str):
"""
지정된 API 키를 시스템에서 영구적으로 삭제(폐기)합니다.
"""
success = api_key_service.revoke_api_key(api_key)
if not success:
raise HTTPException(status_code=404, detail="API Key not found.")
return {"message": f"API Key '{api_key}' has been revoked."}