From 09bec65b1504316b8cea57bf2b6ac7b6ddc0ab47 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Thu, 4 Jan 2024 01:11:44 +0000 Subject: [PATCH] Add a Thing for API testing This may not be in the codebase forever, but it's a very useful sanity check to have during development. The current implementation is clearly not a performance or security risk. --- src/openflexure_microscope_server/server.py | 2 ++ .../things/test.py | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/openflexure_microscope_server/things/test.py diff --git a/src/openflexure_microscope_server/server.py b/src/openflexure_microscope_server/server.py index 30236581..ae30e963 100644 --- a/src/openflexure_microscope_server/server.py +++ b/src/openflexure_microscope_server/server.py @@ -11,6 +11,7 @@ from .things.system_control import SystemControlThing from .things.settings_manager import SettingsManager from .things.auto_recentre_stage import RecentringThing from .things.smart_scan import SmartScanThing, BackgroundDetectThing +from .things.test import APITestThing from .serve_static_files import add_static_files from .logging import configure_logging, retrieve_log @@ -26,6 +27,7 @@ thing_server.add_thing(SystemControlThing(), "/system_control/") thing_server.add_thing(SettingsManager(), "/settings/") thing_server.add_thing(SmartScanThing(), "/smart_scan/") thing_server.add_thing(BackgroundDetectThing(), "/background_detect/") +thing_server.add_thing(APITestThing(), "/api_test/") try: add_static_files(thing_server.app) except RuntimeError: diff --git a/src/openflexure_microscope_server/things/test.py b/src/openflexure_microscope_server/things/test.py new file mode 100644 index 00000000..c6163681 --- /dev/null +++ b/src/openflexure_microscope_server/things/test.py @@ -0,0 +1,31 @@ +""" +OpenFlexure Microscope API test Thing + +This Thing is intended only for use testing out the API and client(s). +""" +from labthings_fastapi.thing import Thing +from labthings_fastapi.decorators import thing_action, thing_property +from labthings_fastapi.dependencies.invocation import CancelHook, InvocationLogger + + +class APITestThing(Thing): + _counter: int = 0 + + @thing_property + def counter(self) -> int: + return self._counter + + @thing_action + def count_slowly( + self, + cancel: CancelHook, + logger: InvocationLogger, + n=100, + dt=0.1, + ) -> None: + for i in range(n): + self._counter = i + print(f"counted to {i}") + logger.info(f"Counted to {i}") + cancel.sleep(dt) + self._counter = n