]> git.ipfire.org Git - pbs.git/commitdiff
API: Add an endpoint to fetch a specific repository from a distro
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jun 2025 13:53:59 +0000 (13:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 30 Jun 2025 13:53:59 +0000 (13:53 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/distros.py

index ead5470a17a68cecb3b3a20d9b78555b9d6180ce..1a100678981a6717ffbf2c110e4ffee056d04705 100644 (file)
@@ -33,12 +33,25 @@ router = fastapi.APIRouter(
        tags=["Distributions"],
 )
 
-async def get_from_path(distro: str = fastapi.Path(...)) -> Distro:
+async def get_distro_from_path(distro: str = fastapi.Path(...)) -> Distro:
        """
                Fetches the distribution from the name in the path
        """
        return await backend.distros.get_by_slug(distro)
 
+async def get_repo_from_path(
+               distro: typing.Optional[Distro] = fastapi.Depends(get_distro_from_path),
+               repo: str = fastapi.Path(...)) -> typing.Optional[Repo]:
+       """
+               Fetches the repository from the name in the path
+       """
+       # Return None if we could not find the distro
+       if distro is None:
+               return None
+
+       # Return the repo
+       return await distro.get_repo(repo)
+
 @router.get("")
 async def get_distros() -> typing.List[Distro]:
        """
@@ -47,7 +60,7 @@ async def get_distros() -> typing.List[Distro]:
        return [distro async for distro in backend.distros]
 
 @router.get("/{distro}")
-async def get_distro(distro = fastapi.Depends(get_from_path)) -> Distro:
+async def get_distro(distro = fastapi.Depends(get_distro_from_path)) -> Distro:
        """
                An endpoint to fetch information about a specific distribution
        """
@@ -58,7 +71,7 @@ async def get_distro(distro = fastapi.Depends(get_from_path)) -> Distro:
        return distro
 
 @router.get("/{distro}/repos")
-async def get_repos(distro = fastapi.Depends(get_from_path)) -> typing.List[Repo]:
+async def get_repos(distro = fastapi.Depends(get_distro_from_path)) -> typing.List[Repo]:
        """
                An endpoint to fetch all repositories that belong to a distro
        """
@@ -69,5 +82,17 @@ async def get_repos(distro = fastapi.Depends(get_from_path)) -> typing.List[Repo
        # Return a list with all repos
        return await distro.get_repos()
 
+@router.get("/{distro}/repos/{repo}")
+async def get_repo(repo = fastapi.Depends(get_repo_from_path)) -> Repo:
+       """
+               An endpoint to fetch all repositories that belong to a distro
+       """
+       # Send 404 if we could not find the repo
+       if not repo:
+               raise fastapi.HTTPException(404, "Could not find the repository")
+
+       # Return the repository
+       return repo
+
 # Add everything to the APIv1
 apiv1.include_router(router)