diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 4b028d8b..5ecacda3 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -168,8 +168,8 @@ def cleanup(): atexit.register(cleanup) if __name__ == "__main__": -# app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False) + app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False) #from pprint import pprint #pprint(labthing.spec.to_dict()) - with open('spec.yaml', 'w') as f: - f.write(labthing.spec.to_yaml()) \ No newline at end of file + #with open('spec.yaml', 'w') as f: + # f.write(labthing.spec.to_yaml()) \ No newline at end of file diff --git a/openflexure_microscope/common/flask_labthings/labthing.py b/openflexure_microscope/common/flask_labthings/labthing.py index 208e0313..ea895b4b 100644 --- a/openflexure_microscope/common/flask_labthings/labthing.py +++ b/openflexure_microscope/common/flask_labthings/labthing.py @@ -8,6 +8,8 @@ from .views.tasks import TaskList, TaskResource from .spec import view2path +from .utilities import description_from_view + from openflexure_microscope.common.labthings_core.utilities import get_docstring from .exceptions import JSONExceptionHandler @@ -110,8 +112,13 @@ class LabThing(object): pass def _create_base_routes(self): - # Add thing description to root + # Add root representation + self.app.add_url_rule(self._complete_url("/", ""), "rootrep", self.rootrep) + # Add thing description self.app.add_url_rule(self._complete_url("/td", ""), "td", self.td) + # Add swagger spec + self.app.add_url_rule(self._complete_url("/swagger", ""), "swagger", self.swagger) + # Add plugin overview self.add_resource(PluginListResource, "/plugins") self.register_property(PluginListResource) @@ -299,4 +306,42 @@ class LabThing(object): return jsonify(td) - # TODO: Add a nicer root resource like the old self-documenting system + def rootrep(self): + """ + Root representation + """ + # TODO: Allow custom root representations + + rr = { + "id": url_for("rootrep", _external=True), + "title": self.title, + "description": self.description, + "links": { + "thingDescription": { + "href": url_for("td", _external=True), + "description": get_docstring(self.td), + "methods": ["GET"], + }, + "swagger": { + "href": url_for("swagger", _external=True), + "description": get_docstring(self.swagger), + "methods": ["GET"], + }, + "plugins": { + "href": self.url_for(PluginListResource, _external=True), + **description_from_view(PluginListResource), + }, + "tasks": { + "href": self.url_for(TaskList, _external=True), + **description_from_view(TaskList), + } + } + } + + return jsonify(rr) + + def swagger(self): + """ + OpenAPI v3 documentation + """ + return jsonify(self.spec.to_dict()) \ No newline at end of file diff --git a/openflexure_microscope/common/flask_labthings/utilities.py b/openflexure_microscope/common/flask_labthings/utilities.py index 68c328e8..9fb110cf 100644 --- a/openflexure_microscope/common/flask_labthings/utilities.py +++ b/openflexure_microscope/common/flask_labthings/utilities.py @@ -1,9 +1,7 @@ -from openflexure_microscope.common.labthings_core.utilities import get_docstring +from openflexure_microscope.common.labthings_core.utilities import get_docstring, get_summary from .schema import Schema, marshmallow, MARSHMALLOW_VERSION_INFO import collections.abc -from webargs import dict2schema as wa_dict2schema - import logging @@ -12,9 +10,9 @@ def description_from_view(view_class): for method_key in ["get", "post", "put", "delete"]: if hasattr(view_class, method_key): methods.append(method_key.upper()) - brief_description = get_docstring(view_class).partition("\n")[0].strip() + summary = get_summary(view_class) - d = {"methods": methods, "description": brief_description} + d = {"methods": methods, "description": summary} return d diff --git a/openflexure_microscope/common/flask_labthings/views/plugins.py b/openflexure_microscope/common/flask_labthings/views/plugins.py index 03bd212a..c4e90c8c 100644 --- a/openflexure_microscope/common/flask_labthings/views/plugins.py +++ b/openflexure_microscope/common/flask_labthings/views/plugins.py @@ -45,6 +45,9 @@ class PluginSchema(Schema): class PluginListResource(Resource): + """ + List and basic documentation for all enabled plugins + """ @marshal_with(PluginSchema(many=True)) def get(self): """ diff --git a/openflexure_microscope/common/flask_labthings/views/tasks.py b/openflexure_microscope/common/flask_labthings/views/tasks.py index 6a3821c9..4b1450cf 100644 --- a/openflexure_microscope/common/flask_labthings/views/tasks.py +++ b/openflexure_microscope/common/flask_labthings/views/tasks.py @@ -36,6 +36,9 @@ class TaskSchema(Schema): class TaskList(Resource): + """ + List and basic documentation for all session tasks + """ @marshal_with(TaskSchema(many=True)) def get(self): return tasks.tasks()