labthings_fastapi.endpoints
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 @endpoint. See the documentation for that function for
more detail.
Attributes
Valid HTTP verbs to use with |
Functions
|
Mark a function as a FastAPI endpoint without making it an action. |
Module Contents
- labthings_fastapi.endpoints.HTTPMethod
Valid HTTP verbs to use with
endpointorEndpointDescriptor.
- labthings_fastapi.endpoints.endpoint(method: HTTPMethod, path: str | None = None, **kwargs: Any) Callable[[Callable], EndpointDescriptor]
Mark a function as a FastAPI endpoint without making it an action.
This decorator will cause a method of a
Thingto be directly added to the HTTP API, bypassing the machinery underlying Action and Property affordances. Such endpoints will not be documented in the Thing Description but may be used as the target of links. For example, this could allow a file to be downloaded from theThingat a known URL, or serve a video stream that wouldn’t be supported as aBlob.The majority of
Thingimplementations won’t need this decorator, but it is here to enable flexibility when it’s needed.This decorator always takes arguments; in particular,
methodis required. It should be used as:class DownloadThing(Thing): @endpoint("get") def plain_text_response(self) -> str: return "example string"
This decorator is intended to work very similarly to the
fastapidecorators@app.get,@app.post, etc., with two changes:- The path is relative to the host
Thingand will default to the name of the method.
- The path is relative to the host
- The method will be called with the host
Thingas its first argument, i.e. it will be bound to the class as usua.
- The method will be called with the host
- Parameters:
method – The HTTP verb this endpoint responds to.
path – The path, relative to the host
Thingbase URL.**kwargs – Additional keyword arguments are passed to the
fastapi.FastAPI.getdecorator ifmethodisget, or to the equivalent decorator for other HTTP verbs.
- Returns:
When used as intended, the result is an
EndpointDescriptor.