labthings_fastapi.dependencies.thing_server
Retrieve the ThingServer object.
This module provides a function that will retrieve the ThingServer
based on the fastapi.Request object. It may be used as a dependency with
Annotated[ThingServer, Depends(thing_server_from_request)].
See Dependencies for more information on the dependency mechanism,
and Using Things from other Things for more on how Things interact.
Note
This module does not provide a ready-made annotated type to use as a
dependency. Doing so would mean this module has a hard dependency on
ThingServer and cause circular references. See above for the
annotated type, which you may define in any code that needs it.
Note
The rationale for this function is that we want to make sure Thing
instances only access the server associated with the current request.
This means that we use the fastapi.Request to look up the
fastapi.FastAPI app, and then use the app to look up the ThingServer.
As each Thing is connected to exactly one ThingServer, this may
become unnecessary in the future as the server could be exposed as a
property of the Thing.
Attributes
Functions
|
Find the ThingServer associated with an app. |
Retrieve the |
Module Contents
- labthings_fastapi.dependencies.thing_server._thing_servers: weakref.WeakSet[labthings_fastapi.server.ThingServer]
- labthings_fastapi.dependencies.thing_server.find_thing_server(app: fastapi.FastAPI) labthings_fastapi.server.ThingServer
Find the ThingServer associated with an app.
This function will return the
ThingServerobject that contains a particularfastapi.FastAPIapp. The app is available as part of thefastapi.Requestobject, so this makes it possible to get theThingServerin dependency functions.This function will not work as a dependency, but
thing_server_from_requestwill.- Parameters:
app – The
fastapi.FastAPIapplication that implements theThingServer, i.e. this isthing_server.app.- Returns:
the
ThingServerthat owns theapp.- Raises:
RuntimeError – if there is no
ThingServerassociated with the current FastAPI application. This should only happen if this function is called on afastapi.FastAPIinstance that was not created by aThingServer.
- labthings_fastapi.dependencies.thing_server.thing_server_from_request(request: fastapi.Request) labthings_fastapi.server.ThingServer
Retrieve the
ThingServerfrom a request.This is for use as a FastAPI dependency, so the thing server is retrieved from the request object. See
find_thing_server.It may be used as a dependency with:
ServerDep = Annotated[ThingServer, Depends(thing_server_from_request)]
This is not provided as a ready-made annotated type because it would introduce a hard dependency on the
servermodule and cause circular references.- Parameters:
request – is supplied automatically by FastAPI when used as a dependency.
- Returns:
the
ThingServerhandling the current request.