Commit b2824251 authored by Joel Collins's avatar Joel Collins
Browse files

Include Swagger UI

parent 5c6708cd
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import time
import atexit
import logging
import os
import pkg_resources

from flask import Flask, jsonify, send_file

@@ -59,8 +60,9 @@ else:
app, labthing = create_app(
    __name__,
    prefix="/api/v2",
    description="Test LabThing-based API for OpenFlexure Microscope",
    title=f"OpenFlexure Microscope {api_microscope.name}",
    description="Test LabThing-based API for OpenFlexure Microscope",
    version=pkg_resources.get_distribution("openflexure_microscope").version,
)

# Use custom JSON encoder
+11 −45
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ from .spec import view2path

from .views.extensions import ExtensionList
from .views.tasks import TaskList, TaskResource
from .views.docs import docs_blueprint, APISpecResource, W3CThingDescriptionResource

from openflexure_microscope.common.labthings_core.utilities import get_docstring

@@ -106,11 +107,12 @@ class LabThing(object):
        # 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
        )
        # self.app.add_url_rule(
        #    self._complete_url("/swagger", ""), "swagger", self.swagger
        # )
        self.app.register_blueprint(docs_blueprint, url_prefix=self.url_prefix)

        # Add extension overview
        self.add_resource(
@@ -270,35 +272,6 @@ class LabThing(object):
        return endpoint in self.endpoints

    ### Description

    def td(self):
        """
        W3C-style Thing Description
        """
        props = {}
        for key, prop in self.properties.items():
            props[key] = {}
            props[key]["title"] = prop.__name__
            props[key]["description"] = get_docstring(prop)
            props[key]["links"] = [{"href": self.url_for(prop, _external=True)}]

        actions = {}
        for key, prop in self.actions.items():
            actions[key] = {}
            actions[key]["title"] = prop.__name__
            actions[key]["description"] = get_docstring(prop)
            actions[key]["links"] = [{"href": self.url_for(prop, _external=True)}]

        td = {
            "id": url_for("td", _external=True),
            "title": self.title,
            "description": self.description,
            "properties": props,
            "actions": actions,
        }

        return jsonify(td)

    def rootrep(self):
        """
        Root representation
@@ -311,14 +284,13 @@ class LabThing(object):
            "description": self.description,
            "links": {
                "thingDescription": {
                    "href": url_for("td", _external=True),
                    "description": get_docstring(self.td),
                    "href": url_for("labthings_docs.w3c_td", _external=True),
                    "description": get_docstring(W3CThingDescriptionResource),
                    "methods": ["GET"],
                },
                "swagger": {
                    "href": url_for("swagger", _external=True),
                    "description": get_docstring(self.swagger),
                    "methods": ["GET"],
                "swaggerUI": {
                    "href": url_for("labthings_docs.swagger_ui", _external=True),
                    **description_from_view(APISpecResource),
                },
                "extensions": {
                    "href": self.url_for(ExtensionList, _external=True),
@@ -332,9 +304,3 @@ class LabThing(object):
        }

        return jsonify(rr)

    def swagger(self):
        """
        OpenAPI v3 documentation
        """
        return jsonify(self.spec.to_dict())
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ def create_app(

    # Create a LabThing
    labthing = LabThing(
        app, prefix=prefix, title=title, description=description, version=version
        app, prefix=prefix, title=title, description=description, version=str(version)
    )

    # Store references to added-in handlers
+79 −0
Original line number Diff line number Diff line
from flask import abort, url_for, jsonify, render_template, Blueprint

from openflexure_microscope.common.labthings_core.utilities import get_docstring

from ...resource import Resource
from ...find import current_labthing

import os


class APISpecResource(Resource):
    """
    OpenAPI v3 documentation
    """

    def get(self):
        """
        OpenAPI v3 documentation
        """
        return jsonify(current_labthing().spec.to_dict())


class SwaggerUIResource(Resource):
    """
    Swagger UI documentation
    """

    def get(self):
        return render_template("swagger-ui.html")


class W3CThingDescriptionResource(Resource):
    """
    W3C-style Thing Description
    """

    def get(self):
        props = {}
        for key, prop in current_labthing().properties.items():
            props[key] = {}
            props[key]["title"] = prop.__name__
            props[key]["description"] = get_docstring(prop)
            props[key]["links"] = [
                {"href": current_labthing().url_for(prop, _external=True)}
            ]

        actions = {}
        for key, prop in current_labthing().actions.items():
            actions[key] = {}
            actions[key]["title"] = prop.__name__
            actions[key]["description"] = get_docstring(prop)
            actions[key]["links"] = [
                {"href": current_labthing().url_for(prop, _external=True)}
            ]

        td = {
            "id": url_for("labthings_docs.w3c_td", _external=True),
            "title": current_labthing().title,
            "description": current_labthing().description,
            "properties": props,
            "actions": actions,
        }

        return jsonify(td)


docs_blueprint = Blueprint(
    "labthings_docs", __name__, static_folder="./static", template_folder="./templates"
)

docs_blueprint.add_url_rule(
    "/swagger", view_func=APISpecResource.as_view("swagger_json")
)
docs_blueprint.add_url_rule(
    "/swagger-ui", view_func=SwaggerUIResource.as_view("swagger_ui")
)
docs_blueprint.add_url_rule(
    "/td", view_func=W3CThingDescriptionResource.as_view("w3c_td")
)
+665 B
Loading image diff...
Loading