From: Michael Tremer Date: Mon, 30 Jun 2025 13:53:59 +0000 (+0000) Subject: API: Add an endpoint to fetch a specific repository from a distro X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce24c2e1cd6b0c6291f697fa243eccdafb1c9ecc;p=pbs.git API: Add an endpoint to fetch a specific repository from a distro Signed-off-by: Michael Tremer --- diff --git a/src/api/distros.py b/src/api/distros.py index ead5470a..1a100678 100644 --- a/src/api/distros.py +++ b/src/api/distros.py @@ -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)