labthings_fastapi.descriptors.endpoint
Add a FastAPI endpoint without making it an action.
The EndpointDescriptor wraps a function and marks it to be added to the
HTTP API at the same time as the properties and actions of the host Thing.
This is intended to allow flexibility to implement endpoints that cannot be
described in a Thing Description as actions or properties.
It may use any fastapi responses or arguments, as it passes keyword
arguments through to the relevant fastapi decorator.
This will most usually be applied as a decorator with arguments, available as :deco:`.fastapi_endpoint`. See the documentation for that function for more detail.
Attributes
Valid HTTP verbs to use with |
Classes
A descriptor to allow Things to easily add other endpoints. |
Module Contents
- labthings_fastapi.descriptors.endpoint.HTTPMethod
Valid HTTP verbs to use with
fastapi_endpointorEndpointDescriptor.
- class labthings_fastapi.descriptors.endpoint.EndpointDescriptor(func: Callable, http_method: HTTPMethod = 'get', path: str | None = None, **kwargs: Mapping)
A descriptor to allow Things to easily add other endpoints.
Initialise an EndpointDescriptor.
See
fastapi_endpoint, which is the usual way of instantiating this class.- Parameters:
func – is the method (defined on a
Thing) wrapped by this descriptor.http_method – the HTTP verb we are responding to. This selects the FastAPI decorator:
"get"corresponds to@app.get.path – the URL, relative to the host
Thing, for the endpoint.**kwargs – additional keyword arguments are passed to the FastAPI decorator, allowing you to specify responses, OpenAPI parameters, etc.
- func
- http_method = 'get'
- _path = None
- kwargs
- __get__(obj: Literal[None], type=None) typing_extensions.Self
- __get__(obj: labthings_fastapi.thing.Thing, type=None) Callable
Bind the method to the host
Thingand return it.When called on a
Thing, this descriptor returns the wrapped function, with theThingbound as its first argument. This is the usual behaviour for Python methods.If
objis None, the descriptor is returned, so we can get the descriptor conveniently as an attribute of the class.
- property name
The name of the wrapped function.
- property path
The path of the endpoint (relative to the Thing).
- property title
A human-readable title.
- property description
A description of the endpoint.
- add_to_fastapi(app: fastapi.FastAPI, thing: labthings_fastapi.thing.Thing)
Add an endpoint for this function to a FastAPI app.
We will add an endpoint to the app, bound to a particular
Thing. The URL will be prefixed with theThingpath, i.e. the specified URL (which defaults to the name of this descriptor) is relative to the hostThing.- Parameters:
app – the
fastapi.FastAPIapplication we are adding to.thing – the
Thingwe’re bound to.