Skip to content

Commit

Permalink
Added things and thing descriptions views
Browse files Browse the repository at this point in the history
  • Loading branch information
rwb27 committed Nov 2, 2023
1 parent a184776 commit 08a4a2b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/labthings_fastapi/thing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())

0 comments on commit 08a4a2b

Please sign in to comment.