Skip to content

Commit

Permalink
Create archive API route for listing archived resources
Browse files Browse the repository at this point in the history
  • Loading branch information
marksparkza committed May 3, 2024
1 parent 2ef7b4c commit 14c629f
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions odp/api/routers/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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],
Expand Down Expand Up @@ -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),
)

0 comments on commit 14c629f

Please sign in to comment.