diff --git a/couchpotato/__init__.py b/couchpotato/__init__.py index 11d8de93..8dc691da 100644 --- a/couchpotato/__init__.py +++ b/couchpotato/__init__.py @@ -25,7 +25,7 @@ class WebHandler(RequestHandler): if not views.get(route): page_not_found(self) return - self.finish(views[route]()) + self.write(views[route]()) def addView(route, func, static = False): views[route] = func @@ -64,7 +64,7 @@ class KeyHandler(RequestHandler): if (self.get_argument('u') == md5(username) or not username) and (self.get_argument('p') == password or not password): api = Env.setting('api_key') - self.finish({ + self.write({ 'success': api is not None, 'api_key': api }) @@ -81,5 +81,5 @@ def page_not_found(rh): time.sleep(0.1) rh.set_status(404) - rh.finish('Wrong API key used') + rh.write('Wrong API key used') diff --git a/couchpotato/api.py b/couchpotato/api.py index 258537b6..77957f18 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -4,10 +4,12 @@ from threading import Thread from tornado.gen import coroutine from tornado.web import RequestHandler, asynchronous import json +import threading import tornado import urllib api = {} +api_locks = {} api_nonblock = {} api_docs = {} @@ -39,7 +41,7 @@ class NonBlockHandler(RequestHandler): if self.request.connection.stream.closed(): return - self.finish(response) + self.write(response) def on_connection_close(self): @@ -63,9 +65,11 @@ class ApiHandler(RequestHandler): def get(self, route, *args, **kwargs): route = route.strip('/') if not api.get(route): - self.finish('API call doesn\'t seem to exist') + self.write('API call doesn\'t seem to exist') return + api_locks[route].acquire() + kwargs = {} for x in self.request.arguments: kwargs[x] = urllib.unquote(self.get_argument(x)) @@ -88,16 +92,20 @@ class ApiHandler(RequestHandler): jsonp_callback = self.get_argument('callback_func', default = None) if jsonp_callback: - self.finish(str(jsonp_callback) + '(' + json.dumps(result) + ')') + self.write(str(jsonp_callback) + '(' + json.dumps(result) + ')') elif isinstance(result, (tuple)) and result[0] == 'redirect': self.redirect(result[1]) else: - self.finish(result) + self.write(result) + + api_locks[route].release() def addApiView(route, func, static = False, docs = None, **kwargs): if static: func(route) - else: api[route] = func + else: + api[route] = func + api_locks[route] = threading.Lock() if docs: api_docs[route[4:] if route[0:4] == 'api.' else route] = docs