From 8157fa6d58f62c51496af9d46834046d9abca6b7 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 27 Jun 2021 21:08:48 +0100 Subject: [PATCH] Add YAML export from APISpec --- docs/Makefile | 5 +++++ openflexure_microscope/api/app.py | 2 ++ .../api/v2/views/__init__.py | 1 + openflexure_microscope/api/v2/views/docs.py | 21 +++++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 openflexure_microscope/api/v2/views/docs.py diff --git a/docs/Makefile b/docs/Makefile index 5d25534e..4d2bdb40 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -13,6 +13,11 @@ help: .PHONY: help Makefile +openapi: $(BUILDDIR)/swagger.yaml + +$(BUILDDIR)/swagger.yaml: Makefile + curl http://localhost:5000/api/v2/docs/swagger.yaml > $@ + # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index d288bf65..9bc7f9d9 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -153,6 +153,8 @@ labthing.add_view( views.NestedConfigurationProperty, "/instrument/configuration/" ) labthing.add_root_link(views.ConfigurationProperty, "instrumentConfiguration") +labthing.add_view(views.APISpecJSONView, "/docs/swagger.json") +labthing.add_view(views.APISpecYAMLView, "/docs/swagger.yaml") # Attach stage resources labthing.add_view(views.StageTypeProperty, "/instrument/stage/type") diff --git a/openflexure_microscope/api/v2/views/__init__.py b/openflexure_microscope/api/v2/views/__init__.py index 5771e204..c5f59242 100644 --- a/openflexure_microscope/api/v2/views/__init__.py +++ b/openflexure_microscope/api/v2/views/__init__.py @@ -4,3 +4,4 @@ from .captures import * from .instrument import * from .stage import * from .streams import * +from .docs import * \ No newline at end of file diff --git a/openflexure_microscope/api/v2/views/docs.py b/openflexure_microscope/api/v2/views/docs.py new file mode 100644 index 00000000..999668ac --- /dev/null +++ b/openflexure_microscope/api/v2/views/docs.py @@ -0,0 +1,21 @@ +from labthings.views import View +from labthings import current_labthing +from flask import Response + +class APISpecJSONView(View): + """OpenAPI v3 documentation + + A JSON document containing an API description in OpenAPI format + """ + + def get(self): + return current_labthing().spec.to_dict() + +class APISpecYAMLView(View): + """OpenAPI v3 documentation + + A YAML document containing an API description in OpenAPI format + """ + + def get(self): + return Response(current_labthing().spec.to_yaml(), mimetype='text/yaml') \ No newline at end of file