From 4e6c3d9da3c92910792aeb5f4cd32b5b4e8d3bce Mon Sep 17 00:00:00 2001 From: robbertuittenbroek Date: Wed, 18 Dec 2024 16:04:05 +0100 Subject: [PATCH] Add global URN resolver --- task_registry/api/main.py | 3 ++- task_registry/api/routes/urns.py | 27 +++++++++++++++++++++++++++ task_registry/data.py | 3 +++ tests/api/routes/test_urns.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 task_registry/api/routes/urns.py create mode 100644 tests/api/routes/test_urns.py diff --git a/task_registry/api/main.py b/task_registry/api/main.py index 4ffc9f7..d9a7ae8 100644 --- a/task_registry/api/main.py +++ b/task_registry/api/main.py @@ -1,8 +1,9 @@ from fastapi import APIRouter -from task_registry.api.routes import health, instruments, measures, requirements +from task_registry.api.routes import health, instruments, measures, requirements, urns api_router = APIRouter() api_router.include_router(health.router, prefix="/health", tags=["health"]) api_router.include_router(instruments.router, prefix="/instruments", tags=["instruments"]) api_router.include_router(measures.router, prefix="/measures", tags=["measures"]) api_router.include_router(requirements.router, prefix="/requirements", tags=["requirements"]) +api_router.include_router(urns.router, prefix="/urns", tags=["urn"]) diff --git a/task_registry/api/routes/urns.py b/task_registry/api/routes/urns.py new file mode 100644 index 0000000..2e7effd --- /dev/null +++ b/task_registry/api/routes/urns.py @@ -0,0 +1,27 @@ +import logging + +from fastapi import APIRouter, HTTPException +from fastapi.responses import JSONResponse +from task_registry.data import Index, TaskType +from task_registry.lifespan import CACHED_REGISTRY + +router = APIRouter() + +logger = logging.getLogger(__name__) + + +@router.get( + "/{urn}", + summary="Get the contents of the specific instrument which has given URN.", + description="This endpoint returns a JSON with the contents of a specific instrument identified by URN" + " and version.", + responses={ + 200: {"description": "JSON with the specific contents of the instrument."}, + 400: {"description": "The URN does not exist or is not valid."}, + }, +) +async def get_urn(urn: str, version: str = "latest") -> JSONResponse: + for taskType in TaskType: + if CACHED_REGISTRY.has_urn(urn, taskType): + return JSONResponse(content=CACHED_REGISTRY.get_task(urn, taskType)) + raise HTTPException(status_code=400, detail=f"Unknown urn: {urn}") diff --git a/task_registry/data.py b/task_registry/data.py index ff85978..1a25e8a 100644 --- a/task_registry/data.py +++ b/task_registry/data.py @@ -77,6 +77,9 @@ def get_tasks_index(self, tasks: TaskType) -> Index: def get_task(self, urn: str, tasks: TaskType) -> dict[str, Any]: return self.tasks_cache[(urn, tasks)] + def has_urn(self, urn: str, tasks: TaskType) -> bool: + return (urn, tasks) in self.tasks_cache + def generate_index( tasks: TaskType, diff --git a/tests/api/routes/test_urns.py b/tests/api/routes/test_urns.py new file mode 100644 index 0000000..178b38c --- /dev/null +++ b/tests/api/routes/test_urns.py @@ -0,0 +1,29 @@ +from fastapi.testclient import TestClient + + +def test_get_main_page(client: TestClient) -> None: + # when + response = client.get("/urns/") + + # then + assert response.status_code == 404 + + +def test_get_urn(client: TestClient) -> None: + # when + response = client.get("/urns/test_urn") + + # then + assert response.status_code == 200 + assert response.headers["content-type"] == "application/json" + assert b'{"urn":"test_urn"}' in response.content.lower() + + +def test_get_nonexistent_urn(client: TestClient) -> None: + # when + response = client.get("/urns/idonotexist") + + # then + assert response.status_code == 400 + assert response.headers["content-type"] == "application/json" + assert b'{"detail":"unknown urn: idonotexist"}' in response.content.lower()