move more REST API code on to the main thread
This commit is contained in:
parent
8653068650
commit
2fc75c6af7
1 changed files with 19 additions and 17 deletions
|
@ -70,22 +70,17 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(response.get_data())
|
self.wfile.write(response.get_data())
|
||||||
|
|
||||||
def _get_settings(self, key):
|
def _key_settings(self, key):
|
||||||
key_setting = _bot.get_setting("api-key-%s" % key, {})
|
return _bot.get_setting("api-key-%s" % key, {})
|
||||||
minify = _bot.get_setting("rest-api-minify", False)
|
def _minify_setting(self):
|
||||||
return [key_setting, minify]
|
return _bot.get_setting("rest-api-minify", False)
|
||||||
|
|
||||||
def _handle(self, method):
|
|
||||||
path, endpoint, args = self._path_data()
|
|
||||||
|
|
||||||
_log.debug("[HTTP] starting _handle for %s from %s:%d: %s",
|
|
||||||
[method, self.client_address[0], self.client_address[1], path])
|
|
||||||
|
|
||||||
|
def _handle(self, method, path, endpoint, args):
|
||||||
headers = utils.CaseInsensitiveDict(dict(self.headers.items()))
|
headers = utils.CaseInsensitiveDict(dict(self.headers.items()))
|
||||||
params = self._url_params()
|
params = self._url_params()
|
||||||
data = self._body()
|
data = self._body()
|
||||||
|
|
||||||
response = Response()
|
response = Response(compact=self._minify_setting())
|
||||||
response.code = 404
|
response.code = 404
|
||||||
|
|
||||||
hooks = _events.on("api").on(method).on(endpoint).get_hooks()
|
hooks = _events.on("api").on(method).on(endpoint).get_hooks()
|
||||||
|
@ -94,7 +89,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
hook = hooks[0]
|
hook = hooks[0]
|
||||||
authenticated = hook.get_kwarg("authenticated", True)
|
authenticated = hook.get_kwarg("authenticated", True)
|
||||||
key = params.get("key", None)
|
key = params.get("key", None)
|
||||||
key_setting, minify = _bot.trigger(lambda: self._get_settings(key))
|
key_setting = self._key_settings(key)
|
||||||
permissions = key_setting.get("permissions", [])
|
permissions = key_setting.get("permissions", [])
|
||||||
|
|
||||||
if key_setting:
|
if key_setting:
|
||||||
|
@ -105,11 +100,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
if path.startswith("/api/"):
|
if path.startswith("/api/"):
|
||||||
event_response = None
|
event_response = None
|
||||||
try:
|
try:
|
||||||
event_response = _bot.trigger(lambda:
|
event_response = _events.on("api").on(method).on(
|
||||||
_events.on("api").on(method).on(
|
|
||||||
endpoint).call_for_result_unsafe(params=params,
|
endpoint).call_for_result_unsafe(params=params,
|
||||||
path=args, data=data, headers=headers,
|
path=args, data=data, headers=headers,
|
||||||
response=response))
|
response=response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_log.error("failed to call API endpoint \"%s\"",
|
_log.error("failed to call API endpoint \"%s\"",
|
||||||
[path], exc_info=True)
|
[path], exc_info=True)
|
||||||
|
@ -120,7 +114,15 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
response.content_type = "application/json"
|
response.content_type = "application/json"
|
||||||
else:
|
else:
|
||||||
response.code = 401
|
response.code = 401
|
||||||
|
return response
|
||||||
|
|
||||||
|
def _handle_wrap(self, method):
|
||||||
|
path, endpoint, args = self._path_data()
|
||||||
|
_log.debug("[HTTP] starting _handle for %s from %s:%d: %s",
|
||||||
|
[method, self.client_address[0], self.client_address[1], path])
|
||||||
|
|
||||||
|
response = _bot.trigger(lambda: self._handle(method, path, endpoint,
|
||||||
|
args))
|
||||||
self._respond(response)
|
self._respond(response)
|
||||||
|
|
||||||
_log.debug("[HTTP] finishing _handle for %s from %s:%d (%d)",
|
_log.debug("[HTTP] finishing _handle for %s from %s:%d (%d)",
|
||||||
|
@ -128,10 +130,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
response.code])
|
response.code])
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
self._handle("GET")
|
self._handle_wrap("GET")
|
||||||
|
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
self._handle("POST")
|
self._handle_wrap("POST")
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, format, *args):
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue