mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
misc: Improve API docs and annotations for platform endpoints
- Add type annotations for FastAPI query, and path parameters.
- Add type annotations for request body content.
- Update docstrings to clarify endpoint functionality, and remove
unnecessary details.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
|
||||
from decorators.auth import protected_route
|
||||
from endpoints.responses import MessageResponse
|
||||
from endpoints.responses.platform import PlatformSchema
|
||||
from exceptions.endpoint_exceptions import PlatformNotFoundInDatabaseException
|
||||
from exceptions.fs_exceptions import PlatformAlreadyExistsException
|
||||
from fastapi import Request
|
||||
from fastapi import Body
|
||||
from fastapi import Path as PathVar
|
||||
from fastapi import Request, status
|
||||
from handler.auth.constants import Scope
|
||||
from handler.database import db_platform_handler
|
||||
from handler.filesystem import fs_platform_handler
|
||||
@@ -22,19 +25,18 @@ router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
@protected_route(router.post, "", [Scope.PLATFORMS_WRITE])
|
||||
async def add_platforms(request: Request) -> PlatformSchema:
|
||||
"""Create platform endpoint
|
||||
@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."""
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
|
||||
Returns:
|
||||
PlatformSchema: Just created platform
|
||||
"""
|
||||
|
||||
data = await request.json()
|
||||
fs_slug = data["fs_slug"]
|
||||
try:
|
||||
fs_platform_handler.add_platforms(fs_slug=fs_slug)
|
||||
except PlatformAlreadyExistsException:
|
||||
@@ -47,15 +49,7 @@ async def add_platforms(request: Request) -> PlatformSchema:
|
||||
|
||||
@protected_route(router.get, "", [Scope.PLATFORMS_READ])
|
||||
def get_platforms(request: Request) -> list[PlatformSchema]:
|
||||
"""Get platforms endpoint
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
id (int, optional): Platform id. Defaults to None.
|
||||
|
||||
Returns:
|
||||
list[PlatformSchema]: List of platforms
|
||||
"""
|
||||
"""Retrieve platforms."""
|
||||
|
||||
return [
|
||||
PlatformSchema.model_validate(p) for p in db_platform_handler.get_platforms()
|
||||
@@ -64,23 +58,17 @@ def get_platforms(request: Request) -> list[PlatformSchema]:
|
||||
|
||||
@protected_route(router.get, "/supported", [Scope.PLATFORMS_READ])
|
||||
def get_supported_platforms(request: Request) -> list[PlatformSchema]:
|
||||
"""Get list of supported platforms endpoint
|
||||
"""Retrieve the list of supported platforms."""
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
|
||||
Returns:
|
||||
list[PlatformSchema]: List of supported platforms
|
||||
"""
|
||||
|
||||
supported_platforms = []
|
||||
db_platforms = db_platform_handler.get_platforms()
|
||||
db_platforms_map = {p.name: p.id for p in db_platforms}
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
supported_platforms = []
|
||||
for platform in IGDB_PLATFORM_LIST:
|
||||
now = datetime.now(timezone.utc)
|
||||
platform_id = db_platforms_map.get(platform["name"], -1)
|
||||
sup_plat = {
|
||||
"id": -1,
|
||||
"id": platform_id,
|
||||
"name": platform["name"],
|
||||
"fs_slug": platform["slug"],
|
||||
"slug": platform["slug"],
|
||||
@@ -94,78 +82,71 @@ def get_supported_platforms(request: Request) -> list[PlatformSchema]:
|
||||
"is_identified": True,
|
||||
"missing_from_fs": False,
|
||||
}
|
||||
|
||||
if platform["name"] in db_platforms_map:
|
||||
sup_plat["id"] = db_platforms_map[platform["name"]]
|
||||
|
||||
supported_platforms.append(PlatformSchema.model_validate(sup_plat).model_dump())
|
||||
|
||||
return supported_platforms
|
||||
|
||||
|
||||
@protected_route(router.get, "/{id}", [Scope.PLATFORMS_READ])
|
||||
def get_platform(request: Request, id: int) -> PlatformSchema:
|
||||
"""Get platforms endpoint
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
id (int, optional): Platform id. Defaults to None.
|
||||
|
||||
Returns:
|
||||
PlatformSchema: Platform
|
||||
"""
|
||||
@protected_route(
|
||||
router.get,
|
||||
"/{id}",
|
||||
[Scope.PLATFORMS_READ],
|
||||
responses={status.HTTP_404_NOT_FOUND: {}},
|
||||
)
|
||||
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)
|
||||
|
||||
if not platform:
|
||||
raise PlatformNotFoundInDatabaseException(id)
|
||||
|
||||
return PlatformSchema.model_validate(platform)
|
||||
|
||||
|
||||
@protected_route(router.put, "/{id}", [Scope.PLATFORMS_WRITE])
|
||||
async def update_platform(request: Request, id: int) -> PlatformSchema:
|
||||
"""Update platform endpoint
|
||||
@protected_route(
|
||||
router.put,
|
||||
"/{id}",
|
||||
[Scope.PLATFORMS_WRITE],
|
||||
responses={status.HTTP_404_NOT_FOUND: {}},
|
||||
)
|
||||
async def update_platform(
|
||||
request: Request,
|
||||
id: Annotated[int, PathVar(description="Platform id.", ge=1)],
|
||||
aspect_ratio: Annotated[str | None, Body(description="Cover aspect ratio.")] = None,
|
||||
custom_name: Annotated[
|
||||
str | None, Body(description="Custom platform name.")
|
||||
] = None,
|
||||
) -> PlatformSchema:
|
||||
"""Update a platform."""
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
id (int): Platform id
|
||||
|
||||
Returns:
|
||||
MessageResponse: Standard message response
|
||||
"""
|
||||
data = await request.json()
|
||||
platform_db = db_platform_handler.get_platform(id)
|
||||
|
||||
if not platform_db:
|
||||
raise PlatformNotFoundInDatabaseException(id)
|
||||
|
||||
platform_db.aspect_ratio = data.get("aspect_ratio", platform_db.aspect_ratio)
|
||||
platform_db.custom_name = data.get("custom_name", platform_db.custom_name)
|
||||
if aspect_ratio is not None:
|
||||
platform_db.aspect_ratio = aspect_ratio
|
||||
if custom_name is not None:
|
||||
platform_db.custom_name = custom_name
|
||||
platform_db = db_platform_handler.add_platform(platform_db)
|
||||
|
||||
return PlatformSchema.model_validate(platform_db)
|
||||
|
||||
|
||||
@protected_route(router.delete, "/{id}", [Scope.PLATFORMS_WRITE])
|
||||
async def delete_platforms(request: Request, id: int) -> MessageResponse:
|
||||
"""Delete platforms endpoint
|
||||
|
||||
Args:
|
||||
request (Request): Fastapi Request object
|
||||
{
|
||||
"platforms": List of rom's ids to delete
|
||||
}
|
||||
|
||||
Raises:
|
||||
HTTPException: Platform not found
|
||||
|
||||
Returns:
|
||||
MessageResponse: Standard message response
|
||||
"""
|
||||
@protected_route(
|
||||
router.delete,
|
||||
"/{id}",
|
||||
[Scope.PLATFORMS_WRITE],
|
||||
responses={status.HTTP_404_NOT_FOUND: {}},
|
||||
)
|
||||
async def delete_platform(
|
||||
request: Request,
|
||||
id: Annotated[int, PathVar(description="Platform id.", ge=1)],
|
||||
) -> MessageResponse:
|
||||
"""Delete a platform."""
|
||||
|
||||
platform = db_platform_handler.get_platform(id)
|
||||
|
||||
if not platform:
|
||||
raise PlatformNotFoundInDatabaseException(id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user