diff --git a/motioneye/remote.py b/motioneye/remote.py index 943db06a3..4a4e909b4 100644 --- a/motioneye/remote.py +++ b/motioneye/remote.py @@ -14,12 +14,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import functools import json import logging import re -from tornado.httpclient import AsyncHTTPClient, HTTPRequest +from typing import Union + +from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPResponse from motioneye import settings from motioneye import utils @@ -60,23 +61,21 @@ def _make_request(scheme, host, port, username, password, path, method='GET', da validate_cert=settings.VALIDATE_CERTS) -def _callback_wrapper(callback): - @functools.wraps(callback) - def wrapper(response): - try: - decoded = json.loads(response.body) - if decoded['error'] == 'unauthorized': - response.error = 'Authentication Error' +async def _send_request(request: HTTPRequest) -> HTTPResponse: + response = await AsyncHTTPClient().fetch(request) - elif decoded['error']: - response.error = decoded['error'] + try: + decoded = json.loads(response.body) + if decoded['error'] == 'unauthorized': + response.error = 'Authentication Error' - except: - pass + elif decoded['error']: + response.error = decoded['error'] - return callback(response) + except: + pass - return wrapper + return response def pretty_camera_url(local_config, camera=True): @@ -124,7 +123,14 @@ def _remote_params(local_config): return params -def list(local_config, callback): +def make_camera_response(c): + return { + 'id': c['id'], + 'name': c['name'] + } + + +async def list_cameras(local_config) -> utils.GetCamerasResponse: scheme, host, port, username, password, path, _ = _remote_params(local_config) logging.debug('listing remote cameras on %(url)s' % { @@ -133,42 +139,36 @@ def list(local_config, callback): request = _make_request(scheme, host, port, username, password, path + '/config/list/') - def on_response(response): - def make_camera_response(c): - return { - 'id': c['id'], - 'name': c['name'] - } + response = await _send_request(request) - if response.error: - logging.error('failed to list remote cameras on %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config, camera=False), - 'msg': utils.pretty_http_error(response)}) + if response.error: + logging.error('failed to list remote cameras on %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config, camera=False), + 'msg': utils.pretty_http_error(response)}) - return callback(error=utils.pretty_http_error(response)) + return utils.GetCamerasResponse(None, utils.pretty_http_error(response)) - try: - response = json.loads(response.body) + try: + response = json.loads(response.body) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config, camera=False), - 'msg': str(e)}) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config, camera=False), + 'msg': str(e)}) - return callback(error=str(e)) + return utils.GetCamerasResponse(None, str(e)) + + else: cameras = response['cameras'] - + # filter out simple mjpeg cameras cameras = [make_camera_response(c) for c in cameras if c['proto'] != 'mjpeg' and c.get('enabled')] + + return utils.GetCamerasResponse(cameras, None) - callback(cameras) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - - -def get_config(local_config, callback): +async def get_config(local_config) -> utils.GetConfigResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('getting config for remote camera %(id)s on %(url)s' % { @@ -177,36 +177,34 @@ def get_config(local_config, callback): request = _make_request(scheme, host, port, username, password, path + '/config/%(id)s/get/' % {'id': camera_id}) + response = await _send_request(request) - def on_response(response): - if response.error: - logging.error('failed to get config for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + if response.error: + logging.error('failed to get config for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - try: - response = json.loads(response.body) + return utils.GetConfigResponse(None, error=utils.pretty_http_error(response)) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config), - 'msg': str(e)}) + try: + response = json.loads(response.body) - return callback(error=str(e)) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config), + 'msg': str(e)}) + return utils.GetConfigResponse(None, error=str(e)) + + else: response['host'] = host response['port'] = port - callback(response) - - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return utils.GetConfigResponse(remote_ui_config=response, error=None) -def set_config(local_config, ui_config, callback): +async def set_config(local_config, ui_config) -> Union[str, None]: scheme = local_config.get('@scheme', local_config.get('scheme')) host = local_config.get('@host', local_config.get('host')) port = local_config.get('@port', local_config.get('port')) @@ -224,23 +222,22 @@ def set_config(local_config, ui_config, callback): p = path + '/config/%(id)s/set/' % {'id': camera_id} request = _make_request(scheme, host, port, username, password, p, method='POST', data=ui_config, content_type='application/json') + response = await _send_request(request) - def on_response(response): - if response.error: - logging.error('failed to set config for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) + result = None - return callback(error=utils.pretty_http_error(response)) + if response.error: + logging.error('failed to set config for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - callback() + result = utils.pretty_http_error(response) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return result -def test(local_config, data, callback): +async def test(local_config, data) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) what = data['what'] logging.debug('testing %(what)s on remote camera %(id)s, on %(url)s' % { @@ -253,29 +250,25 @@ def test(local_config, data, callback): p = path + '/config/%(id)s/test/' % {'id': camera_id} request = _make_request(scheme, host, port, username, password, p, method='POST', data=data, content_type='application/json') + response = await _send_request(request) + if response.error: + logging.error('failed to test %(what)s on remote camera %(id)s, on %(url)s: %(msg)s' % { + 'what': what, + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to test %(what)s on remote camera %(id)s, on %(url)s: %(msg)s' % { - 'what': what, - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) + return utils.CommonExternalResponse(None, error=utils.pretty_http_error(response)) - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(None, None) # it will never return result = True, what the point? - callback() - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - - -def get_current_picture(local_config, width, height, callback): +async def get_current_picture(local_config, width, height) -> utils.GetCurrentPictureResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) - # logging.debug('getting current picture for remote camera %(id)s on %(url)s' % { - # 'id': camera_id, - # 'url': pretty_camera_url(local_config)}) + logging.debug('getting current picture for remote camera %(id)s on %(url)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config)}) query = {} @@ -286,30 +279,29 @@ def get_current_picture(local_config, width, height, callback): query['height'] = str(height) p = path + '/picture/%(id)s/current/' % {'id': camera_id} + request = _make_request(scheme, host, port, username, password, p, query=query) + response = await _send_request(request) + + cookies = utils.parse_cookies(response.headers.get_list('Set-Cookie')) + motion_detected = cookies.get('motion_detected_' + str(camera_id)) == 'true' + capture_fps = cookies.get('capture_fps_' + str(camera_id)) + capture_fps = float(capture_fps) if capture_fps else 0 + monitor_info = cookies.get('monitor_info_' + str(camera_id)) - def on_response(response): - cookies = utils.parse_cookies(response.headers.get_list('Set-Cookie')) - motion_detected = cookies.get('motion_detected_' + str(camera_id)) == 'true' - capture_fps = cookies.get('capture_fps_' + str(camera_id)) - capture_fps = float(capture_fps) if capture_fps else 0 - monitor_info = cookies.get('monitor_info_' + str(camera_id)) - - if response.error: - logging.error('failed to get current picture for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) + if response.error: + logging.error('failed to get current picture for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - return callback(error=utils.pretty_http_error(response)) + return utils.GetCurrentPictureResponse(error=utils.pretty_http_error(response)) - callback(motion_detected, capture_fps, monitor_info, response.body) + return utils.GetCurrentPictureResponse(motion_detected=motion_detected, capture_fps=capture_fps, + monitor_info=monitor_info, picture=response.body) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - -def list_media(local_config, media_type, prefix, callback): +async def list_media(local_config, media_type, prefix) -> utils.ListMediaResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('getting media list for remote camera %(id)s on %(url)s' % { @@ -324,33 +316,29 @@ def list_media(local_config, media_type, prefix, callback): p = path + '/%(media_type)s/%(id)s/list/' % {'id': camera_id, 'media_type': media_type} request = _make_request(scheme, host, port, username, password, p, query=query, timeout=10 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + logging.error('failed to get media list for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to get media list for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + return utils.ListMediaResponse(error=utils.pretty_http_error(response)) - try: - response = json.loads(response.body) + try: + response = json.loads(response.body) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config), - 'msg': str(e)}) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config), + 'msg': str(e)}) - return callback(error=str(e)) + return utils.ListMediaResponse(error=str(e)) - return callback(response) + return utils.ListMediaResponse(media_list=response) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - -def get_media_content(local_config, filename, media_type, callback): +async def get_media_content(local_config, filename, media_type) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('downloading file %(filename)s of remote camera %(id)s on %(url)s' % { @@ -366,24 +354,20 @@ def get_media_content(local_config, filename, media_type, callback): # timeout here is 10 times larger than usual - we expect a big delay when fetching the media list request = _make_request(scheme, host, port, username, password, path, timeout=10 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + logging.error('failed to download file %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { + 'filename': filename, + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to download file %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { - 'filename': filename, - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) - - return callback(response.body) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return utils.CommonExternalResponse(result=response.body) -def make_zipped_content(local_config, media_type, group, callback): +async def make_zipped_content(local_config, media_type, group) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('preparing zip file for group "%(group)s" of remote camera %(id)s on %(url)s' % { @@ -399,37 +383,33 @@ def make_zipped_content(local_config, media_type, group, callback): # timeout here is 100 times larger than usual - we expect a big delay request = _make_request(scheme, host, port, username, password, prepare_path, timeout=100 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + msg = 'failed to prepare zip file for group "%(group)s" ' \ + 'of remote camera %(id)s on %(url)s: %(msg)s' % { + 'group': group or 'ungrouped', + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)} - def on_response(response): - if response.error: - msg = 'failed to prepare zip file for group "%(group)s" ' \ - 'of remote camera %(id)s on %(url)s: %(msg)s' % { - 'group': group or 'ungrouped', - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)} - - logging.error(msg) - - return callback(error=utils.pretty_http_error(response)) + logging.error(msg) - try: - key = json.loads(response.body)['key'] + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config), - 'msg': str(e)}) + try: + key = json.loads(response.body)['key'] - return callback(error=str(e)) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config), + 'msg': str(e)}) - callback({'key': key}) - - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return utils.CommonExternalResponse(error=str(e)) + else: + return utils.CommonExternalResponse(result={'key': key}) -def get_zipped_content(local_config, media_type, key, group, callback): +async def get_zipped_content(local_config, media_type, key, group) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('downloading zip file for remote camera %(id)s on %(url)s' % { @@ -444,27 +424,23 @@ def get_zipped_content(local_config, media_type, key, group, callback): request = _make_request(scheme, host, port, username, password, p, timeout=10 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + logging.error('failed to download zip file for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to download zip file for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - callback({ - 'data': response.body, - 'content_type': response.headers.get('Content-Type'), - 'content_disposition': response.headers.get('Content-Disposition') - }) + return utils.CommonExternalResponse(result={ + 'data': response.body, + 'content_type': response.headers.get('Content-Type'), + 'content_disposition': response.headers.get('Content-Disposition') + }) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - -def make_timelapse_movie(local_config, framerate, interval, group, callback): +async def make_timelapse_movie(local_config, framerate, interval, group) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) msg = 'making timelapse movie for group "%(group)s" of remote camera %(id)s ' \ @@ -485,39 +461,36 @@ def make_timelapse_movie(local_config, framerate, interval, group, callback): request = _make_request(scheme, host, port, username, password, path, timeout=100 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + msg = 'failed to make timelapse movie for group "%(group)s" of remote camera %(id)s ' \ + 'with rate %(framerate)s/%(int)s on %(url)s: %(msg)s' % { + 'group': group or 'ungrouped', + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'int': interval, + 'framerate': framerate, + 'msg': utils.pretty_http_error(response)} - def on_response(response): - if response.error: - msg = 'failed to make timelapse movie for group "%(group)s" of remote camera %(id)s ' \ - 'with rate %(framerate)s/%(int)s on %(url)s: %(msg)s' % { - 'group': group or 'ungrouped', - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'int': interval, - 'framerate': framerate, - 'msg': utils.pretty_http_error(response)} - - logging.error(msg) + logging.error(msg) - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - try: - response = json.loads(response.body) + try: + response = json.loads(response.body) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config), - 'msg': str(e)}) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config), + 'msg': str(e)}) - return callback(error=str(e)) + return utils.CommonExternalResponse(error=str(e)) - callback(response) - - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + else: + return utils.CommonExternalResponse(result=response) -def check_timelapse_movie(local_config, group, callback): +async def check_timelapse_movie(local_config, group) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('checking timelapse movie status for remote camera %(id)s on %(url)s' % { @@ -528,33 +501,31 @@ def check_timelapse_movie(local_config, group, callback): 'id': camera_id, 'group': group} request = _make_request(scheme, host, port, username, password, p) + response = await _send_request(request) + + if response.error: + logging.error('failed to check timelapse movie status for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to check timelapse movie status for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - try: - response = json.loads(response.body) + try: + response = json.loads(response.body) - except Exception as e: - logging.error('failed to decode json answer from %(url)s: %(msg)s' % { - 'url': pretty_camera_url(local_config), - 'msg': str(e)}) + except Exception as e: + logging.error('failed to decode json answer from %(url)s: %(msg)s' % { + 'url': pretty_camera_url(local_config), + 'msg': str(e)}) - return callback(error=str(e)) + return utils.CommonExternalResponse(error=str(e)) - callback(response) - - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + else: + return utils.CommonExternalResponse(result=response) -def get_timelapse_movie(local_config, key, group, callback): +async def get_timelapse_movie(local_config, key, group) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('downloading timelapse movie for remote camera %(id)s on %(url)s' % { @@ -568,27 +539,23 @@ def get_timelapse_movie(local_config, key, group, callback): request = _make_request(scheme, host, port, username, password, p, timeout=10 * settings.REMOTE_REQUEST_TIMEOUT) + response = await _send_request(request) + if response.error: + logging.error('failed to download timelapse movie for remote camera %(id)s on %(url)s: %(msg)s' % { + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to download timelapse movie for remote camera %(id)s on %(url)s: %(msg)s' % { - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(result={ + 'data': response.body, + 'content_type': response.headers.get('Content-Type'), + 'content_disposition': response.headers.get('Content-Disposition') + }) - callback({ - 'data': response.body, - 'content_type': response.headers.get('Content-Type'), - 'content_disposition': response.headers.get('Content-Disposition') - }) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - - -def get_media_preview(local_config, filename, media_type, width, height, callback): +async def get_media_preview(local_config, filename, media_type, width, height) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('getting file preview for %(filename)s of remote camera %(id)s on %(url)s' % { @@ -610,24 +577,20 @@ def get_media_preview(local_config, filename, media_type, width, height, callbac query['height'] = str(height) request = _make_request(scheme, host, port, username, password, path, query=query) + response = await _send_request(request) + if response.error: + logging.error('failed to get file preview for %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { + 'filename': filename, + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to get file preview for %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { - 'filename': filename, - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - callback(response.body) + return utils.CommonExternalResponse(result=response.body) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - -def del_media_content(local_config, filename, media_type, callback): +async def del_media_content(local_config, filename, media_type) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('deleting file %(filename)s of remote camera %(id)s on %(url)s' % { @@ -642,24 +605,20 @@ def del_media_content(local_config, filename, media_type, callback): request = _make_request(scheme, host, port, username, password, path, method='POST', data='{}', timeout=settings.REMOTE_REQUEST_TIMEOUT, content_type='application/json') + response = await _send_request(request) + if response.error: + logging.error('failed to delete file %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { + 'filename': filename, + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to delete file %(filename)s of remote camera %(id)s on %(url)s: %(msg)s' % { - 'filename': filename, - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - callback() + return utils.CommonExternalResponse() - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) - -def del_media_group(local_config, group, media_type, callback): +async def del_media_group(local_config, group, media_type) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('deleting group "%(group)s" of remote camera %(id)s on %(url)s' % { @@ -674,24 +633,20 @@ def del_media_group(local_config, group, media_type, callback): request = _make_request(scheme, host, port, username, password, path, method='POST', data='{}', timeout=settings.REMOTE_REQUEST_TIMEOUT, content_type='application/json') + response = await _send_request(request) + if response.error: + logging.error('failed to delete group "%(group)s" of remote camera %(id)s on %(url)s: %(msg)s' % { + 'group': group or 'ungrouped', + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to delete group "%(group)s" of remote camera %(id)s on %(url)s: %(msg)s' % { - 'group': group or 'ungrouped', - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) - - callback() + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return utils.CommonExternalResponse() -def exec_action(local_config, action, callback): +async def exec_action(local_config, action) -> utils.CommonExternalResponse: scheme, host, port, username, password, path, camera_id = _remote_params(local_config) logging.debug('executing action "%(action)s" of remote camera %(id)s on %(url)s' % { @@ -705,18 +660,14 @@ def exec_action(local_config, action, callback): request = _make_request(scheme, host, port, username, password, path, method='POST', data='{}', timeout=settings.REMOTE_REQUEST_TIMEOUT, content_type='application/json') + response = await _send_request(request) + if response.error: + logging.error('failed to execute action "%(action)s" of remote camera %(id)s on %(url)s: %(msg)s' % { + 'action': action, + 'id': camera_id, + 'url': pretty_camera_url(local_config), + 'msg': utils.pretty_http_error(response)}) - def on_response(response): - if response.error: - logging.error('failed to execute action "%(action)s" of remote camera %(id)s on %(url)s: %(msg)s' % { - 'action': action, - 'id': camera_id, - 'url': pretty_camera_url(local_config), - 'msg': utils.pretty_http_error(response)}) - - return callback(error=utils.pretty_http_error(response)) - - callback() + return utils.CommonExternalResponse(error=utils.pretty_http_error(response)) - http_client = AsyncHTTPClient() - http_client.fetch(request, _callback_wrapper(on_response)) + return utils.CommonExternalResponse()