Skip to content

Commit 62729ea

Browse files
author
Joel Collins
committed
Added taskification to action builder
1 parent 85d7f25 commit 62729ea

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

examples/builder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def cleanup():
4343
"/dictionary",
4444
)
4545
labthing.add_view(
46-
action_from(my_component.average_data, description="Take an averaged measurement"),
46+
action_from(
47+
my_component.average_data, description="Take an averaged measurement", task=True
48+
),
4749
"/average",
4850
)
4951

labthings/server/view/builder.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from labthings.core.tasks import taskify
12
from labthings.server.types import (
23
value_to_field,
34
data_dict_to_schema,
@@ -7,6 +8,7 @@
78
ThingProperty,
89
PropertySchema,
910
ThingAction,
11+
marshal_task,
1012
use_args,
1113
Doc,
1214
)
@@ -72,12 +74,19 @@ def _put(self, args):
7274
return generated_class
7375

7476

75-
def action_from(function, name: str = None, description=None):
77+
def action_from(function, name: str = None, description=None, task=False):
7678

7779
# Create a class name
7880
if not name:
7981
name = f"Action_{function.__name__}"
8082

83+
# Create schema
84+
action_schema = function_signature_to_schema(function)
85+
86+
# Handle taskification
87+
if task:
88+
function = taskify(function)
89+
8190
# Create inner functions
8291
def _post(self, args):
8392
return function(**args)
@@ -86,9 +95,12 @@ def _post(self, args):
8695
generated_class = type(name, (View, object), {"post": _post})
8796

8897
# Add decorators for arguments etc
89-
action_schema = function_signature_to_schema(function)
98+
9099
generated_class.post = use_args(action_schema)(generated_class.post)
91100

101+
if task:
102+
generated_class.post = marshal_task(generated_class.post)
103+
92104
generated_class = ThingAction(generated_class)
93105

94106
if description:

0 commit comments

Comments
 (0)