mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-09-03 13:54:57 +00:00
[pysearpc] Support json return type. (#33)
* [pysearpc] Support json return type. * Added tests for json return type
This commit is contained in:
@@ -85,6 +85,19 @@ def _fret_objlist(ret_str):
|
|||||||
|
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
def _fret_json(ret_str):
|
||||||
|
try:
|
||||||
|
dicts = json.loads(ret_str)
|
||||||
|
except:
|
||||||
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
|
if dicts.has_key('err_code'):
|
||||||
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
|
if dicts['ret']:
|
||||||
|
return dicts['ret']
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def searpc_func(ret_type, param_types):
|
def searpc_func(ret_type, param_types):
|
||||||
|
|
||||||
@@ -101,6 +114,8 @@ def searpc_func(ret_type, param_types):
|
|||||||
fret = _fret_int
|
fret = _fret_int
|
||||||
elif ret_type == "string":
|
elif ret_type == "string":
|
||||||
fret = _fret_string
|
fret = _fret_string
|
||||||
|
elif ret_type == "json":
|
||||||
|
fret = _fret_json
|
||||||
else:
|
else:
|
||||||
raise SearpcError('Invial return type')
|
raise SearpcError('Invial return type')
|
||||||
|
|
||||||
|
@@ -22,6 +22,10 @@ def init_server():
|
|||||||
searpc_server.create_service(SVCNAME)
|
searpc_server.create_service(SVCNAME)
|
||||||
searpc_server.register_function(SVCNAME, add, 'add')
|
searpc_server.register_function(SVCNAME, add, 'add')
|
||||||
searpc_server.register_function(SVCNAME, mul, 'multi')
|
searpc_server.register_function(SVCNAME, mul, 'multi')
|
||||||
|
searpc_server.register_function(SVCNAME, json_func, 'json_func')
|
||||||
|
|
||||||
|
def json_func(a, b):
|
||||||
|
return {'a': a, 'b': b}
|
||||||
|
|
||||||
|
|
||||||
class DummyTransport(SearpcTransport):
|
class DummyTransport(SearpcTransport):
|
||||||
@@ -40,6 +44,10 @@ class RpcMixin(object):
|
|||||||
def multi(self, x, y):
|
def multi(self, x, y):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@searpc_func("json", ["string", "int"])
|
||||||
|
def json_func(self, x, y):
|
||||||
|
pass
|
||||||
|
|
||||||
class DummyRpcClient(SearpcClient, RpcMixin):
|
class DummyRpcClient(SearpcClient, RpcMixin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.transport = DummyTransport()
|
self.transport = DummyTransport()
|
||||||
@@ -86,6 +94,9 @@ class SearpcTest(unittest.TestCase):
|
|||||||
v = client.multi('abc', 2)
|
v = client.multi('abc', 2)
|
||||||
self.assertEqual(v, 'abcabc')
|
self.assertEqual(v, 'abcabc')
|
||||||
|
|
||||||
|
v = client.json_func(1, 2)
|
||||||
|
self.assertEqual(v, json_func(1, 2))
|
||||||
|
|
||||||
def setup_logging(level=logging.INFO):
|
def setup_logging(level=logging.INFO):
|
||||||
kw = {
|
kw = {
|
||||||
# 'format': '[%(asctime)s][%(pathname)s]: %(message)s',
|
# 'format': '[%(asctime)s][%(pathname)s]: %(message)s',
|
||||||
|
Reference in New Issue
Block a user