Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/labthings_fastapi/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
from labthings_fastapi.logs import add_thing_log_destination
from labthings_fastapi.message_broker import Message
from labthings_fastapi.middleware.url_for import URLFor
from labthings_fastapi.problem_details import ProblemDetails
from labthings_fastapi.problem_details import (
ProblemDetails,
exceptions_to_problem_details,
)
from labthings_fastapi.thing_description import type_to_dataschema
from labthings_fastapi.thing_description._model import (
ActionAffordance,
Expand Down Expand Up @@ -595,8 +598,8 @@
with self._invocations_lock:
try:
invocation: Any = self._invocations[id]
except KeyError as e:
raise HTTPException(

Check warning on line 602 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

601-602 lines are not covered with tests
status_code=404,
detail="No action invocation found with ID {id}",
) from e
Expand All @@ -609,7 +612,7 @@
invocation.output.response
):
# TODO: honour "accept" header
return invocation.output.response()

Check warning on line 615 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

615 line is not covered with tests
try:
return serialise_from_user_code(
model_instance=invocation.output_model_instance,
Expand Down Expand Up @@ -645,8 +648,8 @@
with self._invocations_lock:
try:
invocation: Any = self._invocations[id]
except KeyError as e:
raise HTTPException(

Check warning on line 652 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

651-652 lines are not covered with tests
status_code=404,
detail="No action invocation found with ID {id}",
) from e
Expand Down Expand Up @@ -841,7 +844,7 @@
"""
super().__set_name__(owner, name)
if self.name != self.func.__name__:
raise ValueError(

Check warning on line 847 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

847 line is not covered with tests
f"Action name '{self.name}' does not match function name "
f"'{self.func.__name__}'",
)
Expand Down Expand Up @@ -932,6 +935,7 @@
# at runtime.
# The solution below is to manually add the annotation, before passing
# the function to the decorator.
@exceptions_to_problem_details(logger=thing.logger)
def start_action(
body: Any, # This annotation will be overwritten below.
background_tasks: BackgroundTasks,
Expand All @@ -945,16 +949,12 @@
dependencies=dependencies,
)
background_tasks.add_task(action_manager.expire_invocations)
try:
return serialise_from_user_code(
model_instance=invocation.response(),
description=f"{invocation}",
status_code=201,
code=self.func,
)
except InvalidReturnValueError as e:
thing.logger.error(e)
raise HTTPException(status_code=500, detail=str(e)) from e
return serialise_from_user_code(
model_instance=invocation.response(),
description=f"{invocation}",
status_code=201,
code=self.func,
)

if issubclass(self.input_model, EmptyInput):
annotation = Body(default_factory=StrictEmptyInput)
Expand Down Expand Up @@ -985,14 +985,14 @@
try:
responses[200]["model"] = self.output_model
pass
except AttributeError:
print(f"Failed to generate response model for action {self.name}")

Check warning on line 989 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

988-989 lines are not covered with tests
# Add an additional media type if we may return a file
if hasattr(self.output_model, "media_type"):
responses[200]["content"][self.output_model.media_type] = {}

Check warning on line 992 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

992 line is not covered with tests
# Now we can add the endpoint to the app.
if thing.path is None:
raise NotConnectedToServerError(

Check warning on line 995 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

995 line is not covered with tests
"Can't add the endpoint without thing.path!"
)
app.post(
Expand Down Expand Up @@ -1040,7 +1040,7 @@
"""
path = path or thing.path
if path is None:
raise NotConnectedToServerError("Can't generate forms without a path!")

Check warning on line 1043 in src/labthings_fastapi/actions.py

View workflow job for this annotation

GitHub Actions / coverage

1043 line is not covered with tests
forms = [
Form[ActionOp](href=path + self.name, op=[ActionOp.invokeaction]),
]
Expand Down
Loading