labthings_fastapi.dependencies.raw_thing
FastAPI dependency to obtain a Thing directly.
This module allows actions to obtain a Thing instance without the
DirectThingClient wrapper. As a rule, it is best to use DirectThingClient
where possible.
Attributes
Functions
|
Generate a function that locates the instance of a Thing subclass. |
Module Contents
- labthings_fastapi.dependencies.raw_thing.ThingInstance
- labthings_fastapi.dependencies.raw_thing.find_raw_thing_by_class(cls: type[ThingInstance]) Callable[[fastapi.Request], ThingInstance]
Generate a function that locates the instance of a Thing subclass.
Warning
Using a
Thingdirectly can be tricky: unless you really need to, it is usually better to use aDirectThingClient, which provides an interface that should be identical to the HTTP thing client in Python. This is safer, and means code should be easier to translate between server and client-side.In order to access the instance of
OtherThingattached to your thing server, declare your argument type as:OtherThingDep = Annotated[ OtherThing, Depends(find_raw_thing_by_class(OtherThing)) ] def endpoint(other_thing: OtherThingDep): pass
LabThings will supply this argument automatically through the Dependencies mechanism.
Note that this function returns a dependency - it should be called with arguments inside
fastapi.Depends.- Parameters:
cls – is the
Thingsubclass that will be returned by the dependency.- Returns:
a dependency suitable for use with
fastapi.Depends(see example).