labthings_fastapi.decorators
Mark the Interaction Affordances of a Thing.
See Web of Things Core Concepts for definitions of Interaction Affordance and other terms.
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.
Actions
You can add an Action to a Thing by declaring a method, decorated with :deco:`.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 :deco:`.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 :deco`property` decorator).
Events
Events are created by decorating attributes with :deco:`.thing_event`. Functions are not supported at this time.
Attributes
Valid HTTP verbs to use with |
Functions
|
Determine the return type of a function. |
|
Mark a method of a Thing as an Action. |
Package Contents
- labthings_fastapi.decorators.HTTPMethod
Valid HTTP verbs to use with
fastapi_endpointorEndpointDescriptor.
- labthings_fastapi.decorators.return_type(func: Callable) Type
Determine the return type of a function.
- Parameters:
func – a function to inspect
- Returns:
the return type of the function.
- 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- Parameters:
func – The function to be decorated.
**kwargs – Additional keyword arguments are passed to the constructor of
ActionDescriptor.
- Returns:
An
ActionDescriptorwrapping the method.