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

ActionManagerDep

A ready-made dependency type for the ActionManager object.

ActionManagerContext

ActionManagerContextDep

Functions

action_manager_from_thing_server(...)

Retrieve the Action Manager from the Thing Server.

make_action_manager_context_available(...)

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 request to access the ThingServer and thus access the ActionManager.

Parameters:

request – the FastAPI request object. This will be supplied by FastAPI when this function is used as a dependency.

Returns:

the ActionManager object associated with our ThingServer.

labthings_fastapi.dependencies.action_manager.ActionManagerDep

A ready-made dependency type for the ActionManager object.

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 ActionManager object. Note that this is an annotated type so it will be supplied automatically when used as a FastAPI dependency.

Yield:

the ActionManager object.

labthings_fastapi.dependencies.action_manager.ActionManagerContextDep: typing_extensions.TypeAlias