Skip to content

Commit

Permalink
Add global URN resolver (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert authored Dec 19, 2024
2 parents e2f4502 + f79ec8b commit a1798a9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion task_registry/api/main.py
Original file line number Diff line number Diff line change
@@ -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"])
26 changes: 26 additions & 0 deletions task_registry/api/routes/urns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging

from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from task_registry.data import TaskType
from task_registry.lifespan import CACHED_REGISTRY

router = APIRouter()

logger = logging.getLogger(__name__)


@router.get(
"/{urn}",
summary="Get the contents of the specific URN.",
description="This endpoint returns a JSON with the contents of a specific URN" " and version.",
responses={
200: {"description": "JSON with the specific contents of the URN."},
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}")
3 changes: 3 additions & 0 deletions task_registry/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions tests/api/routes/test_urns.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit a1798a9

Please sign in to comment.