Commit 424fdb0b authored by Romain Courteaud's avatar Romain Courteaud

libslap: add callJsonRpcAPI method

Drop GET/POST methods
parent 3a98bda4
......@@ -60,6 +60,42 @@ ALLOWED_JIO_FIELD_LIST = [
"FloatField",
"TextAreaField"]
# https://stackoverflow.com/a/33571117
def _byteify(data, ignore_dicts = False):
if isinstance(data, str):
return data
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.items() # changed to .items() for python 2.7/3
}
# python 3 compatible duck-typing
# if this is a unicode string, return its string representation
if str(type(data)) == "<type 'unicode'>":
return data.encode('utf-8')
# if it's anything else, return it in its original form
return data
def json_loads_byteified(json_text):
"""
Encode string when loading JSON
"""
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
class TempDocument(object):
def __init__(self, **kw):
"""
......@@ -156,21 +192,15 @@ class ConnectionHelper:
return req
def GET(self, path, params=None, headers=None):
req = self.do_request(self.session.get,
path=path,
params=params,
headers=headers)
return req.text.encode('utf-8')
def POST(self, path, params=None, data=None,
content_type='application/x-www-form-urlencoded'):
def callJsonRpcAPI(self, path, data, cert_key=None):
req = self.do_request(requests.post,
path=path,
params=params,
data=data,
headers={'Content-type': content_type})
return req.text.encode('utf-8')
data=json.dumps(data),
headers={'Content-type': 'application/json'},
expect_json_error=True,
cert_key=cert_key)
return json_loads_byteified(req.text)
class HateoasNavigator(object):
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment