mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Adding a function to ease error reporting
This commit is contained in:
parent
79c219a7d5
commit
ec01271dc0
@ -30,17 +30,14 @@ def set_formatter():
|
|||||||
else:
|
else:
|
||||||
request.formatter = ResponseHelper.responsize_xml
|
request.formatter = ResponseHelper.responsize_xml
|
||||||
|
|
||||||
|
request.error_formatter = lambda code, msg: request.formatter({ 'error': { 'code': code, 'message': msg } }, error = True)
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
def authorize():
|
def authorize():
|
||||||
if not request.path.startswith('/rest/'):
|
if not request.path.startswith('/rest/'):
|
||||||
return
|
return
|
||||||
|
|
||||||
error = request.formatter({
|
error = request.error_formatter(40, 'Unauthorized'), 401
|
||||||
'error': {
|
|
||||||
'code': 40,
|
|
||||||
'message': 'Unauthorized'
|
|
||||||
}
|
|
||||||
}, error = True), 401
|
|
||||||
|
|
||||||
(username, decoded_pass) = map(request.args.get, [ 'u', 'p' ])
|
(username, decoded_pass) = map(request.args.get, [ 'u', 'p' ])
|
||||||
if not username or not decoded_pass:
|
if not username or not decoded_pass:
|
||||||
@ -73,12 +70,7 @@ def not_found(error):
|
|||||||
if not request.path.startswith('/rest/'):
|
if not request.path.startswith('/rest/'):
|
||||||
return error
|
return error
|
||||||
|
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Not implemented'), 501
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Not implemented'
|
|
||||||
}
|
|
||||||
}, error = True), 501
|
|
||||||
|
|
||||||
class ResponseHelper:
|
class ResponseHelper:
|
||||||
|
|
||||||
|
@ -17,12 +17,7 @@ def rand_songs():
|
|||||||
toYear = int(toYear) if toYear else None
|
toYear = int(toYear) if toYear else None
|
||||||
fid = uuid.UUID(musicFolderId) if musicFolderId else None
|
fid = uuid.UUID(musicFolderId) if musicFolderId else None
|
||||||
except:
|
except:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Invalid parameter format')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Invalid parameter format'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
query = Track.query
|
query = Track.query
|
||||||
if fromYear:
|
if fromYear:
|
||||||
|
@ -24,12 +24,7 @@ def list_indexes():
|
|||||||
try:
|
try:
|
||||||
ifModifiedSince = int(ifModifiedSince)
|
ifModifiedSince = int(ifModifiedSince)
|
||||||
except:
|
except:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Invalid timestamp')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Invalid timestamp'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
if musicFolderId is None:
|
if musicFolderId is None:
|
||||||
folder = MusicFolder.query.all()
|
folder = MusicFolder.query.all()
|
||||||
@ -37,22 +32,12 @@ def list_indexes():
|
|||||||
try:
|
try:
|
||||||
mfid = uuid.UUID(musicFolderId)
|
mfid = uuid.UUID(musicFolderId)
|
||||||
except:
|
except:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Invalid id')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Invalid id'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
folder = MusicFolder.query.get(mfid)
|
folder = MusicFolder.query.get(mfid)
|
||||||
|
|
||||||
if not folder:
|
if not folder:
|
||||||
return request.formatter({
|
return request.error_formatter(70, 'Folder not found')
|
||||||
'error': {
|
|
||||||
'code': 70,
|
|
||||||
'message': 'Folder not found'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan
|
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()))
|
last_modif_ts = int(time.mktime(last_modif.timetuple()))
|
||||||
|
28
api/media.py
28
api/media.py
@ -9,42 +9,22 @@ import os.path, uuid
|
|||||||
def stream_media():
|
def stream_media():
|
||||||
id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
|
id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
|
||||||
if not id:
|
if not id:
|
||||||
return request.formatter({
|
return request.error_formatter(10, 'Missing media id')
|
||||||
'error': {
|
|
||||||
'code': 10,
|
|
||||||
'message': 'Missing media id'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tid = uuid.UUID(id)
|
tid = uuid.UUID(id)
|
||||||
except:
|
except:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Invalid media id')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'Message': 'Invalid media id'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
track = Track.query.get(tid)
|
track = Track.query.get(tid)
|
||||||
if not track:
|
if not track:
|
||||||
return request.formatter({
|
return request.error_formatter(70, 'Media not found'), 404
|
||||||
'error': {
|
|
||||||
'code': 70,
|
|
||||||
'message': 'Media not found'
|
|
||||||
}
|
|
||||||
}, error = True), 404
|
|
||||||
|
|
||||||
if maxBitRate:
|
if maxBitRate:
|
||||||
try:
|
try:
|
||||||
maxBitRate = int(maxBitRate)
|
maxBitRate = int(maxBitRate)
|
||||||
except:
|
except:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Invalid bitrate value')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Invalid bitrate value'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
if track.bitrate > maxBitRate:
|
if track.bitrate > maxBitRate:
|
||||||
# TODO transcode
|
# TODO transcode
|
||||||
|
14
api/user.py
14
api/user.py
@ -8,21 +8,11 @@ from db import User
|
|||||||
def user_info():
|
def user_info():
|
||||||
username = request.args.get('username')
|
username = request.args.get('username')
|
||||||
if username is None:
|
if username is None:
|
||||||
return request.formatter({
|
return request.error_formatter(10, 'Missing username')
|
||||||
'error': {
|
|
||||||
'code': 10,
|
|
||||||
'message': 'Missing username'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
user = User.query.filter(User.name == username).first()
|
user = User.query.filter(User.name == username).first()
|
||||||
if user is None:
|
if user is None:
|
||||||
return request.formatter({
|
return request.error_formatter(0, 'Unknown user')
|
||||||
'error': {
|
|
||||||
'code': 0,
|
|
||||||
'message': 'Unkwown user'
|
|
||||||
}
|
|
||||||
}, error = True)
|
|
||||||
|
|
||||||
return request.formatter({
|
return request.formatter({
|
||||||
'user': {
|
'user': {
|
||||||
|
Loading…
Reference in New Issue
Block a user