81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
"""HTTP mapping for the minimal component registry."""
|
|
|
|
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
from app.components.dependencies import get_component_service
|
|
from app.components.exceptions import (
|
|
ComponentActorRequired,
|
|
ComponentNotFound,
|
|
ComponentValidationError,
|
|
DuplicateComponentAssetID,
|
|
)
|
|
from app.components.models import (
|
|
ComponentCreateRequest,
|
|
ComponentResponse,
|
|
)
|
|
from app.components.service import ComponentService
|
|
from app.identity import require_permission
|
|
|
|
|
|
router = APIRouter(prefix="/api/components", tags=["components"])
|
|
require_component_read = require_permission("components.read")
|
|
require_component_create = require_permission("components.create")
|
|
|
|
ComponentServiceDependency = Annotated[
|
|
ComponentService,
|
|
Depends(get_component_service),
|
|
]
|
|
ComponentReadUser = Annotated[dict, Depends(require_component_read)]
|
|
ComponentCreateUser = Annotated[dict, Depends(require_component_create)]
|
|
|
|
|
|
@router.post(
|
|
"",
|
|
response_model=ComponentResponse,
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
def create_component(
|
|
request: ComponentCreateRequest,
|
|
service: ComponentServiceDependency,
|
|
current_user: ComponentCreateUser,
|
|
) -> ComponentResponse:
|
|
try:
|
|
component = service.create_component(
|
|
request,
|
|
actor_user_id=UUID(str(current_user["id"])),
|
|
)
|
|
except DuplicateComponentAssetID as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=str(exc),
|
|
) from exc
|
|
except (ComponentActorRequired, ComponentValidationError) as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
detail=str(exc),
|
|
) from exc
|
|
return ComponentResponse(data=component)
|
|
|
|
|
|
@router.get(
|
|
"/{component_id}",
|
|
response_model=ComponentResponse,
|
|
)
|
|
def get_component(
|
|
component_id: UUID,
|
|
service: ComponentServiceDependency,
|
|
current_user: ComponentReadUser,
|
|
) -> ComponentResponse:
|
|
del current_user
|
|
try:
|
|
component = service.get_component(component_id)
|
|
except ComponentNotFound as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Component not found",
|
|
) from exc
|
|
return ComponentResponse(data=component)
|