labthings_fastapi.client

A first pass at a client library for LabThings-FastAPI

This will become its own package if it’s any good. The goal is to see if we can make a client library that produces introspectable Python objects from a Thing Description.

Submodules

Package Contents

Classes

ThingClient

A client for a LabThings-FastAPI Thing

PropertyClientDescriptor

Functions

get_link

Retrieve a link from an object’s links list, by its rel attribute

get_link_href

Retrieve the href from an object’s links list, by its rel attribute

task_href

Extract the endpoint address from a task dictionary

poll_task

Poll a task until it finishes, and return the return value

property_descriptor

Create a correctly-typed descriptor that gets and/or sets a property

add_action

Add an action to a ThingClient subclass

add_property

Add a property to a ThingClient subclass

Data

ACTION_RUNNING_KEYWORDS

API

labthings_fastapi.client.ACTION_RUNNING_KEYWORDS

[‘idle’, ‘pending’, ‘running’]

Retrieve a link from an object’s links list, by its rel attribute

Retrieve the href from an object’s links list, by its rel attribute

labthings_fastapi.client.task_href(t)

Extract the endpoint address from a task dictionary

labthings_fastapi.client.poll_task(client, task, interval=0.5, first_interval=0.05)

Poll a task until it finishes, and return the return value

class labthings_fastapi.client.ThingClient(base_url: str, client: Optional[httpx.Client] = None)

A client for a LabThings-FastAPI Thing

NB ThingClient must be subclassed to add actions/properties, so this class will be minimally useful on its own.

__init__(base_url: str, client: Optional[httpx.Client] = None)
get_property(path: str) Any
set_property(path: str, value: Any)
invoke_action(path: str, **kwargs)

Invoke an action on the Thing

Follow a link in a response object, by its rel attribute

classmethod from_url(thing_url: str, client: Optional[httpx.Client] = None, **kwargs) typing_extensions.Self

Create a ThingClient from a URL

This will dynamically create a subclass with properties and actions, and return an instance of that subclass pointing at the Thing URL.

Additional kwargs will be passed to the subclass constructor, in particular you may pass a client object (useful for testing).

classmethod subclass_from_td(thing_description: dict) type[typing_extensions.Self]

Create a ThingClient subclass from a Thing Description

class labthings_fastapi.client.PropertyClientDescriptor
labthings_fastapi.client.property_descriptor(property_name: str, model: Union[type, pydantic.BaseModel], description: Optional[str] = None, readable: bool = True, writeable: bool = True, property_path: Optional[str] = None) labthings_fastapi.client.PropertyClientDescriptor

Create a correctly-typed descriptor that gets and/or sets a property

labthings_fastapi.client.add_action(cls: type[labthings_fastapi.client.ThingClient], action_name: str, action: dict)

Add an action to a ThingClient subclass

labthings_fastapi.client.add_property(cls: type[labthings_fastapi.client.ThingClient], property_name: str, property: dict)

Add a property to a ThingClient subclass