generated from coulomb/repo-seed
feat: use hub-core policy router
This commit is contained in:
@@ -1,23 +1,13 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi import HTTPException
|
||||
from hub_core.routers.policy import create_policy_router
|
||||
from hub_core.schemas.policy import PolicyRead
|
||||
|
||||
POLICY_DIR = Path(__file__).parent.parent.parent / "policies"
|
||||
_VALID_NAME = re.compile(r"^[a-z0-9][a-z0-9-]{0,63}$")
|
||||
|
||||
router = APIRouter(prefix="/policy", tags=["policy"])
|
||||
|
||||
|
||||
class PolicyRead(BaseModel):
|
||||
name: str
|
||||
content: str
|
||||
|
||||
|
||||
class PolicyUpdate(BaseModel):
|
||||
content: str
|
||||
|
||||
|
||||
def _policy_path(name: str) -> Path:
|
||||
if not _VALID_NAME.match(name):
|
||||
@@ -28,14 +18,17 @@ def _policy_path(name: str) -> Path:
|
||||
return path
|
||||
|
||||
|
||||
@router.get("/{name}", response_model=PolicyRead)
|
||||
def get_policy(name: str) -> PolicyRead:
|
||||
def _load_policy(name: str) -> PolicyRead:
|
||||
path = _policy_path(name)
|
||||
return PolicyRead(name=name, content=path.read_text())
|
||||
|
||||
|
||||
@router.put("/{name}", response_model=PolicyRead)
|
||||
def update_policy(name: str, body: PolicyUpdate) -> PolicyRead:
|
||||
def _update_policy(name: str, content: str) -> PolicyRead:
|
||||
path = _policy_path(name)
|
||||
path.write_text(body.content)
|
||||
return PolicyRead(name=name, content=body.content)
|
||||
path.write_text(content)
|
||||
return PolicyRead(name=name, content=content)
|
||||
|
||||
|
||||
router = create_policy_router(_load_policy, update_policy=_update_policy)
|
||||
|
||||
__all__ = ["router"]
|
||||
|
||||
Reference in New Issue
Block a user