labthings_fastapi.dependencies.raw_thing

Module Contents

Functions

find_raw_thing_by_class

Generate a function that locates the instance of a specific Thing subclass

raw_thing_dependency

Generate a dependency that will supply a particular Thing at runtime.

Data

ThingInstance

API

labthings_fastapi.dependencies.raw_thing.ThingInstance

‘TypeVar(…)’

labthings_fastapi.dependencies.raw_thing.find_raw_thing_by_class(cls: type[labthings_fastapi.dependencies.raw_thing.ThingInstance]) Callable[[fastapi.Request], labthings_fastapi.dependencies.raw_thing.ThingInstance]

Generate a function that locates the instance of a specific Thing subclass

Warning

Using a raw thing.Thing can be tricky: unless you really need to, it is usually better to use an internal thing client, 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 OtherThing attached to your thing server, declare your argument type as:

def endpoint(
    other_thing: Annotated[OtherThing, Depends(find_thing_by_class(OtherThing)]
):
    pass

LabThings will supply this argument automatically through the FastAPI dependency mechanism.

labthings_fastapi.dependencies.raw_thing.raw_thing_dependency(cls: type[labthings_fastapi.dependencies.raw_thing.ThingInstance]) type[labthings_fastapi.dependencies.raw_thing.ThingInstance]

Generate a dependency that will supply a particular Thing at runtime.

Warning

Using a raw labthings_fastapi.thing.Thing can be tricky: unless you really need to, it is usually better to use labthings_fastapi.dependencies.thing.direct_thing_client_dependency, 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.

This function should make it simple to depend on a Thing object directly. If you declare a type alias using this function, it will include an annotation that prompts FastAPI to supply the instance of the class.

Usage:

from my_other_thing import MyOtherThing as MyOtherThingClass

MyOtherThing = raw_thing_dependency(MyOtherThingClass)

class MyThing(Thing):
    @thing_action
    def do_something(self, other_thing: MyOtherThing) -> None:
        "This action needs no arguments"
        other_thing.function_only_available_in_python()
Parameters:

cls – The class of the Thing that will be supplied

Returns:

A type alias that works as a dependency to supply an instance of cls at runtime.