2
0
mirror of https://github.com/xcat2/confluent.git synced 2025-08-22 11:10:25 +00:00

Add relation information to json output

This commit is contained in:
Jarrod Johnson
2013-11-03 17:07:17 -05:00
parent 09bd9b3131
commit 559b00b8e8
2 changed files with 45 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ import Cookie
import confluent.auth as auth
import confluent.consoleserver as consoleserver
import confluent.exceptions as exc
import confluent.messages
import confluent.pluginapi as pluginapi
import confluent.util as util
import eventlet
@@ -262,15 +263,52 @@ def _assemble_html(responses, resource, querydict):
def _assemble_json(responses, resource):
yield '['
docomma = False
links = {
'self': ['{"href"="%s"' % resource],
'collection': ['{"href"="%s"' % '../'],
}
yield '{'
hadrsp = False
for rsp in responses:
if isinstance(rsp, confluent.messages.LinkRelation):
haldata = rsp.json_hal()
for hk in haldata.iterkeys():
if hk in links:
links[hk].append(haldata[hk])
else:
links[hk] = [haldata[hk]]
continue
hadrsp = True
if docomma:
yield ','
else:
docomma = True
yield rsp.json()
yield ']'
docomma = False
if hadrsp:
yield ','
yield '"_links": {'
groupcomma = False
for link in links.iterkeys():
if groupcomma:
yield ','
else:
groupcomma = True
yield json.dumps(link) + ":"
if len(links[link]) == 1:
yield links[link][0]
else:
yield '['
for lk in links[link]:
if docomma:
yield ','
else:
docomma = True
yield lk
yield ']'
yield '}'
yield '}'
def serve():

View File

@@ -15,7 +15,8 @@ class ConfluentMessage(object):
def json(self):
# This will create the canonical json representation of this message
return json.dumps(self.kvpairs, separators=(',', ':'))
jsonsnippet = json.dumps(self.kvpairs, separators=(',', ':'))[1:-1]
return jsonsnippet
def strip_node(self, node):
self.kvpairs = self.kvpairs[node]
@@ -61,6 +62,9 @@ class ConfluentChoiceMessage(ConfluentMessage):
return snippet
class LinkRelation(ConfluentMessage):
def json_hal(self):
return {self.rel: '{ "href": "%s" }' % self.href }
def html(self):
return '<a href="%s" rel="%s">%s</a>' % (self.href, self.rel, self.href)