Make sure bot.lock is released in do_GET
This commit is contained in:
parent
fdddb33f32
commit
7fc0359e3d
1 changed files with 26 additions and 24 deletions
|
@ -8,34 +8,36 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
timeout = 10
|
timeout = 10
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
_bot.lock.acquire()
|
_bot.lock.acquire()
|
||||||
parsed = urllib.parse.urlparse(self.path)
|
|
||||||
query = parsed.query
|
|
||||||
get_params = urllib.parse.parse_qs(query)
|
|
||||||
|
|
||||||
response = ""
|
|
||||||
code = 404
|
|
||||||
|
|
||||||
if not "key" in get_params or not _bot.get_setting(
|
|
||||||
"api-key-%s" % get_params["key"][0], False):
|
|
||||||
code = 401
|
|
||||||
else:
|
|
||||||
if parsed.path.startswith("/api/"):
|
|
||||||
_, _, endpoint = parsed.path[1:].partition("/")
|
|
||||||
response = _events.on("api").on(endpoint).call_for_result(
|
|
||||||
params=get_params, path=endpoint.split("/"))
|
|
||||||
|
|
||||||
if response:
|
|
||||||
response = json.dumps(response, sort_keys=True, indent=4)
|
|
||||||
code = 200
|
|
||||||
|
|
||||||
self.send_response(code)
|
|
||||||
self.send_header("Content-type", "application/json")
|
|
||||||
self.end_headers()
|
|
||||||
try:
|
try:
|
||||||
|
parsed = urllib.parse.urlparse(self.path)
|
||||||
|
query = parsed.query
|
||||||
|
get_params = urllib.parse.parse_qs(query)
|
||||||
|
|
||||||
|
response = ""
|
||||||
|
code = 404
|
||||||
|
|
||||||
|
if not "key" in get_params or not _bot.get_setting(
|
||||||
|
"api-key-%s" % get_params["key"][0], False):
|
||||||
|
code = 401
|
||||||
|
else:
|
||||||
|
if parsed.path.startswith("/api/"):
|
||||||
|
_, _, endpoint = parsed.path[1:].partition("/")
|
||||||
|
response = _events.on("api").on(endpoint).call_for_result(
|
||||||
|
params=get_params, path=endpoint.split("/"))
|
||||||
|
|
||||||
|
if response:
|
||||||
|
response = json.dumps(response, sort_keys=True,
|
||||||
|
indent=4)
|
||||||
|
code = 200
|
||||||
|
|
||||||
|
self.send_response(code)
|
||||||
|
self.send_header("Content-type", "application/json")
|
||||||
|
self.end_headers()
|
||||||
self.wfile.write(response.encode("utf8"))
|
self.wfile.write(response.encode("utf8"))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
_bot.lock.release()
|
finally:
|
||||||
|
_bot.lock.release()
|
||||||
|
|
||||||
@utils.export("botset", {"setting": "rest-api",
|
@utils.export("botset", {"setting": "rest-api",
|
||||||
"help": "Enable/disable REST API",
|
"help": "Enable/disable REST API",
|
||||||
|
|
Loading…
Reference in a new issue