]> git.ipfire.org Git - pbs.git/commitdiff
api: Add an endpoint that lists all known packages
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Jul 2025 11:03:41 +0000 (11:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Jul 2025 11:03:41 +0000 (11:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/api/__init__.py
src/api/packages.py [new file with mode: 0644]
src/buildservice/packages.py

index 97433b12e8e5cd9c0727e9a3ad3e8eb51b108cea..83ac3a6021ff895d1d2fa195a315c291bbe2ad4f 100644 (file)
@@ -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
index 1aab1d3e77ed2622a9cb29e7f603520a42cf03d7..27bd851e185d17af516b5ec02d0b70828505bc3d 100644 (file)
@@ -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 (file)
index 0000000..4b333b2
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+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)
index f8b2025dd2c8a7010355133b837fc3ce8861633b..f9d87c2d6d8bb96cb82767dcf4d7fb0b169be686 100644 (file)
@@ -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 = (