From 2691fa52e6a40224e588d732a9a92a12b1cddf3c Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 19 Feb 2019 19:45:26 +0000 Subject: [PATCH] Do database-related work on UI thread (rest_api.py) --- modules/rest_api.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/rest_api.py b/modules/rest_api.py index 25a214e7..b17875cc 100644 --- a/modules/rest_api.py +++ b/modules/rest_api.py @@ -33,6 +33,12 @@ class Handler(http.server.BaseHTTPRequestHandler): self.end_headers() self.wfile.write(data.encode("utf8")) + def _get_settings(self): + key = params.get("key", None) + key_setting = _bot.get_setting("api-key-%s" % key, {}) + minify = _bot.get_setting("rest-api-minify", False): + return [key_setting, minify] + def _handle(self, method): path, endpoint, args = self._path_data() headers = utils.CaseInsensitiveDict(dict(self.headers.items())) @@ -48,7 +54,7 @@ class Handler(http.server.BaseHTTPRequestHandler): hook = hooks[0] authenticated = hook.get_kwarg("authenticated", True) key = params.get("key", None) - key_setting = _bot.get_setting("api-key-%s" % key, {}) + key_setting, minify = _bot.trigger(self._get_settings) permissions = key_setting.get("permissions", []) if key_setting: @@ -70,7 +76,7 @@ class Handler(http.server.BaseHTTPRequestHandler): if not event_response == None: content_type = "application/json" - if _bot.get_setting("rest-api-minify", False): + if minify: response = json.dumps(event_response, sort_keys=True, separators=(",", ":")) else: