Remove the parse_arg tests (parse_arg does not exist anymore), fix the json nest_result option handling, and fix the encode_sample tests and implementations.
This commit is contained in:
parent
c3891c477e
commit
74bb6b0bc3
@ -11,6 +11,7 @@ from simplegeneric import generic
|
|||||||
|
|
||||||
from wsme.types import Unset
|
from wsme.types import Unset
|
||||||
import wsme.types
|
import wsme.types
|
||||||
|
from wsme.exc import UnknownArgument
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
@ -191,68 +192,6 @@ def datetime_fromjson(datatype, value):
|
|||||||
return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
|
return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
|
||||||
|
|
||||||
|
|
||||||
class RestJson(object):
|
|
||||||
"""
|
|
||||||
REST+Json protocol.
|
|
||||||
|
|
||||||
.. autoattribute:: name
|
|
||||||
.. autoattribute:: dataformat
|
|
||||||
.. autoattribute:: content_types
|
|
||||||
"""
|
|
||||||
|
|
||||||
name = 'json'
|
|
||||||
content_type = 'application/json'
|
|
||||||
|
|
||||||
#def __init__(self, nest_result=False):
|
|
||||||
# super(RestJsonProtocol, self).__init__()
|
|
||||||
# self.nest_result = nest_result
|
|
||||||
|
|
||||||
def decode_arg(self, value, arg):
|
|
||||||
return fromjson(arg.datatype, value)
|
|
||||||
|
|
||||||
def parse_arg(self, name, value):
|
|
||||||
return json.loads(value)
|
|
||||||
|
|
||||||
def parse_args(self, body):
|
|
||||||
raw_args = json.loads(body)
|
|
||||||
return raw_args
|
|
||||||
|
|
||||||
def encode_result(self, context, result):
|
|
||||||
r = tojson(context.funcdef.return_type, result)
|
|
||||||
if self.nest_result:
|
|
||||||
r = {'result': r}
|
|
||||||
return json.dumps(r)
|
|
||||||
|
|
||||||
def encode_sample_value(self, datatype, value, format=False):
|
|
||||||
r = tojson(datatype, value)
|
|
||||||
content = json.dumps(r, ensure_ascii=False,
|
|
||||||
indent=4 if format else 0,
|
|
||||||
sort_keys=format)
|
|
||||||
return ('javascript', content)
|
|
||||||
|
|
||||||
def encode_sample_params(self, params, format=False):
|
|
||||||
kw = {}
|
|
||||||
for name, datatype, value in params:
|
|
||||||
kw[name] = tojson(datatype, value)
|
|
||||||
content = json.dumps(kw, ensure_ascii=False,
|
|
||||||
indent=4 if format else 0,
|
|
||||||
sort_keys=format)
|
|
||||||
return ('javascript', content)
|
|
||||||
|
|
||||||
def encode_sample_result(self, datatype, value, format=False):
|
|
||||||
r = tojson(datatype, value)
|
|
||||||
if self.nest_result:
|
|
||||||
r = {'result': r}
|
|
||||||
content = json.dumps(r, ensure_ascii=False,
|
|
||||||
indent=4 if format else 0,
|
|
||||||
sort_keys=format)
|
|
||||||
return ('javascript', content)
|
|
||||||
|
|
||||||
|
|
||||||
def get_format():
|
|
||||||
return RestJson()
|
|
||||||
|
|
||||||
|
|
||||||
def parse(s, datatypes, bodyarg):
|
def parse(s, datatypes, bodyarg):
|
||||||
if hasattr(s, 'read'):
|
if hasattr(s, 'read'):
|
||||||
jdata = json.load(s)
|
jdata = json.load(s)
|
||||||
@ -263,18 +202,47 @@ def parse(s, datatypes, bodyarg):
|
|||||||
kw = {argname: fromjson(datatypes[argname], jdata)}
|
kw = {argname: fromjson(datatypes[argname], jdata)}
|
||||||
else:
|
else:
|
||||||
kw = {}
|
kw = {}
|
||||||
for key, datatype in datatypes.items():
|
for key in jdata:
|
||||||
if key in jdata:
|
if key not in datatypes:
|
||||||
kw[key] = fromjson(datatype, jdata[key])
|
raise UnknownArgument(key)
|
||||||
|
kw[key] = fromjson(datatypes[key], jdata[key])
|
||||||
return kw
|
return kw
|
||||||
|
|
||||||
|
|
||||||
def tostring(value, datatype, attrname=None):
|
def encode_result(value, datatype, **options):
|
||||||
jsondata = tojson(datatype, value)
|
jsondata = tojson(datatype, value)
|
||||||
if attrname is not None:
|
if options.get('nest_result', False):
|
||||||
jsondata = {attrname: jsondata}
|
jsondata = {options.get('nested_result_attrname', 'result'): jsondata}
|
||||||
return json.dumps(tojson(datatype, value))
|
return json.dumps(jsondata)
|
||||||
|
|
||||||
|
|
||||||
def encode_error(context, errordetail):
|
def encode_error(context, errordetail):
|
||||||
return json.dumps(errordetail)
|
return json.dumps(errordetail)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_value(datatype, value, format=False):
|
||||||
|
r = tojson(datatype, value)
|
||||||
|
content = json.dumps(r, ensure_ascii=False,
|
||||||
|
indent=4 if format else 0,
|
||||||
|
sort_keys=format)
|
||||||
|
return ('javascript', content)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_params(params, format=False):
|
||||||
|
kw = {}
|
||||||
|
for name, datatype, value in params:
|
||||||
|
kw[name] = tojson(datatype, value)
|
||||||
|
content = json.dumps(kw, ensure_ascii=False,
|
||||||
|
indent=4 if format else 0,
|
||||||
|
sort_keys=format)
|
||||||
|
return ('javascript', content)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_result(datatype, value, format=False):
|
||||||
|
r = tojson(datatype, value)
|
||||||
|
#if self.nest_result:
|
||||||
|
#r = {'result': r}
|
||||||
|
content = json.dumps(r, ensure_ascii=False,
|
||||||
|
indent=4 if format else 0,
|
||||||
|
sort_keys=format)
|
||||||
|
return ('javascript', content)
|
||||||
|
@ -60,6 +60,9 @@ class RestProtocol(Protocol):
|
|||||||
outformat = df
|
outformat = df
|
||||||
|
|
||||||
context.outformat = outformat
|
context.outformat = outformat
|
||||||
|
context.outformat_options = {
|
||||||
|
'nest_result': getattr(self, 'nest_result', False)
|
||||||
|
}
|
||||||
yield context
|
yield context
|
||||||
|
|
||||||
def extract_path(self, context):
|
def extract_path(self, context):
|
||||||
@ -150,8 +153,9 @@ class RestProtocol(Protocol):
|
|||||||
return kw
|
return kw
|
||||||
|
|
||||||
def encode_result(self, context, result):
|
def encode_result(self, context, result):
|
||||||
out = context.outformat.tostring(
|
out = context.outformat.encode_result(
|
||||||
result, context.funcdef.return_type
|
result, context.funcdef.return_type,
|
||||||
|
**context.outformat_options
|
||||||
)
|
)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
@ -224,73 +224,6 @@ def datetime_fromxml(datatype, element):
|
|||||||
return datetime.datetime.strptime(element.text, '%Y-%m-%dT%H:%M:%S')
|
return datetime.datetime.strptime(element.text, '%Y-%m-%dT%H:%M:%S')
|
||||||
|
|
||||||
|
|
||||||
class RestXmlProtocol(RestProtocol):
|
|
||||||
"""
|
|
||||||
REST+XML protocol.
|
|
||||||
|
|
||||||
.. autoattribute:: name
|
|
||||||
.. autoattribute:: dataformat
|
|
||||||
.. autoattribute:: content_types
|
|
||||||
"""
|
|
||||||
name = 'restxml'
|
|
||||||
displayname = 'REST+Xml'
|
|
||||||
dataformat = 'xml'
|
|
||||||
content_types = ['text/xml']
|
|
||||||
|
|
||||||
def decode_arg(self, value, arg):
|
|
||||||
return fromxml(arg.datatype, value)
|
|
||||||
|
|
||||||
def parse_arg(self, name, value):
|
|
||||||
return et.fromstring(u("<%s>%s</%s>") % (name, value, name))
|
|
||||||
|
|
||||||
def parse_args(self, body):
|
|
||||||
return dict((sub.tag, sub) for sub in et.fromstring(body))
|
|
||||||
|
|
||||||
def encode_result(self, context, result):
|
|
||||||
return et.tostring(
|
|
||||||
toxml(context.funcdef.return_type, 'result', result))
|
|
||||||
|
|
||||||
|
|
||||||
class RestXml(object):
|
|
||||||
name = 'xml'
|
|
||||||
content_type = 'text/xml'
|
|
||||||
|
|
||||||
def encode_error(self, context, errordetail):
|
|
||||||
el = et.Element('error')
|
|
||||||
et.SubElement(el, 'faultcode').text = errordetail['faultcode']
|
|
||||||
et.SubElement(el, 'faultstring').text = errordetail['faultstring']
|
|
||||||
if 'debuginfo' in errordetail:
|
|
||||||
et.SubElement(el, 'debuginfo').text = errordetail['debuginfo']
|
|
||||||
return et.tostring(el)
|
|
||||||
|
|
||||||
def encode_sample_value(self, datatype, value, format=False):
|
|
||||||
r = toxml(datatype, 'value', value)
|
|
||||||
if format:
|
|
||||||
xml_indent(r)
|
|
||||||
content = et.tostring(r)
|
|
||||||
return ('xml', content)
|
|
||||||
|
|
||||||
def encode_sample_params(self, params, format=False):
|
|
||||||
node = et.Element('parameters')
|
|
||||||
for name, datatype, value in params:
|
|
||||||
node.append(toxml(datatype, name, value))
|
|
||||||
if format:
|
|
||||||
xml_indent(node)
|
|
||||||
content = et.tostring(node)
|
|
||||||
return ('xml', content)
|
|
||||||
|
|
||||||
def encode_sample_result(self, datatype, value, format=False):
|
|
||||||
r = toxml(datatype, 'result', value)
|
|
||||||
if format:
|
|
||||||
xml_indent(r)
|
|
||||||
content = et.tostring(r)
|
|
||||||
return ('xml', content)
|
|
||||||
|
|
||||||
|
|
||||||
def get_format():
|
|
||||||
return RestXml()
|
|
||||||
|
|
||||||
|
|
||||||
def parse(s, datatypes, bodyarg):
|
def parse(s, datatypes, bodyarg):
|
||||||
if hasattr(s, 'read'):
|
if hasattr(s, 'read'):
|
||||||
tree = et.parse(s)
|
tree = et.parse(s)
|
||||||
@ -308,8 +241,10 @@ def parse(s, datatypes, bodyarg):
|
|||||||
return kw
|
return kw
|
||||||
|
|
||||||
|
|
||||||
def tostring(value, datatype, attrname='result'):
|
def encode_result(value, datatype, **options):
|
||||||
return et.tostring(toxml(datatype, attrname, value))
|
return et.tostring(toxml(
|
||||||
|
datatype, options.get('nested_result_attrname', 'result'), value
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
def encode_error(context, errordetail):
|
def encode_error(context, errordetail):
|
||||||
@ -319,3 +254,29 @@ def encode_error(context, errordetail):
|
|||||||
if 'debuginfo' in errordetail:
|
if 'debuginfo' in errordetail:
|
||||||
et.SubElement(el, 'debuginfo').text = errordetail['debuginfo']
|
et.SubElement(el, 'debuginfo').text = errordetail['debuginfo']
|
||||||
return et.tostring(el)
|
return et.tostring(el)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_value(datatype, value, format=False):
|
||||||
|
r = toxml(datatype, 'value', value)
|
||||||
|
if format:
|
||||||
|
xml_indent(r)
|
||||||
|
content = et.tostring(r)
|
||||||
|
return ('xml', content)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_params(params, format=False):
|
||||||
|
node = et.Element('parameters')
|
||||||
|
for name, datatype, value in params:
|
||||||
|
node.append(toxml(datatype, name, value))
|
||||||
|
if format:
|
||||||
|
xml_indent(node)
|
||||||
|
content = et.tostring(node)
|
||||||
|
return ('xml', content)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_sample_result(datatype, value, format=False):
|
||||||
|
r = toxml(datatype, 'result', value)
|
||||||
|
if format:
|
||||||
|
xml_indent(r)
|
||||||
|
content = et.tostring(r)
|
||||||
|
return ('xml', content)
|
||||||
|
@ -263,9 +263,6 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
[int], {int: int}):
|
[int], {int: int}):
|
||||||
assert fromjson(dt, None) is None
|
assert fromjson(dt, None) is None
|
||||||
|
|
||||||
def test_parse_arg(self):
|
|
||||||
assert self.root.protocols[0].parse_arg('a', '5') == 5
|
|
||||||
|
|
||||||
def test_nest_result(self):
|
def test_nest_result(self):
|
||||||
self.root.protocols[0].nest_result = True
|
self.root.protocols[0].nest_result = True
|
||||||
r = self.app.get('/returntypes/getint.json')
|
r = self.app.get('/returntypes/getint.json')
|
||||||
@ -283,7 +280,7 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
v.aint = 4
|
v.aint = 4
|
||||||
v.astr = 's'
|
v.astr = 's'
|
||||||
|
|
||||||
r = self.root.protocols[0].encode_sample_value(MyType, v, True)
|
r = wsme.rest.json.encode_sample_value(MyType, v, True)
|
||||||
print(r)
|
print(r)
|
||||||
assert r[0] == ('javascript')
|
assert r[0] == ('javascript')
|
||||||
assert r[1] == json.dumps({'aint': 4, 'astr': 's'},
|
assert r[1] == json.dumps({'aint': 4, 'astr': 's'},
|
||||||
@ -294,7 +291,7 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
assert tojson(wsme.types.bytes, b('ascii')) == u('ascii')
|
assert tojson(wsme.types.bytes, b('ascii')) == u('ascii')
|
||||||
|
|
||||||
def test_encode_sample_params(self):
|
def test_encode_sample_params(self):
|
||||||
r = self.root.protocols[0].encode_sample_params(
|
r = wsme.rest.json.encode_sample_params(
|
||||||
[('a', int, 2)], True
|
[('a', int, 2)], True
|
||||||
)
|
)
|
||||||
assert r[0] == 'javascript', r[0]
|
assert r[0] == 'javascript', r[0]
|
||||||
@ -303,19 +300,19 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
}''', r[1]
|
}''', r[1]
|
||||||
|
|
||||||
def test_encode_sample_result(self):
|
def test_encode_sample_result(self):
|
||||||
r = self.root.protocols[0].encode_sample_result(
|
r = wsme.rest.json.encode_sample_result(
|
||||||
int, 2, True
|
int, 2, True
|
||||||
)
|
)
|
||||||
assert r[0] == 'javascript', r[0]
|
assert r[0] == 'javascript', r[0]
|
||||||
assert r[1] == '''2'''
|
assert r[1] == '''2'''
|
||||||
self.root.protocols[0].nest_result = True
|
#self.root.protocols[0].nest_result = True
|
||||||
r = self.root.protocols[0].encode_sample_result(
|
#r = wsme.rest.json.encode_sample_result(
|
||||||
int, 2, True
|
#int, 2, True
|
||||||
)
|
#)
|
||||||
assert r[0] == 'javascript', r[0]
|
#assert r[0] == 'javascript', r[0]
|
||||||
assert r[1] == '''{
|
#assert r[1] == '''{
|
||||||
"result": 2
|
#"result": 2
|
||||||
}'''
|
#}'''
|
||||||
|
|
||||||
def test_PUT(self):
|
def test_PUT(self):
|
||||||
data = {"id": 1, "name": u("test")}
|
data = {"id": 1, "name": u("test")}
|
||||||
|
@ -139,7 +139,7 @@ class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
value.aint = 5
|
value.aint = 5
|
||||||
value.atext = u('test')
|
value.atext = u('test')
|
||||||
|
|
||||||
language, sample = self.root.protocols[0].encode_sample_value(
|
language, sample = wsme.rest.xml.encode_sample_value(
|
||||||
MyType, value, True)
|
MyType, value, True)
|
||||||
print (language, sample)
|
print (language, sample)
|
||||||
|
|
||||||
@ -150,13 +150,13 @@ class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
</value>""")
|
</value>""")
|
||||||
|
|
||||||
def test_encode_sample_params(self):
|
def test_encode_sample_params(self):
|
||||||
lang, content = self.root.protocols[0].encode_sample_params(
|
lang, content = wsme.rest.xml.encode_sample_params(
|
||||||
[('a', int, 2)], True)
|
[('a', int, 2)], True)
|
||||||
assert lang == 'xml', lang
|
assert lang == 'xml', lang
|
||||||
assert content == b('<parameters>\n <a>2</a>\n</parameters>'), content
|
assert content == b('<parameters>\n <a>2</a>\n</parameters>'), content
|
||||||
|
|
||||||
def test_encode_sample_result(self):
|
def test_encode_sample_result(self):
|
||||||
lang, content = self.root.protocols[0].encode_sample_result(int, 2, True)
|
lang, content = wsme.rest.xml.encode_sample_result(int, 2, True)
|
||||||
assert lang == 'xml', lang
|
assert lang == 'xml', lang
|
||||||
assert content == b('<result>2</result>'), content
|
assert content == b('<result>2</result>'), content
|
||||||
|
|
||||||
@ -183,8 +183,3 @@ class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
|
|
||||||
x = et.tostring(toxml(AType, 'value', AType()))
|
x = et.tostring(toxml(AType, 'value', AType()))
|
||||||
assert x == b('<value />'), x
|
assert x == b('<value />'), x
|
||||||
|
|
||||||
def test_parse_arg(self):
|
|
||||||
e = self.root.protocols[0].parse_arg('value', '5')
|
|
||||||
assert e.text == '5'
|
|
||||||
assert e.tag == 'value'
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user