Loading openflexure_microscope/api/utilities.py +7 −2 Original line number Diff line number Diff line import pprint import logging import copy from werkzeug.exceptions import BadRequest class JsonPayload: def __init__(self, request): Loading @@ -8,7 +9,11 @@ class JsonPayload: Object to wrap up simple functionality for parsing a JSON response. """ # Try to load as json try: self.json = request.get_json() #: dict: Dictionary representation of request JSON except BadRequest as e: logging.error(e) self.json = {} if self.json is None: self.json = {} Loading openflexure_microscope/api/v1/blueprints/task.py +2 −0 Original line number Diff line number Diff line Loading @@ -43,6 +43,8 @@ class TaskListAPI(MicroscopeView): :>header Content-Type: application/json """ print(self.microscope.task.state) data = self.microscope.task.state return jsonify(data) Loading openflexure_microscope/plugins/example/api.py +19 −0 Original line number Diff line number Diff line Loading @@ -74,3 +74,22 @@ class LongRunningAPI(MicroscopeViewPlugin): except TaskDeniedException: return abort(409) class SomeExceptionAPI(MicroscopeViewPlugin): """ An example API plugin that uses a long-running but broken plugin method. """ def post(self): """ Method to call when an HTTP POST request is made. """ # Get payload JSON payload = JsonPayload(request) # Attach the long-running method as a microscope task try: task = self.microscope.task.start(self.plugin.some_exception) return jsonify(task.state), 202 except TaskDeniedException: return abort(409) No newline at end of file openflexure_microscope/plugins/example/plugin.py +12 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ import random import time from openflexure_microscope.plugins import MicroscopePlugin from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI, SomeExceptionAPI class Plugin(MicroscopePlugin): Loading @@ -14,6 +14,7 @@ class Plugin(MicroscopePlugin): '/identify': IdentifyAPI, '/hello': HelloWorldAPI, '/long_running': LongRunningAPI, '/some_exception': SomeExceptionAPI, } def identify(self): Loading @@ -32,6 +33,16 @@ class Plugin(MicroscopePlugin): return "Hello world!" def some_exception(self): """ Demonstrate some broken plugin method that would be long running """ with self.microscope.lock: time.sleep(3) result = 15./0 return result def long_running(self, t_run): """ Demonstrate a long-running method that requires microscope hardware Loading openflexure_microscope/task.py +5 −1 Original line number Diff line number Diff line from threading import Thread from functools import wraps import datetime import logging import traceback import time import uuid Loading Loading @@ -111,7 +113,9 @@ class Task: r = f(*args, **kwargs) s = 'success' except Exception as e: r = e logging.error(e) logging.error(traceback.format_exc()) r = str(e) s = 'error' self.state['return'] = r self.state['status'] = s Loading Loading
openflexure_microscope/api/utilities.py +7 −2 Original line number Diff line number Diff line import pprint import logging import copy from werkzeug.exceptions import BadRequest class JsonPayload: def __init__(self, request): Loading @@ -8,7 +9,11 @@ class JsonPayload: Object to wrap up simple functionality for parsing a JSON response. """ # Try to load as json try: self.json = request.get_json() #: dict: Dictionary representation of request JSON except BadRequest as e: logging.error(e) self.json = {} if self.json is None: self.json = {} Loading
openflexure_microscope/api/v1/blueprints/task.py +2 −0 Original line number Diff line number Diff line Loading @@ -43,6 +43,8 @@ class TaskListAPI(MicroscopeView): :>header Content-Type: application/json """ print(self.microscope.task.state) data = self.microscope.task.state return jsonify(data) Loading
openflexure_microscope/plugins/example/api.py +19 −0 Original line number Diff line number Diff line Loading @@ -74,3 +74,22 @@ class LongRunningAPI(MicroscopeViewPlugin): except TaskDeniedException: return abort(409) class SomeExceptionAPI(MicroscopeViewPlugin): """ An example API plugin that uses a long-running but broken plugin method. """ def post(self): """ Method to call when an HTTP POST request is made. """ # Get payload JSON payload = JsonPayload(request) # Attach the long-running method as a microscope task try: task = self.microscope.task.start(self.plugin.some_exception) return jsonify(task.state), 202 except TaskDeniedException: return abort(409) No newline at end of file
openflexure_microscope/plugins/example/plugin.py +12 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ import random import time from openflexure_microscope.plugins import MicroscopePlugin from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI, SomeExceptionAPI class Plugin(MicroscopePlugin): Loading @@ -14,6 +14,7 @@ class Plugin(MicroscopePlugin): '/identify': IdentifyAPI, '/hello': HelloWorldAPI, '/long_running': LongRunningAPI, '/some_exception': SomeExceptionAPI, } def identify(self): Loading @@ -32,6 +33,16 @@ class Plugin(MicroscopePlugin): return "Hello world!" def some_exception(self): """ Demonstrate some broken plugin method that would be long running """ with self.microscope.lock: time.sleep(3) result = 15./0 return result def long_running(self, t_run): """ Demonstrate a long-running method that requires microscope hardware Loading
openflexure_microscope/task.py +5 −1 Original line number Diff line number Diff line from threading import Thread from functools import wraps import datetime import logging import traceback import time import uuid Loading Loading @@ -111,7 +113,9 @@ class Task: r = f(*args, **kwargs) s = 'success' except Exception as e: r = e logging.error(e) logging.error(traceback.format_exc()) r = str(e) s = 'error' self.state['return'] = r self.state['status'] = s Loading