Merge pull request #531 from geekerzp/develop_2.0_python_datetime

Update santizeForSerialization to normalize parameters for Python client
This commit is contained in:
Tony Tam 2015-03-21 08:18:00 -07:00
commit 38204b2237

View File

@ -19,7 +19,7 @@ import string
from models import *
class ApiClient:
class ApiClient(object):
"""Generic API client for Swagger client library builds
Attributes:
@ -41,13 +41,13 @@ class ApiClient:
headers = {}
if headerParams:
for param, value in headerParams.iteritems():
headers[param] = value
headers[param] = ApiClient.sanitizeForSerialization(value)
if self.headerName:
headers[self.headerName] = self.headerValue
headers[self.headerName] = ApiClient.sanitizeForSerialization(self.headerValue)
if self.cookie:
headers['Cookie'] = self.cookie
headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)
data = None
@ -55,8 +55,8 @@ class ApiClient:
# Need to remove None values, these should not be sent
sentQueryParams = {}
for param, value in queryParams.items():
if value != None:
sentQueryParams[param] = value
if value is not None:
sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
url = url + '?' + urllib.urlencode(sentQueryParams)
if method in ['GET']:
@ -65,7 +65,7 @@ class ApiClient:
elif method in ['POST', 'PUT', 'DELETE']:
if postData:
postData = self.sanitizeForSerialization(postData)
postData = ApiClient.sanitizeForSerialization(postData)
if 'Content-type' not in headers:
headers['Content-type'] = 'application/json'
data = json.dumps(postData)
@ -107,34 +107,34 @@ class ApiClient:
else:
return urllib.quote(str(obj))
def sanitizeForSerialization(self, obj):
"""Dump an object into JSON for POSTing."""
@staticmethod
def sanitizeForSerialization(obj):
"""
Sanitize an object for Request.
if type(obj) == type(None):
If obj is None, return None.
If obj is str, int, long, 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 type(obj) in [str, int, long, float, bool]:
elif isinstance(obj, (str, int, long, float, bool, file)):
return obj
elif type(obj) == list:
return [self.sanitizeForSerialization(subObj) for subObj in obj]
elif type(obj) == datetime.datetime:
elif isinstance(obj, list):
return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj]
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
else:
if type(obj) == dict:
if isinstance(obj, dict):
objDict = obj
else:
objDict = obj.__dict__
return {key: self.sanitizeForSerialization(val)
for (key, val) in objDict.iteritems()
if key != 'swaggerTypes'}
if type(postData) == list:
# Could be a list of objects
if type(postData[0]) in safeToDump:
data = json.dumps(postData)
else:
data = json.dumps([datum.__dict__ for datum in postData])
elif type(postData) not in safeToDump:
data = json.dumps(postData.__dict__)
return {key: ApiClient.sanitizeForSerialization(val)
for (key, val) in objDict.iteritems()
if key != 'swaggerTypes'}
def buildMultipartFormData(self, postData, files):
def escape_quotes(s):
@ -243,4 +243,4 @@ class MethodRequest(urllib2.Request):
return urllib2.Request.__init__(self, *args, **kwargs)
def get_method(self):
return getattr(self, 'method', urllib2.Request.get_method(self))
return getattr(self, 'method', urllib2.Request.get_method(self))