labthings_fastapi.decorators

Actions

You can add an Action to a Thing by declaring a method, decorated with @thing_action. Parameters are not usually needed, but can be supplied to set various options.

Properties

As with Actions, Properties can be declared by decorating either a function, or an attribute, with @thing_property. You can use the decorator either on a function (in which case that function acts as the “getter” just like with Python’s @property decorator).

Events

Events are created by decorating attributes with @thing_event. Functions are not supported at this time.

The decorators in this module mark the Interaction Affordances of a Thing.

LabThings generates a “Thing Description” to allow actions, properties, and events to be used by client code. The descriptions of each “interaction affordance” rely on docstrings and Python type hints to provide a full description of the parameters, so it’s important that you use these effectively.

If you have a complex datatype, it’s recommended to use a pydantic model to describe it - this is often the case for complicated properties or events. For actions, a model is created automatically based on the function’s signature: if you want to add descriptions or validators to individual arguments, you may use pydantic.Field to do this.

Package Contents

Functions

mark_thing_action

Mark a method of a Thing as an Action

thing_action

thing_property

Mark a method of a Thing as a Property

fastapi_endpoint

Add a function to FastAPI as an endpoint

API

labthings_fastapi.decorators.mark_thing_action(func: Callable, **kwargs) labthings_fastapi.descriptors.ActionDescriptor

Mark a method of a Thing as an Action

We replace the function with a Descriptor that’s a subclass of ActionDescriptor

labthings_fastapi.decorators.thing_action(func: Optional[Callable] = None, **kwargs)
labthings_fastapi.decorators.thing_property(func: Callable) labthings_fastapi.descriptors.PropertyDescriptor

Mark a method of a Thing as a Property

We replace the function with a Descriptor that’s a subclass of PropertyDescriptor

TODO: try https://stackoverflow.com/questions/54413434/type-hinting-with-descriptors

labthings_fastapi.decorators.fastapi_endpoint(method: labthings_fastapi.descriptors.HTTPMethod, path: Optional[str] = None, **kwargs)

Add a function to FastAPI as an endpoint