From: Michael Tremer Date: Mon, 7 Jul 2025 11:03:41 +0000 (+0000) Subject: api: Add an endpoint that lists all known packages X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c6fb88c8d77ada2cc6d7fe3a03bfc07b94e4603;p=pbs.git api: Add an endpoint that lists all known packages Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 97433b12..83ac3a60 100644 --- a/Makefile.am +++ b/Makefile.am @@ -129,6 +129,7 @@ api_PYTHON = \ src/api/downloads.py \ src/api/events.py \ src/api/mirrors.py \ + src/api/packages.py \ src/api/uploads.py \ src/api/users.py \ src/api/util.py diff --git a/src/api/__init__.py b/src/api/__init__.py index 1aab1d3e..27bd851e 100644 --- a/src/api/__init__.py +++ b/src/api/__init__.py @@ -59,6 +59,7 @@ from . import distros from . import downloads from . import events from . import mirrors +from . import packages from . import uploads from . import users diff --git a/src/api/packages.py b/src/api/packages.py new file mode 100644 index 00000000..4b333b2a --- /dev/null +++ b/src/api/packages.py @@ -0,0 +1,54 @@ +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import fastapi +import pydantic + +from . import apiv1 +from . import backend + +# Create a new router for all endpoints +router = fastapi.APIRouter( + prefix="/packages", + tags=["Packages"], +) + +class PackageSummary(pydantic.BaseModel): + """ + This is a helper model to shorten the list of packages which + would otherwise generate an API response that is just under + half a megabyte. + """ + # Name + name: str + + # Summary + summary: str + + +@router.get("") +async def get_packages() -> list[PackageSummary]: + """ + An endpoint to list all available packages + """ + return [package async for package in backend.packages] + +# Add everything to the APIv1 +apiv1.include_router(router) diff --git a/src/buildservice/packages.py b/src/buildservice/packages.py index f8b2025d..f9d87c2d 100644 --- a/src/buildservice/packages.py +++ b/src/buildservice/packages.py @@ -30,35 +30,36 @@ from .errors import * log = logging.getLogger("pbs.packages") class Packages(base.Object): - async def list(self): + def __aiter__(self): """ Returns a list with all package names and the summary line that have at one time been part of the distribution """ - stmt = \ - sqlalchemy.select( + stmt = ( + sqlmodel + .select( + Package, + ) + .distinct( Package.name, - Package.summary, - Package.created_at, - ) \ - .distinct(Package.name) \ - .select_from(Package) \ + ) .join( builds.Build, Package.id == builds.Build.pkg_id, isouter=True, - ) \ + ) .where( Package.deleted_at == None, builds.Build.deleted_at == None, Package.arch == "src", - ) \ + ) .order_by( Package.name, Package.created_at.desc(), ) + ) - return self.db.select(stmt) + return self.db.fetch(stmt) async def get_by_uuid(self, uuid): stmt = (