mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 08:56:17 +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:
|
||||
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:
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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()))
|
||||
|
28
api/media.py
28
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
|
||||
|
14
api/user.py
14
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': {
|
||||
|
Loading…
Reference in New Issue
Block a user