diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-09-29 14:08:30 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-09-29 14:08:30 +0200 |
commit | 5c385a3f12968051a060d5bda389777d4d0c732c (patch) | |
tree | b42a8ce95023fa04dcff5a13717a4637d1faff12 | |
parent | new plugin Base class providing useful methods (diff) | |
download | pyload-5c385a3f12968051a060d5bda389777d4d0c732c.tar.xz |
better json encoding
-rw-r--r-- | module/common/json_layer.py | 5 | ||||
-rw-r--r-- | module/web/api_app.py | 35 |
2 files changed, 18 insertions, 22 deletions
diff --git a/module/common/json_layer.py b/module/common/json_layer.py index e9ed003a0..a075d2527 100644 --- a/module/common/json_layer.py +++ b/module/common/json_layer.py @@ -1,12 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - # abstraction layer for json operations -try: # since python 2.7 +try: # since python 2.6 + import json from json import loads as json_loads from json import dumps as json_dumps except ImportError: + import module.lib.simplejson as json from module.lib.simplejson import loads as json_loads from module.lib.simplejson import dumps as json_dumps
\ No newline at end of file diff --git a/module/web/api_app.py b/module/web/api_app.py index 12fffe099..c14eb6ed5 100644 --- a/module/web/api_app.py +++ b/module/web/api_app.py @@ -10,27 +10,22 @@ from bottle import route, request, response, HTTPError from thrift.protocol.TBase import TBase from utils import toDict, set_session - from webinterface import PYLOAD -from module.common.json_layer import json_dumps +from module.common.json_layer import json try: from ast import literal_eval except ImportError: # python 2.5 from module.lib.SafeEval import safe_eval as literal_eval +# json encoder that accepts TBase objects +class TBaseEncoder(json.JSONEncoder): -# convert to format serializable by json -def traverse(data): - if type(data) == list: - return [traverse(x) for x in data] - elif type(data) == dict: - return dict([(x, traverse(y)) for x, y in data.iteritems()]) - elif isinstance(data, TBase): - return toDict(data) - else: - return data + def default(self, o): + if isinstance(o, TBase): + return toDict(o) + return json.JSONEncoder.default(self, o) # accepting positional arguments, as well as kwargs via post and get @@ -46,10 +41,10 @@ def call_api(func, args=""): s = s.get_by_id(request.POST['session']) if not s or not s.get("authenticated", False): - return HTTPError(403, json_dumps("Forbidden")) + return HTTPError(403, json.dumps("Forbidden")) if not PYLOAD.isAuthorized(func, {"role": s["role"], "permission": s["perms"]}): - return HTTPError(401, json_dumps("Unauthorized")) + return HTTPError(401, json.dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} @@ -62,18 +57,18 @@ def call_api(func, args=""): return callApi(func, *args, **kwargs) except Exception, e: print_exc() - return HTTPError(500, json_dumps({"error": e.message, "traceback": format_exc()})) + return HTTPError(500, json.dumps({"error": e.message, "traceback": format_exc()})) def callApi(func, *args, **kwargs): if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func - return HTTPError(404, json_dumps("Not Found")) + return HTTPError(404, json.dumps("Not Found")) result = getattr(PYLOAD, func)(*[literal_eval(x) for x in args], **dict([(x, literal_eval(y)) for x, y in kwargs.iteritems()])) - return json_dumps(traverse(result)) + return json.dumps(result, cls=TBaseEncoder) #post -> username, password @@ -88,16 +83,16 @@ def login(): info = PYLOAD.checkAuth(user, password) if not info: - return json_dumps(False) + return json.dumps(False) s = set_session(request, info) # get the session id by dirty way, documentations seems wrong try: sid = s._headers["cookie_out"].split("=")[1].split(";")[0] - return json_dumps(sid) + return json.dumps(sid) except: - return json_dumps(True) + return json.dumps(True) @route("/api/logout") |