def sanitize_for_serialization(obj):
"""
Sanitize an object for Request.
If obj is None, return None.
If obj is str, int, float, bool, return directly.
If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
If obj is list, santize each element in the list.
If obj is dict, return the dict.
If obj is swagger model, return the properties dict.
"""
if isinstance(obj, type(None)):
return None
elif isinstance(obj, (str, int, float, bool, io.IOBase, tuple)):
return obj
elif isinstance(obj, list):
return [NbClientManager.sanitize_for_serialization(sub_obj) for sub_obj in obj]
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
else:
if isinstance(obj, dict):
obj_dict = obj
else:
# Convert model obj to dict except attributes `swaggerTypes`, `attributeMap`
# and attributes which value is not None.
# Convert attribute name to json key in model definition for
# request.
obj_dict = {obj.attributeMap[key]: val
for key, val in obj.__dict__.items()
if key != 'swaggerTypes' and key != 'attributeMap' and val is not None}
return {key: NbClientManager.sanitize_for_serialization(val)
for (key, val) in obj_dict.items()}
评论列表
文章目录