diff --git a/aiida_optimade/routers/references.py b/aiida_optimade/routers/references.py new file mode 100644 index 00000000..6a63465e --- /dev/null +++ b/aiida_optimade/routers/references.py @@ -0,0 +1,69 @@ +from typing import Union + +from fastapi import APIRouter, Depends +from starlette.requests import Request + +from aiida import orm + +from optimade.models import ( + ErrorResponse, + ReferenceResource, + ReferenceResponseMany, + ReferenceResponseOne, +) + +from aiida_optimade.query_params import EntryListingQueryParams, SingleEntryQueryParams +from aiida_optimade.entry_collections import AiidaCollection +from aiida_optimade.mappers import ReferenceMapper +from aiida_optimade.utils import get_backend + +from .utils import get_entries, get_single_entry + + +router = APIRouter() + +references = AiidaCollection( + orm.ReferenceData.objects, ReferenceResource, ReferenceMapper +) + + +@router.get( + "/references", + response_model=Union[ReferenceResponseMany, ErrorResponse], + response_model_skip_defaults=True, + tags=["Reference"], +) +def get_references( + request: Request, + params: EntryListingQueryParams = Depends(), + backend: orm.implementation.Backend = Depends(get_backend), +): + return get_entries( + backend=backend, + collection=references, + response=ReferenceResponseMany, + request=request, + params=params, + ) + + +@router.get( + "/references/{entry_id}", + response_model=Union[ReferenceResponseOne, ErrorResponse], + response_model_skip_defaults=True, + tags=["Reference"], +) +def get_single_reference( + request: Request, + entry_id: int, + params: SingleEntryQueryParams = Depends(), + backend: orm.implementation.Backend = Depends(get_backend), +): + return get_single_entry( + backend=backend, + collection=references, + entry_id=entry_id, + response=ReferenceResponseOne, + request=request, + params=params, + ) diff --git a/pytest.ini b/pytest.ini index f912cf47..0cd57be9 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,4 +2,4 @@ filterwarnings = ignore:.*PY_SSIZE_T_CLEAN will be required for '#' formats.*:DeprecationWarning ignore:.*"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead.*:DeprecationWarning - ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working:DeprecationWarning + ignore:.*Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning