diff --git a/api/__init__.py b/api/__init__.py index 727919c..a16b28b 100755 --- a/api/__init__.py +++ b/api/__init__.py @@ -30,17 +30,14 @@ def set_formatter(): else: request.formatter = ResponseHelper.responsize_xml + request.error_formatter = lambda code, msg: request.formatter({ 'error': { 'code': code, 'message': msg } }, error = True) + @app.before_request def authorize(): if not request.path.startswith('/rest/'): return - error = request.formatter({ - 'error': { - 'code': 40, - 'message': 'Unauthorized' - } - }, error = True), 401 + error = request.error_formatter(40, 'Unauthorized'), 401 (username, decoded_pass) = map(request.args.get, [ 'u', 'p' ]) if not username or not decoded_pass: @@ -73,12 +70,7 @@ def not_found(error): if not request.path.startswith('/rest/'): return error - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Not implemented' - } - }, error = True), 501 + return request.error_formatter(0, 'Not implemented'), 501 class ResponseHelper: diff --git a/api/albums_songs.py b/api/albums_songs.py index e07db20..e2a361c 100755 --- a/api/albums_songs.py +++ b/api/albums_songs.py @@ -17,12 +17,7 @@ def rand_songs(): toYear = int(toYear) if toYear else None fid = uuid.UUID(musicFolderId) if musicFolderId else None except: - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Invalid parameter format' - } - }, error = True) + return request.error_formatter(0, 'Invalid parameter format') query = Track.query if fromYear: diff --git a/api/browse.py b/api/browse.py index 276a7f4..f8ffd58 100755 --- a/api/browse.py +++ b/api/browse.py @@ -24,12 +24,7 @@ def list_indexes(): try: ifModifiedSince = int(ifModifiedSince) except: - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Invalid timestamp' - } - }, error = True) + return request.error_formatter(0, 'Invalid timestamp') if musicFolderId is None: folder = MusicFolder.query.all() @@ -37,22 +32,12 @@ def list_indexes(): try: mfid = uuid.UUID(musicFolderId) except: - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Invalid id' - } - }, error = True) + return request.error_formatter(0, 'Invalid id') folder = MusicFolder.query.get(mfid) if not folder: - return request.formatter({ - 'error': { - 'code': 70, - 'message': 'Folder not found' - } - }, error = True) + return request.error_formatter(70, 'Folder not found') last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan last_modif_ts = int(time.mktime(last_modif.timetuple())) diff --git a/api/media.py b/api/media.py index 9b34bd8..1e3144a 100755 --- a/api/media.py +++ b/api/media.py @@ -9,42 +9,22 @@ import os.path, uuid def stream_media(): id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ]) if not id: - return request.formatter({ - 'error': { - 'code': 10, - 'message': 'Missing media id' - } - }, error = True) + return request.error_formatter(10, 'Missing media id') try: tid = uuid.UUID(id) except: - return request.formatter({ - 'error': { - 'code': 0, - 'Message': 'Invalid media id' - } - }, error = True) + return request.error_formatter(0, 'Invalid media id') track = Track.query.get(tid) if not track: - return request.formatter({ - 'error': { - 'code': 70, - 'message': 'Media not found' - } - }, error = True), 404 + return request.error_formatter(70, 'Media not found'), 404 if maxBitRate: try: maxBitRate = int(maxBitRate) except: - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Invalid bitrate value' - } - }, error = True) + return request.error_formatter(0, 'Invalid bitrate value') if track.bitrate > maxBitRate: # TODO transcode diff --git a/api/user.py b/api/user.py index 947677e..2200d7a 100755 --- a/api/user.py +++ b/api/user.py @@ -8,21 +8,11 @@ from db import User def user_info(): username = request.args.get('username') if username is None: - return request.formatter({ - 'error': { - 'code': 10, - 'message': 'Missing username' - } - }, error = True) + return request.error_formatter(10, 'Missing username') user = User.query.filter(User.name == username).first() if user is None: - return request.formatter({ - 'error': { - 'code': 0, - 'message': 'Unkwown user' - } - }, error = True) + return request.error_formatter(0, 'Unknown user') return request.formatter({ 'user': {