Added simple root and swagger routes

This commit is contained in:
jtc42 2020-01-03 00:19:25 +00:00
parent bcfc8e6eeb
commit 6be8b0044a
5 changed files with 59 additions and 10 deletions

View file

@ -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())
#with open('spec.yaml', 'w') as f:
# f.write(labthing.spec.to_yaml())

View file

@ -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())

View file

@ -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

View file

@ -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):
"""

View file

@ -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()