From: Michael Tremer Date: Mon, 7 Jul 2025 13:05:56 +0000 (+0000) Subject: api: Create an endpoint to fetch the filelist of a package X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d4e7fd69c217c28a49f4c8a77f255fd5401dd70;p=pbs.git api: Create an endpoint to fetch the filelist of a package Signed-off-by: Michael Tremer --- diff --git a/src/api/packages.py b/src/api/packages.py index 65189cbb..b5af7974 100644 --- a/src/api/packages.py +++ b/src/api/packages.py @@ -27,7 +27,7 @@ from uuid import UUID from . import apiv1 from . import backend -from ..packages import Package +from ..packages import Package, File # Create a new router for all endpoints router = fastapi.APIRouter( @@ -35,6 +35,12 @@ router = fastapi.APIRouter( tags=["Packages"], ) +async def get_package_by_uuid(uuid: UUID) -> Package: + """ + Fetches a package by its UUID + """ + return await backend.packages.get_by_uuid(uuid) + class PackageSummary(pydantic.BaseModel): """ This is a helper model to shorten the list of packages which @@ -57,16 +63,15 @@ async def get_packages() -> list[PackageSummary]: @router.get("/{uuid:uuid}") -async def get_package_by_uuid(uuid: UUID) -> Package: - # Fetch the package by its UUID - pkg = await backend.packages.get_by_uuid(uuid) - if not pkg: +async def get_by_uuid( + package: Package = fastapi.Depends(get_package_by_uuid)) -> Package: + if not package: raise fastapi.HTTPException(404, "Could not find package %s" % id) - return pkg + return package @router.get("/{name}") -async def get_package(name: str) -> Package: +async def get_by_name(name: str) -> Package: build = await backend.builds.get_latest_by_name(name) if not build: raise fastapi.HTTPException(404, "Could not find package %s" % name) @@ -74,5 +79,14 @@ async def get_package(name: str) -> Package: # Return the source package return build.pkg +@router.get("/{uuid:uuid}/filelist") +async def get_filelist_by_uuid( + package: Package = fastapi.Depends(get_package_by_uuid)) -> list[File]: + if not package: + raise fastapi.HTTPException(404, "Could not find package") + + # Return the entire filelist + return [file async for file in await package.get_files()] + # Add everything to the APIv1 apiv1.include_router(router)