Browse files

Include metadata in responses

  • Loading branch information...
1 parent 26dbf50 commit 4600ecf389258520e41794b026812813536bea09 @srynot4sale committed Mar 29, 2013
Showing with 38 additions and 17 deletions.
  1. +5 −3 functions/function.py
  2. +22 −8 functions/list.py
  3. +10 −5 functions/server.py
  4. +1 −1 interfaces/http.py
View
8 functions/function.py
@@ -61,18 +61,20 @@ class response(object):
state = None
message = None
data = None
+ write = 0
def __init__(self, state, message = '', data = None):
self.state = state
self.message = message
self.data = data
- def returnBasic(self):
+ def returnAdvanced(self):
basic = {}
basic['state'] = self.state
basic['message'] = self.message
basic['data'] = self.data
+ basic['write'] = self.write
return basic
@@ -115,12 +117,12 @@ def execute(self, data):
# If action has extra usage notes, probably has parameters
if actions[action].usage:
- ausage = '%s %s %s' % (self.function.name, actionname, actions[action].usage)
+ ausage = ['%s %s %s' % (self.function.name, actionname, actions[action].usage)]
# Otherwise we can link directly to the call
else:
call = '/%s/%s' % (self.function.name, actionname)
- ausage = '<a href="%s">%s %s</a>' % (call, self.function.name, actionname)
+ ausage = ['%s %s' % (self.function.name, actionname), '%s %s' % (self.function.name, actionname)]
usage.append(ausage)
View
30 functions/list.py
@@ -126,11 +126,13 @@ def execute(self, data):
items = l.get_all()
if not len(items):
- return function.response(function.STATE_FAILURE, 'No items in list "%s" (<a href="/list/list">List all</a>)' % lstkey)
+ data = []
+ data.append(["List all lists", 'list list'])
+ return function.response(function.STATE_FAILURE, 'No items in list "%s"' % lstkey, data)
data = []
for item in items:
- data.append([item['id'], item['item']])
+ data.append([item['item'], None, {'Delete': 'list remove %s %s' % (lstkey, item['id'])}])
return function.response(function.STATE_SUCCESS, 'List "%s" contents' % lstkey, data)
@@ -141,7 +143,7 @@ def execute(self, data):
lists = self.function.get_all_lists()
data = []
for ls in lists:
- data.append('<a href="/list/view/%s">%s</a>' % (ls['listname'], ls['listname']))
+ data.append([ls['listname'], 'list view %s' % ls['listname']])
return function.response(function.STATE_SUCCESS, 'Lists available', data)
@@ -160,7 +162,12 @@ def execute(self, data):
l = lstobj(self.function, lstkey)
l.add_new(newitem)
- return function.response(function.STATE_SUCCESS, 'Adding "%s" to "<a href="/list/view/%s">%s</a>"' % (newitem, lstkey, lstkey))
+ data = []
+ data.append(["View list", "list view %s" % lstkey])
+ resp = function.response(function.STATE_SUCCESS, 'Adding "%s" to list "%s"' % (newitem, lstkey))
+ resp.data = data
+ resp.write = 1
+ return resp
def undo(self, list):
pass
@@ -178,19 +185,26 @@ def execute(self, data):
items = l.get_all()
itemdata = None
- data = []
for i in items:
- data.append('[%s] %s' % (i['id'], i['item']))
if i['id'] == int(itemid):
itemdata = i
break
+ data = []
+ data.append(["View list", "list view %s" % lstkey])
+
if not itemdata:
- return function.response(function.STATE_FAILURE, 'No item to delete in "<a href="/list/view/%s">%s</a>"' % (lstkey, lstkey), data)
+ resp = function.response(function.STATE_FAILURE, 'No item to delete in list "%s"' % lstkey)
+ resp.data = data
+ resp.write = 1
+ return resp
l.delete_item(itemid)
- return function.response(function.STATE_SUCCESS, 'Deleting "%s" from "<a href="/list/view/%s">%s</a>"' % (itemdata['item'], lstkey, lstkey))
+ resp = function.response(function.STATE_SUCCESS, 'Deleting "%s" from "%s"' % (itemdata['item'], lstkey))
+ resp.data = data
+ resp.write = 1
+ return resp
def undo(self, list):
pass
View
15 functions/server.py
@@ -52,18 +52,19 @@ def execute(self):
class action_connect(kernel.action.action):
def _get_weather(self):
- weather = self.function.kernel.call('list', 'view', ['#weather'])
- return weather.data
+ return self.function.kernel.call('list', 'view', ['#weather'])
def execute(self, data):
user = self.function.kernel.getConfig('username')
date = datetime.datetime.today().strftime('%A %B %d, %Y')
welcome = 'Hi %s. Today is %s' % (user, date)
- data = self._get_weather()
+ weather = self._get_weather()
+ data = weather.data
+
today = self.function.kernel.call('list', 'view', ['today'])
if today.state == function.STATE_SUCCESS:
- data.append('==== Today ====')
+ data.append(['==== Today ====', 'list view today'])
data += today.data
return function.response(function.STATE_SUCCESS, welcome, data)
@@ -102,4 +103,8 @@ def execute(self, data):
stats.append('Python version: %s' % pyver)
stats.append('Database version: %s' % dbver)
- return function.response(function.STATE_SUCCESS, str, stats)
+ advdata = []
+ for stat in stats:
+ advdata.append([stat])
+
+ return function.response(function.STATE_SUCCESS, str, advdata)
View
2 interfaces/http.py
@@ -60,7 +60,7 @@ def do_GET(self):
result = self.server.kernel.call(function, action, data)
- resultbasic = result.returnBasic()
+ resultbasic = result.returnAdvanced()
httpcode = result.getHTTPCode()
output = json.dumps(resultbasic)

0 comments on commit 4600ecf

Please sign in to comment.