From cf2ce1e88bc6755ae6d398112f1fddbeb7b8abcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 10 Mar 2021 21:54:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Default=20to=20JSON=20api?= =?UTF-8?q?=20errors=20for=20blueprints=20as=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Behaviour can be overwritten via BluePrintPlugin .get_blueprint_api_prefixes. --- src/octoprint/plugin/types.py | 15 ++++++++++++++- src/octoprint/server/__init__.py | 13 +++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index b3db80d55..7a50727d6 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1477,9 +1477,22 @@ class BlueprintPlugin(OctoPrintPlugin, RestartNeedingPlugin): If you want your blueprint's endpoints to have specific permissions, return ``False`` for this and do your permissions checks explicitly. """ - return True + # noinspection PyMethodMayBeStatic + def get_blueprint_api_prefixes(self): + """ + Return all prefixes of your endpoint that are an API that should be containing JSON only. + + Anything that matches this will generate JSON error messages in case of flask.abort + calls, instead of the default HTML ones. + + Defaults to all endpoints under the blueprint. Limit this further as needed. E.g., + if you only want your endpoints /foo, /foo/1 and /bar to be declared as API, + return ``["/foo", "/bar"]``. A match will be determined via startswith. + """ + return [""] + class SettingsPlugin(OctoPrintPlugin): """ diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 28e06a9e7..2d36f1f41 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1741,7 +1741,12 @@ class Server(object): json_errors = ["/api"] # also register any blueprints defined in BlueprintPlugins - blueprints.update(self._prepare_blueprint_plugins()) + ( + blueprint_from_plugins, + api_prefixes_from_plugins, + ) = self._prepare_blueprint_plugins() + blueprints.update(blueprint_from_plugins) + json_errors += api_prefixes_from_plugins # and register a blueprint for serving the static files of asset plugins which are not blueprint plugins themselves blueprints.update(self._prepare_asset_plugins()) @@ -1762,6 +1767,7 @@ class Server(object): def _prepare_blueprint_plugins(self): blueprints = OrderedDict() + api_prefixes = [] blueprint_plugins = octoprint.plugin.plugin_manager().get_implementations( octoprint.plugin.BlueprintPlugin @@ -1769,6 +1775,9 @@ class Server(object): for plugin in blueprint_plugins: try: blueprint, prefix = self._prepare_blueprint_plugin(plugin) + api_prefixes += map( + lambda x: prefix + x, plugin.get_blueprint_api_prefixes() + ) blueprints[prefix] = blueprint except Exception: self._logger.exception( @@ -1778,7 +1787,7 @@ class Server(object): ) continue - return blueprints + return blueprints, api_prefixes def _prepare_asset_plugins(self): blueprints = OrderedDict()