From 14c629f9a623ec641ab90df2b9c4fcfaa93847d6 Mon Sep 17 00:00:00 2001 From: Mark Jacobson <52427991+marksparkza@users.noreply.github.com> Date: Fri, 3 May 2024 13:54:02 +0200 Subject: [PATCH] Create archive API route for listing archived resources --- odp/api/routers/archive.py | 44 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/odp/api/routers/archive.py b/odp/api/routers/archive.py index f1b9e43..e4bc57b 100644 --- a/odp/api/routers/archive.py +++ b/odp/api/routers/archive.py @@ -4,10 +4,10 @@ from odp.api.lib.auth import Authorize from odp.api.lib.paging import Page, Paginator -from odp.api.models import ArchiveModel +from odp.api.models import ArchiveModel, ArchiveResourceModel from odp.const import ODPScope from odp.db import Session -from odp.db.models import Archive, ArchiveResource +from odp.db.models import Archive, ArchiveResource, Resource router = APIRouter() @@ -20,6 +20,23 @@ def output_archive_model(result) -> ArchiveModel: ) +def output_archive_resource_model(result) -> ArchiveResourceModel: + return ArchiveResourceModel( + archive_id=result.ArchiveResource.archive_id, + resource_id=result.ArchiveResource.resource_id, + path=result.ArchiveResource.path, + title=result.Resource.title, + description=result.Resource.description, + filename=result.Resource.filename, + mimetype=result.Resource.mimetype, + size=result.Resource.size, + md5=result.Resource.md5, + timestamp=result.Resource.timestamp.isoformat(), + provider_id=result.Resource.provider_id, + provider_key=result.Resource.provider.key, + ) + + @router.get( '/', response_model=Page[ArchiveModel], @@ -60,3 +77,26 @@ async def get_archive( raise HTTPException(HTTP_404_NOT_FOUND) return output_archive_model(result) + + +@router.get( + '/{archive_id}/resources', + response_model=Page[ArchiveResourceModel], + dependencies=[Depends(Authorize(ODPScope.ARCHIVE_READ))], +) +async def list_resources( + archive_id: str, + paginator: Paginator = Depends(), +): + if not Session.get(Archive, archive_id): + raise HTTPException(HTTP_404_NOT_FOUND) + + stmt = ( + select(ArchiveResource, Resource).join(Resource). + where(ArchiveResource.archive_id == archive_id) + ) + + return paginator.paginate( + stmt, + lambda row: output_archive_resource_model(row), + )