From 08a4a2bbb70db20b86c7ea46a46b03461cd4d48e Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 2 Nov 2023 21:30:49 +0000 Subject: [PATCH] Added things and thing descriptions views --- src/labthings_fastapi/thing_server.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/labthings_fastapi/thing_server.py b/src/labthings_fastapi/thing_server.py index b1a5f82..d188114 100644 --- a/src/labthings_fastapi/thing_server.py +++ b/src/labthings_fastapi/thing_server.py @@ -32,6 +32,7 @@ def __init__( self.settings_folder = settings_folder or "./settings" self.action_manager = ActionManager() self.action_manager.attach_to_app(self.app) + self.add_things_view_to_app() self._things: dict[str, Thing] = {} self.blocking_portal: Optional[BlockingPortal] = None global _thing_servers @@ -115,3 +116,18 @@ async def lifespan(self, app: FastAPI): # Remove the blocking portal - the event loop is about to stop. thing._labthings_blocking_portal = None self.blocking_portal = None + + def add_things_view_to_app(self): + """Add an endpoint that shows the list of attached things.""" + thing_server = self + @self.app.get("/thing_descriptions/") + def thing_descriptions() -> Mapping[str, Mapping]: + """A dictionary of all the things available from this server""" + return { + path: thing.thing_description(path) + for path, thing in thing_server.things.items() + } + @self.app.get("/things/") + def thing_paths() -> Sequence[str]: + """A dictionary of all the things available from this server""" + return list(thing_server.things.keys())