Skip to content

Commit 8059d4e

Browse files
committed
Fix thing_descriptions and things endpoints.
I'd accidentally modified these endpoints (and deleted `things`) when I changed the function that added them. I've now added tests for these endpoints, and fixed the URL generation in `things`.
1 parent 9f8da53 commit 8059d4e

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/labthings_fastapi/server/__init__.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ def _things_view_router(self) -> APIRouter:
337337
router = APIRouter()
338338
thing_server = self
339339

340+
@router.get(
341+
"/thing_descriptions/",
342+
response_model_exclude_none=True,
343+
response_model_by_alias=True,
344+
)
340345
def thing_descriptions(request: Request) -> Mapping[str, ThingDescription]:
341346
"""Describe all the things available from this server.
342347
@@ -357,16 +362,7 @@ def thing_descriptions(request: Request) -> Mapping[str, ThingDescription]:
357362
for name, thing in thing_server.things.items()
358363
}
359364

360-
router.add_api_route(
361-
"/thing_descriptions/",
362-
thing_descriptions,
363-
response_model_exclude_none=True,
364-
response_model_by_alias=True,
365-
)
366-
367-
return router
368-
369-
@self.app.get("/things/")
365+
@router.get("/things/")
370366
def thing_paths(request: Request) -> Mapping[str, str]:
371367
"""URLs pointing to the Thing Descriptions of each Thing.
372368
@@ -376,6 +372,8 @@ def thing_paths(request: Request) -> Mapping[str, str]:
376372
URLs will return the :ref:`wot_td` of one `.Thing` each.
377373
""" # noqa: D403 (URLs is correct capitalisation)
378374
return {
379-
t: f"{str(request.base_url).rstrip('/')}{t}"
375+
t: str(request.url_for(f"things.{t}"))
380376
for t in thing_server.things.keys()
381377
}
378+
379+
return router

src/labthings_fastapi/thing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def attach_to_server(self, server: ThingServer) -> None:
176176

177177
@server.app.get(
178178
self.path,
179+
name=f"things.{self.name}",
179180
summary=get_summary(self.thing_description),
180181
description=get_docstring(self.thing_description),
181182
response_model_exclude_none=True,

tests/test_server.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from fastapi.testclient import TestClient
1010
from starlette.routing import Route
1111

12+
from labthings_fastapi.example_things import MyThing
13+
1214

1315
def test_server_from_config_non_thing_error():
1416
"""Test a typeerror is raised if something that's not a Thing is added."""
@@ -81,6 +83,7 @@ class Example(lt.Thing):
8183
"/api/v3/action_invocations/{id}",
8284
"/api/v3/blob/{blob_id}",
8385
"/api/v3/thing_descriptions/",
86+
"/api/v3/things/",
8487
"/api/v3/example/",
8588
]:
8689
assert expected_path in paths
@@ -92,3 +95,33 @@ class Example(lt.Thing):
9295
"/docs/oauth2-redirect",
9396
"/redoc",
9497
}
98+
99+
100+
def test_things_endpoints():
101+
"""Test that the two endpoints for listing Things work."""
102+
server = lt.ThingServer(
103+
{
104+
"thing_a": MyThing,
105+
"thing_b": MyThing,
106+
}
107+
)
108+
with TestClient(server.app) as client:
109+
# Check the thing_descriptions endpoint
110+
response = client.get("/thing_descriptions/")
111+
response.raise_for_status()
112+
tds = response.json()
113+
assert "thing_a" in tds
114+
assert "thing_b" in tds
115+
116+
# Check the things endpoint. This should map names to URLs
117+
response = client.get("/things/")
118+
response.raise_for_status()
119+
things = response.json()
120+
assert "thing_a" in things
121+
assert "thing_b" in things
122+
123+
# Fetch a thing description from the URL in `things`
124+
response = client.get(things["thing_a"])
125+
response.raise_for_status()
126+
td = response.json()
127+
assert td["title"] == "MyThing"

0 commit comments

Comments
 (0)