Added proper root representation

This commit is contained in:
Joel Collins 2019-11-19 15:45:54 +00:00
parent dac50f1f3a
commit edecc01ca1
7 changed files with 122 additions and 13 deletions

View file

@ -144,6 +144,9 @@ app.register_blueprint(task_blueprint, url_prefix=uri("/task", "v1"))
### V2
# Root routes
v2_root_blueprint = v2.blueprints.root.construct_blueprint(api_microscope)
app.register_blueprint(v2_root_blueprint, url_prefix=uri("/", "v2"))
v2_stream_blueprint = v2.blueprints.stream.construct_blueprint(api_microscope)
app.register_blueprint(v2_stream_blueprint, url_prefix=uri("/", "v2"))

View file

@ -1 +1 @@
from . import captures, settings, status, tasks, stream, plugins, actions
from . import root, captures, settings, status, tasks, stream, plugins, actions

View file

@ -1,6 +1,8 @@
from flask import Blueprint, url_for, jsonify
from sys import platform
from openflexure_microscope.api.views import MicroscopeView
from . import camera, stage, system
_actions = {
@ -64,11 +66,14 @@ def actions_representation():
return actions
class ActionsAPI(MicroscopeView):
def get(self):
return jsonify(actions_representation())
def construct_blueprint(microscope_obj):
global _actions
blueprint = Blueprint("actions_blueprint", __name__)
blueprint = Blueprint("v2_actions_blueprint", __name__)
# For each enabled action route defined in our dictionary above
for name, action in enabled_actions().items():
@ -78,8 +83,8 @@ def construct_blueprint(microscope_obj):
view_func=action["view_class"].as_view(name, microscope=microscope_obj),
)
@blueprint.route("/")
def representation():
return jsonify(actions_representation())
blueprint.add_url_rule(
"/", view_func=ActionsAPI.as_view("actions", microscope=microscope_obj)
)
return blueprint

View file

@ -378,7 +378,7 @@ class TagsAPI(MicroscopeView):
def construct_blueprint(microscope_obj):
blueprint = Blueprint("captures_blueprint", __name__)
blueprint = Blueprint("v2_captures_blueprint", __name__)
# Tag routes
blueprint.add_url_rule(
@ -405,6 +405,6 @@ def construct_blueprint(microscope_obj):
)
blueprint.add_url_rule(
"/", view_func=ListAPI.as_view("capture_list", microscope=microscope_obj)
"/", view_func=ListAPI.as_view("captures", microscope=microscope_obj)
)
return blueprint

View file

@ -1,7 +1,7 @@
from openflexure_microscope.plugins import PluginLoader, MicroscopePlugin
from openflexure_microscope.api.views import MicroscopeViewPlugin
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, url_for
from openflexure_microscope.api.views import MicroscopeView
import copy
@ -27,6 +27,11 @@ def plugins_representation(plugin_loader_object: PluginLoader):
"routes": plugin["routes"],
"form": plugin["form"],
}
for route in d["routes"]:
route_id = route["id"]
uri = url_for(f"v2_plugins_blueprint.{route_id}")
route["links"]["self"] = uri
plugins.append(d)
return plugins
@ -51,7 +56,7 @@ class PluginFormAPI(MicroscopeView):
def construct_blueprint(microscope_obj):
blueprint = Blueprint("plugins_blueprint", __name__)
blueprint = Blueprint("v2_plugins_blueprint", __name__)
# Create a base route to return plugin API forms, if any exist
blueprint.add_url_rule(
@ -110,7 +115,12 @@ def construct_blueprint(microscope_obj):
)
# Add route to the plugin representation dictionary
plugin_representation["routes"].append(full_view_route)
route_representation = {
"id": plugin_route_id,
"route": full_view_route,
"links": {},
}
plugin_representation["routes"].append(route_representation)
else:
warnings.warn(

View file

@ -0,0 +1,44 @@
from flask import Blueprint, jsonify, url_for
from openflexure_microscope import Microscope
from openflexure_microscope.api.views import MicroscopeView
def root_representation(microscope_obj: Microscope):
d = {
"settings": {
"description": "Writeable settings for the microscope, and attached hardware",
"links": {"self": url_for("v2_settings_blueprint.settings")}
},
"status": {
"description": "Read-only status of the microscope, and attached hardware info",
"links": {"self": url_for("v2_status_blueprint.status")}
},
"plugins": {
"description": "Top-level representation of attached and enabled plugins",
"links": {"self": url_for("v2_plugins_blueprint.plugins")}
},
"captures": {
"description": "Top-level representation of all acquired captures",
"links": {"self": url_for("v2_captures_blueprint.captures")}
},
"actions": {
"description": "Top-level representation of enabled actions",
"links": {"self": url_for("v2_actions_blueprint.actions")}
},
}
return d
class RootAPI(MicroscopeView):
def get(self):
return jsonify(root_representation(self.microscope))
def construct_blueprint(microscope_obj):
blueprint = Blueprint("root_blueprint", __name__)
blueprint.add_url_rule(
"/", view_func=RootAPI.as_view("root_repr", microscope=microscope_obj)
)
return blueprint