labthings_fastapi.dependencies.action_manager
Context Var access to the Action Manager.
This module provides a context var to access the Action Manager instance.
While LabThings tries pretty hard to conform to FastAPI’s excellent convention
that everything should be passed as a parameter, there are some cases where
that’s hard. In particular, generating URLs when responses are serialised is
difficult, because pydantic doesn’t have a way to access the fastapi.Request
object and use fastapi.Request.url_for.
If an endpoint uses the ActionManagerDep dependency, then the ActionManager
is supplied as an argument. More usefully, when the output is serialised the
ActionManager is available using ActionManagerContext.get().
This is currently only used by Blob objects, as “serialising” a Blob
involves adding it to the ActionManager and generating a download URL.
Attributes
A ready-made dependency type for the |
|
Functions
Retrieve the Action Manager from the Thing Server. |
|
Make the Action Manager available in the context variable. |
Module Contents
- labthings_fastapi.dependencies.action_manager.action_manager_from_thing_server(request: fastapi.Request) labthings_fastapi.actions.ActionManager
Retrieve the Action Manager from the Thing Server.
This is for use as a FastAPI dependency. We use the
requestto access theThingServerand thus access theActionManager.- Parameters:
request – the FastAPI request object. This will be supplied by FastAPI when this function is used as a dependency.
- Returns:
the
ActionManagerobject associated with ourThingServer.
- labthings_fastapi.dependencies.action_manager.ActionManagerDep
A ready-made dependency type for the
ActionManagerobject.
- labthings_fastapi.dependencies.action_manager.ActionManagerContext
- async labthings_fastapi.dependencies.action_manager.make_action_manager_context_available(action_manager: ActionManagerDep) AsyncGenerator[labthings_fastapi.actions.ActionManager]
Make the Action Manager available in the context variable.
The action manager may be accessed using
ActionManagerContext.get()within this context manager.- Parameters:
action_manager – The
ActionManagerobject. Note that this is an annotated type so it will be supplied automatically when used as a FastAPI dependency.- Yield:
the
ActionManagerobject.
- labthings_fastapi.dependencies.action_manager.ActionManagerContextDep: typing_extensions.TypeAlias