Mercurial > libervia-backend
diff sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3977:6fa4ca0c047e
component AP gateway: HTML redirection:
when a request is done on AP endpoint and `Accept` header is not set to
`application/json`, the request can now be redirected to a configurable URL.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 11 Nov 2022 13:51:20 +0100 |
parents | 0aa7023dcd08 |
children | 996e0f84935e |
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/__init__.py Thu Nov 10 15:16:43 2022 +0100 +++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py Fri Nov 11 13:51:20 2022 +0100 @@ -20,7 +20,6 @@ import calendar import hashlib import json -from os import access from pathlib import Path from pprint import pformat import re @@ -33,7 +32,6 @@ Optional, Set, Tuple, - Type, Union, overload, ) @@ -266,6 +264,27 @@ self.host.memory.getConfig(CONF_SECTION, "auto_mentions", C.BOOL_TRUE) ) + html_redirect: Dict[str, Union[str, dict]] = self.host.memory.getConfig( + CONF_SECTION, 'html_redirect_dict', {} + ) + self.html_redirect: Dict[str, List[dict]] = {} + for url_type, target in html_redirect.items(): + if isinstance(target, str): + target = {"url": target} + elif not isinstance(target, dict): + raise exceptions.ConfigError( + f"html_redirect target must be a URL or a dict, not {target!r}" + ) + filters = target.setdefault("filters", {}) + if "url" not in target: + log.warning(f"invalid HTML redirection, missing target URL: {target}") + continue + # a slash in the url_type is a syntactic shortcut to have a node filter + if "/" in url_type: + url_type, node_filter = url_type.split("/", 1) + filters["node"] = node_filter + self.html_redirect.setdefault(url_type, []).append(target) + # HTTP server launch self.server = HTTPServer(self) if connection_type == 'http': @@ -361,7 +380,7 @@ if resp.code == 404: raise exceptions.NotFound(f"Can't find resource at {url}") else: - msg = f"HTTP error {resp.code}: {text}" + msg = f"HTTP error {resp.code} (url: {url}): {text}" raise exceptions.ExternalRequestError(msg) try: return await treq.json_content(resp)