1 # Copyright (C) 2015 Ipsilon project Contributors, for license see COPYING
6 from functools import wraps
7 from ipsilon.util.endpoint import Endpoint
12 JSON output decorator. Does not handle binary data.
15 def wrapper(*args, **kw):
16 value = func(*args, **kw)
17 cherrypy.response.headers["Content-Type"] = \
18 "application/json;charset=utf-8"
19 return json.dumps(value, sort_keys=True, indent=2)
24 def rest_error(status=500, message=''):
26 Create a REST error response.
28 The assumption is that the jsonout wrapper will handle converting
31 cherrypy.response.status = status
32 cherrypy.response.headers['Content-Type'] = 'application/json'
33 return {'status': status, 'message': message}
36 class RestPage(Endpoint):
38 def __init__(self, *args, **kwargs):
39 super(RestPage, self).__init__(*args, **kwargs)
40 self.auth_protect = True
43 class RestPlugins(RestPage):
44 def __init__(self, name, site, parent, facility, ordered=True):
45 super(RestPlugins, self).__init__(site)
48 self.title = '%s plugins' % name
49 self.url = '%s/%s' % (parent.url, name)
50 self.facility = facility
53 parent.add_subtree(name, self)
55 for plugin in self._site[facility].available:
56 obj = self._site[facility].available[plugin]
57 if hasattr(obj, 'rest'):
58 cherrypy.log.error('Rest plugin: %s' % plugin,
59 severity=logging.DEBUG)
62 def root_with_msg(self, message=None, message_type=None, changed=None):
65 def root(self, *args, **kwargs):
66 return self.root_with_msg()
71 def __init__(self, site, mount):
72 super(Rest, self).__init__(site)
75 self.url = '%s/%s' % (self.basepath, mount)
79 def root(self, *args, **kwargs):
80 return rest_error(404, 'Not Found')
82 def add_subtree(self, name, page):
83 self.__dict__[name] = page
85 def del_subtree(self, name):
86 del self.__dict__[name]