Skip to content

Commit 7f7ae99

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 0007781 commit 7f7ae99

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
@@ -342,6 +342,11 @@ def _things_view_router(self) -> APIRouter:
342342
router = APIRouter()
343343
thing_server = self
344344

345+
@router.get(
346+
"/thing_descriptions/",
347+
response_model_exclude_none=True,
348+
response_model_by_alias=True,
349+
)
345350
def thing_descriptions(request: Request) -> Mapping[str, ThingDescription]:
346351
"""Describe all the things available from this server.
347352
@@ -362,16 +367,7 @@ def thing_descriptions(request: Request) -> Mapping[str, ThingDescription]:
362367
for name, thing in thing_server.things.items()
363368
}
364369

365-
router.add_api_route(
366-
"/thing_descriptions/",
367-
thing_descriptions,
368-
response_model_exclude_none=True,
369-
response_model_by_alias=True,
370-
)
371-
372-
return router
373-
374-
@self.app.get("/things/")
370+
@router.get("/things/")
375371
def thing_paths(request: Request) -> Mapping[str, str]:
376372
"""URLs pointing to the Thing Descriptions of each Thing.
377373
@@ -381,6 +377,8 @@ def thing_paths(request: Request) -> Mapping[str, str]:
381377
URLs will return the :ref:`wot_td` of one `.Thing` each.
382378
""" # noqa: D403 (URLs is correct capitalisation)
383379
return {
384-
t: f"{str(request.base_url).rstrip('/')}{t}"
380+
t: str(request.url_for(f"things.{t}"))
385381
for t in thing_server.things.keys()
386382
}
383+
384+
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)