Renamed Resource to View, and allow custom root links

This commit is contained in:
Joel Collins 2020-01-14 11:41:56 +00:00
parent 234ebc1cbb
commit a329b9197b
18 changed files with 90 additions and 67 deletions

View file

@ -10,8 +10,8 @@ from .spec import rule2path, get_spec
from .decorators import tag
from .views.extensions import ExtensionList
from .views.tasks import TaskList, TaskResource
from .views.docs import docs_blueprint, SwaggerUIResource, W3CThingDescriptionResource
from .views.tasks import TaskList, TaskView
from .views.docs import docs_blueprint, SwaggerUIView, W3CThingDescriptionView
from openflexure_microscope.common.labthings_core.utilities import get_docstring
@ -37,6 +37,8 @@ class LabThing(object):
self.properties = {}
self.actions = {}
self.custom_root_links = {}
self.endpoints = set()
self.url_prefix = prefix
@ -114,7 +116,7 @@ class LabThing(object):
self.add_view(ExtensionList, "/extensions", endpoint=EXTENSION_LIST_ENDPOINT)
# Add task routes
self.add_view(TaskList, "/tasks", endpoint=TASK_LIST_ENDPOINT)
self.add_view(TaskResource, "/tasks/<id>", endpoint=TASK_ENDPOINT)
self.add_view(TaskView, "/tasks/<id>", endpoint=TASK_ENDPOINT)
### Device stuff
@ -234,15 +236,18 @@ class LabThing(object):
### Utilities
def url_for(self, resource, **values):
def url_for(self, view, **values):
"""Generates a URL to the given resource.
Works like :func:`flask.url_for`."""
endpoint = resource.endpoint
endpoint = view.endpoint
return url_for(endpoint, **values)
def owns_endpoint(self, endpoint):
return endpoint in self.endpoints
def add_root_link(self, view, title, kwargs={}):
self.custom_root_links[title] = (view, kwargs)
### Description
def rootrep(self):
"""
@ -257,11 +262,11 @@ class LabThing(object):
"links": {
"thingDescription": {
"href": url_for("labthings_docs.w3c_td", _external=True),
"description": get_docstring(W3CThingDescriptionResource),
"description": get_docstring(W3CThingDescriptionView),
},
"swaggerUI": {
"href": url_for("labthings_docs.swagger_ui", _external=True),
**description_from_view(SwaggerUIResource),
**description_from_view(SwaggerUIView),
},
"extensions": {
"href": self.url_for(ExtensionList, _external=True),
@ -274,4 +279,10 @@ class LabThing(object):
},
}
for title, (view, kwargs) in self.custom_root_links.items():
rr["links"][title] = {
"href": self.url_for(view, **kwargs, _external=True),
**description_from_view(view),
}
return jsonify(rr)

View file

@ -1,4 +1,4 @@
from ..resource import Resource
from ..view import View
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
@ -29,7 +29,7 @@ def get_spec(obj):
return obj.__apispec__
def rule2path(rule: Rule, view: Resource, spec: APISpec):
def rule2path(rule: Rule, view: View, spec: APISpec):
params = {
"path": rule_to_path(rule),
"operations": view2operations(view, spec),
@ -52,7 +52,7 @@ def rule2path(rule: Rule, view: Resource, spec: APISpec):
return params
def view2operations(view: Resource, spec: APISpec):
def view2operations(view: View, spec: APISpec):
# Operations inherit tags from parent
inherited_tags = []
if hasattr(view, "__apispec__"):
@ -60,7 +60,7 @@ def view2operations(view: Resource, spec: APISpec):
# Build dictionary of operations (HTTP methods)
ops = {}
for method in Resource.methods:
for method in View.methods:
if hasattr(view, method):
ops[method] = {}

View file

@ -2,15 +2,23 @@ from openflexure_microscope.common.labthings_core.utilities import (
get_docstring,
get_summary,
)
from .view import View
from flask import current_app
def description_from_view(view_class):
summary = get_summary(view_class)
methods = []
for method_key in ["get", "post", "put", "delete"]:
for method_key in View.methods:
if hasattr(view_class, method_key):
methods.append(method_key.upper())
summary = get_summary(view_class)
# If no class summary was given, try using summaries from method functions
if not summary:
summary = get_summary(getattr(view_class, method_key))
d = {"methods": methods, "description": summary}

View file

@ -1,7 +1,7 @@
from flask.views import MethodView
class Resource(MethodView):
class View(MethodView):
"""
A LabThing Resource class should make use of functions get(), put(), post(), and delete()
corresponding to HTTP methods.
@ -20,7 +20,7 @@ class Resource(MethodView):
if hasattr(self, "__apispec__"):
docs.update(self.__apispec__)
for meth in BaseResource.methods:
for meth in View.methods:
if hasattr(self, meth) and hasattr(getattr(self, meth), "__apispec__"):
docs["operations"][meth] = {}
docs["operations"][meth] = getattr(self, meth).__apispec__

View file

@ -2,14 +2,14 @@ from flask import abort, url_for, jsonify, render_template, Blueprint, current_a
from openflexure_microscope.common.labthings_core.utilities import get_docstring
from ...resource import Resource
from ...view import View
from ...find import current_labthing
from ...spec import rule_to_path, rule_to_params
import os
class APISpecResource(Resource):
class APISpecView(View):
"""
OpenAPI v3 documentation
"""
@ -21,7 +21,7 @@ class APISpecResource(Resource):
return jsonify(current_labthing().spec.to_dict())
class SwaggerUIResource(Resource):
class SwaggerUIView(View):
"""
Swagger UI documentation
"""
@ -30,7 +30,7 @@ class SwaggerUIResource(Resource):
return render_template("swagger-ui.html")
class W3CThingDescriptionResource(Resource):
class W3CThingDescriptionView(View):
"""
W3C-style Thing Description
"""
@ -103,11 +103,11 @@ docs_blueprint = Blueprint(
)
docs_blueprint.add_url_rule(
"/swagger", view_func=APISpecResource.as_view("swagger_json")
"/swagger", view_func=APISpecView.as_view("swagger_json")
)
docs_blueprint.add_url_rule(
"/swagger-ui", view_func=SwaggerUIResource.as_view("swagger_ui")
"/swagger-ui", view_func=SwaggerUIView.as_view("swagger_ui")
)
docs_blueprint.add_url_rule(
"/td", view_func=W3CThingDescriptionResource.as_view("w3c_td")
"/td", view_func=W3CThingDescriptionView.as_view("w3c_td")
)

View file

@ -1,14 +1,14 @@
"""
Top-level representation of attached and enabled Extensions
"""
from ..resource import Resource
from ..view import View
from ..find import registered_extensions
from ..schema import ExtensionSchema
from ..decorators import marshal_with, ThingProperty
@ThingProperty
class ExtensionList(Resource):
class ExtensionList(View):
"""
List and basic documentation for all enabled Extensions
"""

View file

@ -1,7 +1,7 @@
from flask import abort, url_for
from ..decorators import marshal_with, ThingProperty, Tag
from ..resource import Resource
from ..view import View
from ..schema import TaskSchema
from openflexure_microscope.common.labthings_core import tasks
@ -9,7 +9,7 @@ from openflexure_microscope.common.labthings_core import tasks
@ThingProperty
@Tag("tasks")
class TaskList(Resource):
class TaskList(View):
@marshal_with(TaskSchema(many=True))
def get(self):
"""
@ -19,7 +19,7 @@ class TaskList(Resource):
@Tag(["properties", "tasks"])
class TaskResource(Resource):
class TaskView(View):
@marshal_with(TaskSchema())
def get(self, id):
"""