mirror of
https://github.com/xcat2/confluent.git
synced 2025-01-15 12:17:47 +00:00
Rework http json in the following ways:
-defer to document completion (all at once) for more straightforward code -data only gets into arrays when there is a multitude -at least for now, 'pretty print' the dumps in http interface
This commit is contained in:
parent
74da3d64e8
commit
67642b7e19
@ -287,55 +287,38 @@ def _assemble_json(responses, resource, url):
|
||||
#once and hold on to all the data in memory
|
||||
docomma = False
|
||||
links = {
|
||||
'self': ['{"href":"%s"}' % resource],
|
||||
'self': {"href":resource},
|
||||
}
|
||||
if url == '/':
|
||||
pass
|
||||
elif resource[-1] == '/':
|
||||
links['collection'] = ['{"href":"%s"}' % '../']
|
||||
links['collection'] = {"href":"../"}
|
||||
else:
|
||||
links['collection'] = ['{"href":"%s"}' % './']
|
||||
yield '{'
|
||||
hadrsp = False
|
||||
links['collection'] = {"href":"./"}
|
||||
rspdata = {}
|
||||
for rsp in responses:
|
||||
if isinstance(rsp, confluent.messages.LinkRelation):
|
||||
haldata = rsp.json_hal()
|
||||
haldata = rsp.raw_rel()
|
||||
for hk in haldata.iterkeys():
|
||||
if hk in links:
|
||||
links[hk].append(haldata[hk])
|
||||
if isinstance(links[hk], list):
|
||||
links[hk].append(haldata[hk])
|
||||
else:
|
||||
links[hk] = [ links[hk], haldata[hk] ]
|
||||
else:
|
||||
links[hk] = [haldata[hk]]
|
||||
continue
|
||||
hadrsp = True
|
||||
if docomma:
|
||||
yield ','
|
||||
links[hk] = haldata[hk]
|
||||
else:
|
||||
docomma = True
|
||||
yield rsp.json()
|
||||
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 ','
|
||||
rsp = rsp.rawdata()
|
||||
for dk in rsp.iterkeys():
|
||||
if dk in rspdata:
|
||||
if isinstance(rspdata[dk], list):
|
||||
rspdata[dk].append(rsp[dk])
|
||||
else:
|
||||
rspdata[dk] = [ rspdata[dk], rsp[dk] ]
|
||||
else:
|
||||
docomma = True
|
||||
yield lk
|
||||
yield ']'
|
||||
yield '}'
|
||||
yield '}'
|
||||
rspdata[dk] = rsp[dk]
|
||||
rspdata["_links"] = links
|
||||
yield json.dumps(rspdata, sort_keys=True, indent=4)
|
||||
|
||||
|
||||
def serve():
|
||||
|
@ -18,6 +18,12 @@ class ConfluentMessage(object):
|
||||
jsonsnippet = json.dumps(self.kvpairs, separators=(',', ':'))[1:-1]
|
||||
return jsonsnippet
|
||||
|
||||
def rawdata(self):
|
||||
"""Return pythonic representation of the response.
|
||||
|
||||
Used by httpapi while assembling data prior to json serialization"""
|
||||
return self.kvpairs
|
||||
|
||||
def strip_node(self, node):
|
||||
self.kvpairs = self.kvpairs[node]
|
||||
|
||||
@ -74,9 +80,23 @@ class ConfluentChoiceMessage(ConfluentMessage):
|
||||
|
||||
class LinkRelation(ConfluentMessage):
|
||||
def json_hal(self):
|
||||
"""Provide json_hal style representation of the relation.
|
||||
|
||||
This currently only makes sense for the socket api.
|
||||
"""
|
||||
return {self.rel: '{ "href": "%s" }' % self.href }
|
||||
|
||||
def raw_rel(self):
|
||||
"""Provide python structure of the relation.
|
||||
|
||||
This currently is only sensible to consume from httpapi.
|
||||
"""
|
||||
return { self.rel: { "href": self.href }}
|
||||
|
||||
def html(self):
|
||||
"""Provide an html representation of the link relation.
|
||||
|
||||
This is used by the API explorer aspect of httpapi"""
|
||||
return '<a href="%s" rel="%s">%s</a>' % (self.href, self.rel, self.href)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user