1 # Copyright (C) 2015 Ipsilon Contributors see COPYING for license
5 from functools import wraps
6 from ipsilon.util.endpoint import Endpoint
11 JSON output decorator. Does not handle binary data.
14 def wrapper(*args, **kw):
15 value = func(*args, **kw)
16 cherrypy.response.headers["Content-Type"] = \
17 "application/json;charset=utf-8"
18 return json.dumps(value, sort_keys=True, indent=2)
23 def rest_error(status=500, message=''):
25 Create a REST error response.
27 The assumption is that the jsonout wrapper will handle converting
30 cherrypy.response.status = status
31 cherrypy.response.headers['Content-Type'] = 'application/json'
32 return {'status': status, 'message': message}
35 class RestPage(Endpoint):
37 def __init__(self, *args, **kwargs):
38 super(RestPage, self).__init__(*args, **kwargs)
39 self.default_headers.update({
40 'Cache-Control': 'no-cache, must-revalidate',
42 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
44 self.auth_protect = True
47 class RestPlugins(RestPage):
48 def __init__(self, name, site, parent, facility, ordered=True):
49 super(RestPlugins, self).__init__(site)
52 self.title = '%s plugins' % name
53 self.url = '%s/%s' % (parent.url, name)
54 self.facility = facility
57 parent.add_subtree(name, self)
59 for plugin in self._site[facility].available:
60 obj = self._site[facility].available[plugin]
61 if hasattr(obj, 'rest'):
62 cherrypy.log.error('Rest plugin: %s' % plugin)
65 def root_with_msg(self, message=None, message_type=None, changed=None):
68 def root(self, *args, **kwargs):
69 return self.root_with_msg()
74 def __init__(self, site, mount):
75 super(Rest, self).__init__(site)
78 self.url = '%s/%s' % (self.basepath, mount)
82 def root(self, *args, **kwargs):
83 return rest_error(404, 'Not Found')
85 def add_subtree(self, name, page):
86 self.__dict__[name] = page
88 def del_subtree(self, name):
89 del self.__dict__[name]