1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-14 22:22:18 +00:00
supysonic/api/__init__.py

143 lines
3.8 KiB
Python
Raw Normal View History

2012-10-13 09:29:48 +00:00
# coding: utf-8
from flask import request
from flask import Response
2012-10-13 09:29:48 +00:00
import simplejson
import cgi
2012-12-02 15:42:25 +00:00
import uuid
import StringIO
from dict2xml import *
2012-10-13 09:29:48 +00:00
from web import app
2013-09-05 16:40:43 +00:00
from managers.user import UserManager
2012-10-13 09:29:48 +00:00
@app.before_request
def set_formatter():
if not request.path.startswith('/rest/'):
return
"""Return a function to create the response."""
(f, callback) = map(request.args.get, ['f', 'callback'])
if f == 'jsonp':
2012-10-20 18:05:39 +00:00
# Some clients (MiniSub, Perisonic) set f to jsonp without callback for streamed data
2012-11-11 20:39:26 +00:00
if not callback and request.endpoint not in [ 'stream_media', 'cover_art' ]:
2012-10-13 09:29:48 +00:00
return ResponseHelper.responsize_json({
'error': {
'code': 0,
'message': 'Missing callback'
}
}, error = True), 400
request.formatter = lambda x, **kwargs: ResponseHelper.responsize_jsonp(x, callback, kwargs)
elif f == "json":
request.formatter = ResponseHelper.responsize_json
else:
request.formatter = ResponseHelper.responsize_xml
request.error_formatter = lambda code, msg: request.formatter({ 'error': { 'code': code, 'message': msg } }, error = True)
2012-10-13 09:29:48 +00:00
@app.before_request
def authorize():
if not request.path.startswith('/rest/'):
return
error = request.error_formatter(40, 'Unauthorized'), 401
2012-10-13 09:29:48 +00:00
if request.authorization:
status, user = UserManager.try_auth(request.authorization.username, request.authorization.password)
if status == UserManager.SUCCESS:
request.username = request.authorization.username
request.user = user
return
2012-12-05 22:38:35 +00:00
(username, password) = map(request.args.get, [ 'u', 'p' ])
if not username or not password:
2012-10-13 09:29:48 +00:00
return error
status, user = UserManager.try_auth(username, password)
if status != UserManager.SUCCESS:
2012-10-13 09:29:48 +00:00
return error
request.username = username
request.user = user
2012-10-13 09:29:48 +00:00
@app.after_request
def set_content_type(response):
if not request.path.startswith('/rest/'):
return response
2012-10-20 18:05:39 +00:00
if response.mimetype.startswith('text'):
f = request.args.get('f')
response.headers['content-type'] = 'application/json' if f in [ 'jsonp', 'json' ] else 'text/xml'
2012-10-13 09:29:48 +00:00
return response
@app.errorhandler(404)
def not_found(error):
if not request.path.startswith('/rest/'):
return error
return request.error_formatter(0, 'Not implemented'), 501
2012-10-13 09:29:48 +00:00
class ResponseHelper:
@staticmethod
def responsize_json(ret, error = False, version = "1.8.0"):
2013-06-12 21:01:24 +00:00
def check_lists(d):
for key, value in d.items():
if isinstance(value, dict):
d[key] = check_lists(value)
elif isinstance(value, list):
if len(value) == 0:
del d[key]
else:
d[key] = [ check_lists(item) if isinstance(item, dict) else item for item in value ]
return d
ret = check_lists(ret)
2012-10-13 09:29:48 +00:00
# add headers to response
ret.update({
'status': 'failed' if error else 'ok',
'version': version,
'xmlns': "http://subsonic.org/restapi"
})
return simplejson.dumps({ 'subsonic-response': ret }, indent = True, encoding = 'utf-8')
2012-10-13 09:29:48 +00:00
@staticmethod
def responsize_jsonp(ret, callback, error = False, version = "1.8.0"):
return "%s(%s)" % (callback, ResponseHelper.responsize_json(ret, error, version))
2012-10-13 09:29:48 +00:00
@staticmethod
def responsize_xml(ret, error = False, version = "1.8.0"):
ret.update({
'status': 'failed' if error else 'ok',
'version': version,
'xmlns': "http://subsonic.org/restapi"
})
output = dict2xml(ret, "subsonic-response")
return Response(u'<?xml version="1.0" encoding="UTF-8"?>' + output, content_type='text/xml; charset=utf-8')
2012-10-13 09:29:48 +00:00
2012-12-02 15:42:25 +00:00
def get_entity(req, ent, param = 'id'):
eid = req.args.get(param)
if not eid:
return False, req.error_formatter(10, 'Missing %s id' % ent.__name__)
try:
eid = uuid.UUID(eid)
except:
return False, req.error_formatter(0, 'Invalid %s id' % ent.__name__)
entity = ent.query.get(eid)
if not entity:
return False, (req.error_formatter(70, '%s not found' % ent.__name__), 404)
return True, entity