Add project registration endpoint
This commit is contained in:
@@ -37,6 +37,7 @@ from atocore.projects.registry import (
|
||||
build_project_registration_proposal,
|
||||
get_project_registry_template,
|
||||
list_registered_projects,
|
||||
register_project,
|
||||
refresh_registered_project,
|
||||
)
|
||||
from atocore.retrieval.retriever import retrieve
|
||||
@@ -202,6 +203,20 @@ def api_project_registration_proposal(req: ProjectRegistrationProposalRequest) -
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/projects/register")
|
||||
def api_project_registration(req: ProjectRegistrationProposalRequest) -> dict:
|
||||
"""Persist a validated project registration to the registry file."""
|
||||
try:
|
||||
return register_project(
|
||||
project_id=req.project_id,
|
||||
aliases=req.aliases,
|
||||
description=req.description,
|
||||
ingest_roots=req.ingest_roots,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/projects/{project_name}/refresh", response_model=ProjectRefreshResponse)
|
||||
def api_refresh_project(project_name: str, purge_deleted: bool = False) -> ProjectRefreshResponse:
|
||||
"""Refresh one registered project from its configured ingest roots."""
|
||||
|
||||
@@ -106,6 +106,7 @@ class Settings(BaseSettings):
|
||||
self.resolved_log_dir,
|
||||
self.resolved_backup_dir,
|
||||
self.resolved_run_dir,
|
||||
self.resolved_project_registry_path.parent,
|
||||
]
|
||||
|
||||
@property
|
||||
|
||||
@@ -93,13 +93,38 @@ def build_project_registration_proposal(
|
||||
}
|
||||
|
||||
|
||||
def register_project(
|
||||
project_id: str,
|
||||
aliases: list[str] | tuple[str, ...] | None = None,
|
||||
description: str = "",
|
||||
ingest_roots: list[dict] | tuple[dict, ...] | None = None,
|
||||
) -> dict:
|
||||
"""Persist a validated project registration to the registry file."""
|
||||
proposal = build_project_registration_proposal(
|
||||
project_id=project_id,
|
||||
aliases=aliases,
|
||||
description=description,
|
||||
ingest_roots=ingest_roots,
|
||||
)
|
||||
if not proposal["valid"]:
|
||||
collision_names = ", ".join(collision["name"] for collision in proposal["collisions"])
|
||||
raise ValueError(f"Project registration has collisions: {collision_names}")
|
||||
|
||||
registry_path = _config.settings.resolved_project_registry_path
|
||||
payload = _load_registry_payload(registry_path)
|
||||
payload.setdefault("projects", []).append(proposal["project"])
|
||||
_write_registry_payload(registry_path, payload)
|
||||
|
||||
return {
|
||||
**proposal,
|
||||
"status": "registered",
|
||||
}
|
||||
|
||||
|
||||
def load_project_registry() -> list[RegisteredProject]:
|
||||
"""Load project registry entries from JSON config."""
|
||||
registry_path = _config.settings.resolved_project_registry_path
|
||||
if not registry_path.exists():
|
||||
return []
|
||||
|
||||
payload = json.loads(registry_path.read_text(encoding="utf-8"))
|
||||
payload = _load_registry_payload(registry_path)
|
||||
entries = payload.get("projects", [])
|
||||
projects: list[RegisteredProject] = []
|
||||
|
||||
@@ -285,3 +310,17 @@ def _find_name_collisions(project_id: str, aliases: list[str]) -> list[dict]:
|
||||
)
|
||||
break
|
||||
return collisions
|
||||
|
||||
|
||||
def _load_registry_payload(registry_path: Path) -> dict:
|
||||
if not registry_path.exists():
|
||||
return {"projects": []}
|
||||
return json.loads(registry_path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def _write_registry_payload(registry_path: Path, payload: dict) -> None:
|
||||
registry_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
registry_path.write_text(
|
||||
json.dumps(payload, indent=2, ensure_ascii=True) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user