better escaping for path parameters

This commit is contained in:
Russell Horton 2012-12-20 14:25:47 -08:00
parent 93c0134da9
commit 29cec989b2
2 changed files with 21 additions and 14 deletions

View File

@ -81,16 +81,16 @@ class ApiClient:
return data
def toPathValue(self, obj):
"""Serialize a list to a CSV string, if necessary.
"""Convert a string or object to a path-friendly value
Args:
obj -- data object to be serialized
obj -- object or string value
Returns:
string -- json serialization of object
string -- quoted value
"""
if type(obj) == list:
return ','.join(obj)
return urllib.quote(','.join(obj))
else:
return obj
return urllib.quote(str(obj))
def sanitizeForSerialization(self, obj):
"""Dump an object into JSON for POSTing."""
@ -139,12 +139,12 @@ class ApiClient:
subClass = match.group(1)
return [self.deserialize(subObj, subClass) for subObj in obj]
if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str']):
if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']):
objClass = eval(objClass)
else: # not a native type, must be model class
objClass = eval(objClass + '.' + objClass)
if objClass in [str, int, long, float, bool]:
if objClass in [int, long, float, dict, list, str, bool]:
return objClass(obj)
elif objClass == datetime:
# Server will always return a time stamp in UTC, but with
@ -165,6 +165,9 @@ class ApiClient:
except UnicodeEncodeError:
value = unicode(value)
setattr(instance, attr, value)
elif (attrType == 'datetime'):
setattr(instance, attr, datetime.datetime.strptime(value[:-5],
"%Y-%m-%dT%H:%M:%S.%f"))
elif 'list[' in attrType:
match = re.match('list\[(.*)\]', attrType)
subClass = match.group(1)
@ -197,3 +200,4 @@ class MethodRequest(urllib2.Request):
def get_method(self):
return getattr(self, 'method', urllib2.Request.get_method(self))

View File

@ -84,16 +84,16 @@ class ApiClient:
return data
def toPathValue(self, obj):
"""Serialize a list to a CSV string, if necessary.
"""Convert a string or object to a path-friendly value
Args:
obj -- data object to be serialized
obj -- object or string value
Returns:
string -- json serialization of object
string -- quoted value
"""
if type(obj) == list:
return ','.join(obj)
return urllib.parse.quote(','.join(obj))
else:
return obj
return urllib.parse.quote(str(obj))
def sanitizeForSerialization(self, obj):
"""Dump an object into JSON for POSTing."""
@ -133,12 +133,12 @@ class ApiClient:
subClass = match.group(1)
return [self.deserialize(subObj, subClass) for subObj in obj]
if (objClass in ['int', 'float', 'dict', 'list', 'str']):
if (objClass in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']):
objClass = eval(objClass)
else: # not a native type, must be model class
objClass = eval(objClass + '.' + objClass)
if objClass in [str, int, float, bool]:
if objClass in [int, float, dict, list, str, bool]:
return objClass(obj)
elif objClass == datetime:
# Server will always return a time stamp in UTC, but with
@ -160,6 +160,9 @@ class ApiClient:
except UnicodeEncodeError:
value = unicode(value)
setattr(instance, attr, value)
elif (attrType == 'datetime'):
setattr(instance, attr, datetime.datetime.strptime(value[:-5],
"%Y-%m-%dT%H:%M:%S.%f"))
elif 'list[' in attrType:
match = re.match('list\[(.*)\]', attrType)
subClass = match.group(1)