mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* docs(api): document error responses for roms routes Describe every 4xx and 5xx the rom routes and their helpers raise in each decorator's responses=, so the OpenAPI schema lists them. A hidden rom, platform or file is described like a missing one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for saves routes Describe every 4xx/5xx the saves, states, screenshots, snapshots, play-sessions and sync routes raise in their responses=, and rewrite the states identifiers docstring that carried Args/Returns blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for devices routes Describe the 4xx/5xx statuses that the device, device auth, install, client token, notification, notification channel, sync channel and heartbeat routes raise, and fill the empty install route entries. Rewrite the setup and heartbeat docstrings that published Args and Returns blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for streaming routes Describe the 4xx and 5xx statuses the streaming, memory card and netplay routes raise in their responses= declarations, and replace the Args: blocks in route docstrings that FastAPI published verbatim. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for admin routes Describe the 4xx and 5xx statuses the config, permissions, user, auth, tasks, logs, activity and export routes raise, and rewrite their Args/Returns docstrings as plain operation descriptions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): document error responses for library routes Describe the 400/403/404/409/500 statuses the collection, feed, firmware, platform, music, playlist and search routes raise, and replace route docstrings that carried Args/Returns blocks with plain sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): describe private collections and playlists as missing Matches #5686, where another user's private collection or playlist answers 404 and a play session on a hidden rom answers like a missing one. The read routes drop their 403, and the write 403 now covers only a public resource another user owns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): address Greptile review on route error docs Fold the nine multi-paragraph route docstrings into one line, and keep the device-scope 403 descriptions in utils/auth.py next to resolve_device, which raises them, instead of in both saves.py and states.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(api): plain-prose pass on route error descriptions Spell out clipped error descriptions, replace internal slang (picked, taken, live, moved past), name the parameter an ID comes from, and split the long save conversion 400s into sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
237 lines
7.0 KiB
Python
237 lines
7.0 KiB
Python
from datetime import datetime
|
|
from typing import Annotated
|
|
|
|
from fastapi import Body
|
|
from fastapi import Path as PathVar
|
|
from fastapi import Query, Request, status
|
|
|
|
from decorators.auth import protected_route
|
|
from endpoints.responses.platform import PlatformSchema
|
|
from exceptions.endpoint_exceptions import PlatformNotFoundInDatabaseException
|
|
from exceptions.fs_exceptions import PlatformAlreadyExistsException
|
|
from handler.audit_handler import AuditTarget, changed_fields, record
|
|
from handler.auth.constants import Scope
|
|
from handler.auth.dependencies import (
|
|
assert_can,
|
|
get_permissions,
|
|
get_rom_visibility_filter,
|
|
visible_platform_or_404,
|
|
)
|
|
from handler.database import db_platform_handler
|
|
from handler.filesystem import fs_platform_handler
|
|
from handler.scan_handler import scan_platform
|
|
from logger.formatter import BLUE
|
|
from logger.formatter import highlight as hl
|
|
from logger.logger import log
|
|
from models.audit_event import AuditAction
|
|
from models.permission import PermAction, PermEntity
|
|
from models.platform import (
|
|
CUSTOM_NAME_MAX_LENGTH,
|
|
DESCRIPTION_MAX_LENGTH,
|
|
)
|
|
from utils.platforms import get_filesystem_platforms, get_supported_platforms
|
|
from utils.router import APIRouter
|
|
|
|
router = APIRouter(
|
|
prefix="/platforms",
|
|
tags=["platforms"],
|
|
)
|
|
|
|
|
|
@protected_route(
|
|
router.post,
|
|
"",
|
|
[Scope.PLATFORMS_WRITE],
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
async def add_platform(
|
|
request: Request,
|
|
fs_slug: Annotated[str, Body(description="Platform slug.", embed=True)],
|
|
) -> PlatformSchema:
|
|
"""Create a platform."""
|
|
|
|
try:
|
|
await fs_platform_handler.add_platform(fs_slug=fs_slug)
|
|
except PlatformAlreadyExistsException:
|
|
log.info(f"Detected platform: {hl(fs_slug)}")
|
|
|
|
scanned_platform = await scan_platform(fs_slug, [fs_slug])
|
|
platform = db_platform_handler.add_platform(scanned_platform)
|
|
record(
|
|
AuditAction.PLATFORM_CREATE,
|
|
request,
|
|
AuditTarget.of_platform(platform),
|
|
{"fs_slug": fs_slug},
|
|
)
|
|
return PlatformSchema.model_validate(platform)
|
|
|
|
|
|
@protected_route(router.get, "", [Scope.PLATFORMS_READ])
|
|
def get_platforms(
|
|
request: Request,
|
|
updated_after: Annotated[
|
|
datetime | None,
|
|
Query(
|
|
description="Filter platforms updated after this datetime (ISO 8601 format with timezone information)."
|
|
),
|
|
] = None,
|
|
) -> list[PlatformSchema]:
|
|
"""Retrieve platforms."""
|
|
|
|
return [
|
|
PlatformSchema.model_validate(p)
|
|
for p in db_platform_handler.get_platforms(
|
|
updated_after=updated_after,
|
|
visibility=get_rom_visibility_filter(request),
|
|
)
|
|
]
|
|
|
|
|
|
@protected_route(router.get, "/identifiers", [Scope.PLATFORMS_READ])
|
|
def get_platform_identifiers(
|
|
request: Request,
|
|
) -> list[int]:
|
|
"""Retrieve platform identifiers."""
|
|
|
|
perms = get_permissions(request)
|
|
return db_platform_handler.get_platform_ids(
|
|
hidden_platform_ids=perms.hidden_platform_ids,
|
|
)
|
|
|
|
|
|
@protected_route(router.get, "/supported", [Scope.PLATFORMS_READ])
|
|
def get_supported_platforms_endpoint(request: Request) -> list[PlatformSchema]:
|
|
"""Retrieve the list of supported platforms."""
|
|
|
|
return get_supported_platforms(get_rom_visibility_filter(request))
|
|
|
|
|
|
@protected_route(router.get, "/filesystem", [Scope.PLATFORMS_READ])
|
|
async def get_filesystem_platforms_endpoint(request: Request) -> list[PlatformSchema]:
|
|
"""Retrieve platform folders on disk that have no database row yet."""
|
|
|
|
return await get_filesystem_platforms()
|
|
|
|
|
|
@protected_route(
|
|
router.get,
|
|
"/{id}",
|
|
[Scope.PLATFORMS_READ],
|
|
responses={
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "No platform the caller can see has this ID."
|
|
}
|
|
},
|
|
)
|
|
def get_platform(
|
|
request: Request,
|
|
id: Annotated[int, PathVar(description="Platform id.", ge=1)],
|
|
) -> PlatformSchema:
|
|
"""Retrieve a platform by ID."""
|
|
|
|
platform = db_platform_handler.get_platform(
|
|
id, visibility=get_rom_visibility_filter(request)
|
|
)
|
|
platform = visible_platform_or_404(request, platform, id)
|
|
return PlatformSchema.model_validate(platform)
|
|
|
|
|
|
@protected_route(
|
|
router.put,
|
|
"/{id}",
|
|
[Scope.PLATFORMS_WRITE],
|
|
responses={
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "No platform the caller can see has this ID."
|
|
}
|
|
},
|
|
)
|
|
async def update_platform(
|
|
request: Request,
|
|
id: Annotated[int, PathVar(description="Platform id.", ge=1)],
|
|
custom_name: Annotated[
|
|
str | None,
|
|
Body(
|
|
embed=True,
|
|
max_length=CUSTOM_NAME_MAX_LENGTH,
|
|
description="Custom platform name.",
|
|
),
|
|
] = None,
|
|
description: Annotated[
|
|
str | None,
|
|
Body(
|
|
embed=True,
|
|
max_length=DESCRIPTION_MAX_LENGTH,
|
|
description="Custom platform description.",
|
|
),
|
|
] = None,
|
|
) -> PlatformSchema:
|
|
"""Update a platform."""
|
|
|
|
platform_db = visible_platform_or_404(
|
|
request, db_platform_handler.get_platform(id), id
|
|
)
|
|
|
|
submitted = {"custom_name": custom_name, "description": description}
|
|
changed = changed_fields(
|
|
platform_db, {k: v for k, v in submitted.items() if v is not None}, submitted
|
|
)
|
|
if custom_name is not None:
|
|
platform_db.custom_name = custom_name
|
|
if description is not None:
|
|
platform_db.description = description
|
|
platform_db = db_platform_handler.add_platform(platform_db)
|
|
if changed:
|
|
record(
|
|
AuditAction.PLATFORM_EDIT,
|
|
request,
|
|
AuditTarget.of_platform(platform_db),
|
|
{"changed": changed},
|
|
)
|
|
|
|
visibility = get_rom_visibility_filter(request)
|
|
if not visibility.is_unrestricted:
|
|
# Re-read for the caller's totals; the saved row carries everyone's.
|
|
refreshed = db_platform_handler.get_platform(id, visibility=visibility)
|
|
if not refreshed:
|
|
raise PlatformNotFoundInDatabaseException(id)
|
|
platform_db = refreshed
|
|
return PlatformSchema.model_validate(platform_db)
|
|
|
|
|
|
@protected_route(
|
|
router.delete,
|
|
"/{id}",
|
|
[Scope.PLATFORMS_WRITE],
|
|
responses={
|
|
status.HTTP_403_FORBIDDEN: {
|
|
"description": "The caller is not allowed to delete platforms."
|
|
},
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "No platform the caller can see has this ID."
|
|
},
|
|
},
|
|
)
|
|
async def delete_platform(
|
|
request: Request,
|
|
id: Annotated[int, PathVar(description="Platform id.", ge=1)],
|
|
) -> None:
|
|
"""Delete a platform by ID."""
|
|
|
|
perms = get_permissions(request)
|
|
platform = visible_platform_or_404(
|
|
request, db_platform_handler.get_platform(id), id
|
|
)
|
|
assert_can(perms, PermEntity.PLATFORMS, PermAction.DELETE)
|
|
|
|
log.info(
|
|
f"Deleting {hl(platform.name, color=BLUE)} [{hl(platform.fs_slug)}] from database"
|
|
)
|
|
db_platform_handler.delete_platform(id)
|
|
record(
|
|
AuditAction.PLATFORM_DELETE,
|
|
request,
|
|
AuditTarget.of_platform(platform),
|
|
{"fs_slug": platform.fs_slug},
|
|
)
|