Added simple root and swagger routes
This commit is contained in:
parent
bcfc8e6eeb
commit
6be8b0044a
5 changed files with 59 additions and 10 deletions
|
|
@ -168,8 +168,8 @@ def cleanup():
|
||||||
atexit.register(cleanup)
|
atexit.register(cleanup)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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
|
#from pprint import pprint
|
||||||
#pprint(labthing.spec.to_dict())
|
#pprint(labthing.spec.to_dict())
|
||||||
with open('spec.yaml', 'w') as f:
|
#with open('spec.yaml', 'w') as f:
|
||||||
f.write(labthing.spec.to_yaml())
|
# f.write(labthing.spec.to_yaml())
|
||||||
|
|
@ -8,6 +8,8 @@ from .views.tasks import TaskList, TaskResource
|
||||||
|
|
||||||
from .spec import view2path
|
from .spec import view2path
|
||||||
|
|
||||||
|
from .utilities import description_from_view
|
||||||
|
|
||||||
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||||
from .exceptions import JSONExceptionHandler
|
from .exceptions import JSONExceptionHandler
|
||||||
|
|
||||||
|
|
@ -110,8 +112,13 @@ class LabThing(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _create_base_routes(self):
|
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)
|
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
|
# Add plugin overview
|
||||||
self.add_resource(PluginListResource, "/plugins")
|
self.add_resource(PluginListResource, "/plugins")
|
||||||
self.register_property(PluginListResource)
|
self.register_property(PluginListResource)
|
||||||
|
|
@ -299,4 +306,42 @@ class LabThing(object):
|
||||||
|
|
||||||
return jsonify(td)
|
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())
|
||||||
|
|
@ -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
|
from .schema import Schema, marshmallow, MARSHMALLOW_VERSION_INFO
|
||||||
import collections.abc
|
import collections.abc
|
||||||
|
|
||||||
from webargs import dict2schema as wa_dict2schema
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -12,9 +10,9 @@ def description_from_view(view_class):
|
||||||
for method_key in ["get", "post", "put", "delete"]:
|
for method_key in ["get", "post", "put", "delete"]:
|
||||||
if hasattr(view_class, method_key):
|
if hasattr(view_class, method_key):
|
||||||
methods.append(method_key.upper())
|
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
|
return d
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,9 @@ class PluginSchema(Schema):
|
||||||
|
|
||||||
|
|
||||||
class PluginListResource(Resource):
|
class PluginListResource(Resource):
|
||||||
|
"""
|
||||||
|
List and basic documentation for all enabled plugins
|
||||||
|
"""
|
||||||
@marshal_with(PluginSchema(many=True))
|
@marshal_with(PluginSchema(many=True))
|
||||||
def get(self):
|
def get(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ class TaskSchema(Schema):
|
||||||
|
|
||||||
|
|
||||||
class TaskList(Resource):
|
class TaskList(Resource):
|
||||||
|
"""
|
||||||
|
List and basic documentation for all session tasks
|
||||||
|
"""
|
||||||
@marshal_with(TaskSchema(many=True))
|
@marshal_with(TaskSchema(many=True))
|
||||||
def get(self):
|
def get(self):
|
||||||
return tasks.tasks()
|
return tasks.tasks()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue