aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jesopo2018-10-04 17:18:24 +0100
committerGravatar jesopo2018-10-04 17:18:24 +0100
commit7fc0359e3d08c0119b5a36fdbe675321ef7dd11d (patch)
tree325247644fd8c89505c25217e99ad59d7a19bfd0
parentAdded api.channels handler (diff)
signature
Make sure bot.lock is released in do_GET
-rw-r--r--modules/rest_api.py44
1 files changed, 23 insertions, 21 deletions
diff --git a/modules/rest_api.py b/modules/rest_api.py
index 63e16ee5..0b8ea74c 100644
--- a/modules/rest_api.py
+++ b/modules/rest_api.py
@@ -8,34 +8,36 @@ class Handler(http.server.BaseHTTPRequestHandler):
timeout = 10
def do_GET(self):
_bot.lock.acquire()
- parsed = urllib.parse.urlparse(self.path)
- query = parsed.query
- get_params = urllib.parse.parse_qs(query)
+ try:
+ parsed = urllib.parse.urlparse(self.path)
+ query = parsed.query
+ get_params = urllib.parse.parse_qs(query)
- response = ""
- code = 404
+ 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 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
+ 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:
+ self.send_response(code)
+ self.send_header("Content-type", "application/json")
+ self.end_headers()
self.wfile.write(response.encode("utf8"))
except:
pass
- _bot.lock.release()
+ finally:
+ _bot.lock.release()
@utils.export("botset", {"setting": "rest-api",
"help": "Enable/disable REST API",