Skip to content

Commit cf13f9e

Browse files
Add global URN resolver
1 parent e2f4502 commit cf13f9e

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

task_registry/api/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from fastapi import APIRouter
2-
from task_registry.api.routes import health, instruments, measures, requirements
2+
from task_registry.api.routes import health, instruments, measures, requirements, urns
33

44
api_router = APIRouter()
55
api_router.include_router(health.router, prefix="/health", tags=["health"])
66
api_router.include_router(instruments.router, prefix="/instruments", tags=["instruments"])
77
api_router.include_router(measures.router, prefix="/measures", tags=["measures"])
88
api_router.include_router(requirements.router, prefix="/requirements", tags=["requirements"])
9+
api_router.include_router(urns.router, prefix="/urns", tags=["urn"])

task_registry/api/routes/urns.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
3+
from fastapi import APIRouter, HTTPException
4+
from fastapi.responses import JSONResponse
5+
from task_registry.data import Index, TaskType
6+
from task_registry.lifespan import CACHED_REGISTRY
7+
8+
router = APIRouter()
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
@router.get(
14+
"/{urn}",
15+
summary="Get the contents of the specific URN.",
16+
description="This endpoint returns a JSON with the contents of a specific URN"
17+
" and version.",
18+
responses={
19+
200: {"description": "JSON with the specific contents of the URN."},
20+
400: {"description": "The URN does not exist or is not valid."},
21+
},
22+
)
23+
async def get_urn(urn: str, version: str = "latest") -> JSONResponse:
24+
for taskType in TaskType:
25+
if CACHED_REGISTRY.has_urn(urn, taskType):
26+
return JSONResponse(content=CACHED_REGISTRY.get_task(urn, taskType))
27+
raise HTTPException(status_code=400, detail=f"Unknown urn: {urn}")

task_registry/data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def get_tasks_index(self, tasks: TaskType) -> Index:
7777
def get_task(self, urn: str, tasks: TaskType) -> dict[str, Any]:
7878
return self.tasks_cache[(urn, tasks)]
7979

80+
def has_urn(self, urn: str, tasks: TaskType) -> bool:
81+
return (urn, tasks) in self.tasks_cache
82+
8083

8184
def generate_index(
8285
tasks: TaskType,

tests/api/routes/test_urns.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from fastapi.testclient import TestClient
2+
3+
4+
def test_get_main_page(client: TestClient) -> None:
5+
# when
6+
response = client.get("/urns/")
7+
8+
# then
9+
assert response.status_code == 404
10+
11+
12+
def test_get_urn(client: TestClient) -> None:
13+
# when
14+
response = client.get("/urns/test_urn")
15+
16+
# then
17+
assert response.status_code == 200
18+
assert response.headers["content-type"] == "application/json"
19+
assert b'{"urn":"test_urn"}' in response.content.lower()
20+
21+
22+
def test_get_nonexistent_urn(client: TestClient) -> None:
23+
# when
24+
response = client.get("/urns/idonotexist")
25+
26+
# then
27+
assert response.status_code == 400
28+
assert response.headers["content-type"] == "application/json"
29+
assert b'{"detail":"unknown urn: idonotexist"}' in response.content.lower()

0 commit comments

Comments
 (0)