forked from loafle/openapi-generator-original
Update sanitizeForSerialization to sanitize parameters to Python client
* Refactor santizeForSerialzation to static method. * Sanitize header/query/form/body parameters. * Sanitize datetime.datetime and datetime.date to iso8601 format string. * Sanitize swagger model to dict.
This commit is contained in:
parent
92e5574ec1
commit
f085860eb1
@ -19,7 +19,7 @@ import string
|
|||||||
from models import *
|
from models import *
|
||||||
|
|
||||||
|
|
||||||
class ApiClient:
|
class ApiClient(object):
|
||||||
"""Generic API client for Swagger client library builds
|
"""Generic API client for Swagger client library builds
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
@ -41,13 +41,13 @@ class ApiClient:
|
|||||||
headers = {}
|
headers = {}
|
||||||
if headerParams:
|
if headerParams:
|
||||||
for param, value in headerParams.iteritems():
|
for param, value in headerParams.iteritems():
|
||||||
headers[param] = value
|
headers[param] = ApiClient.sanitizeForSerialization(value)
|
||||||
|
|
||||||
if self.headerName:
|
if self.headerName:
|
||||||
headers[self.headerName] = self.headerValue
|
headers[self.headerName] = ApiClient.sanitizeForSerialization(self.headerValue)
|
||||||
|
|
||||||
if self.cookie:
|
if self.cookie:
|
||||||
headers['Cookie'] = self.cookie
|
headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)
|
||||||
|
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
@ -55,8 +55,8 @@ class ApiClient:
|
|||||||
# Need to remove None values, these should not be sent
|
# Need to remove None values, these should not be sent
|
||||||
sentQueryParams = {}
|
sentQueryParams = {}
|
||||||
for param, value in queryParams.items():
|
for param, value in queryParams.items():
|
||||||
if value != None:
|
if value is not None:
|
||||||
sentQueryParams[param] = value
|
sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
|
||||||
url = url + '?' + urllib.urlencode(sentQueryParams)
|
url = url + '?' + urllib.urlencode(sentQueryParams)
|
||||||
|
|
||||||
if method in ['GET']:
|
if method in ['GET']:
|
||||||
@ -65,7 +65,7 @@ class ApiClient:
|
|||||||
|
|
||||||
elif method in ['POST', 'PUT', 'DELETE']:
|
elif method in ['POST', 'PUT', 'DELETE']:
|
||||||
if postData:
|
if postData:
|
||||||
postData = self.sanitizeForSerialization(postData)
|
postData = ApiClient.sanitizeForSerialization(postData)
|
||||||
if 'Content-type' not in headers:
|
if 'Content-type' not in headers:
|
||||||
headers['Content-type'] = 'application/json'
|
headers['Content-type'] = 'application/json'
|
||||||
data = json.dumps(postData)
|
data = json.dumps(postData)
|
||||||
@ -107,34 +107,34 @@ class ApiClient:
|
|||||||
else:
|
else:
|
||||||
return urllib.quote(str(obj))
|
return urllib.quote(str(obj))
|
||||||
|
|
||||||
def sanitizeForSerialization(self, obj):
|
@staticmethod
|
||||||
"""Dump an object into JSON for POSTing."""
|
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
|
return None
|
||||||
elif type(obj) in [str, int, long, float, bool]:
|
elif isinstance(obj, (str, int, long, float, bool, file)):
|
||||||
return obj
|
return obj
|
||||||
elif type(obj) == list:
|
elif isinstance(obj, list):
|
||||||
return [self.sanitizeForSerialization(subObj) for subObj in obj]
|
return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj]
|
||||||
elif type(obj) == datetime.datetime:
|
elif isinstance(obj, (datetime.datetime, datetime.date)):
|
||||||
return obj.isoformat()
|
return obj.isoformat()
|
||||||
else:
|
else:
|
||||||
if type(obj) == dict:
|
if isinstance(obj, dict):
|
||||||
objDict = obj
|
objDict = obj
|
||||||
else:
|
else:
|
||||||
objDict = obj.__dict__
|
objDict = obj.__dict__
|
||||||
return {key: self.sanitizeForSerialization(val)
|
return {key: ApiClient.sanitizeForSerialization(val)
|
||||||
for (key, val) in objDict.iteritems()
|
for (key, val) in objDict.iteritems()
|
||||||
if key != 'swaggerTypes'}
|
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__)
|
|
||||||
|
|
||||||
def buildMultipartFormData(self, postData, files):
|
def buildMultipartFormData(self, postData, files):
|
||||||
def escape_quotes(s):
|
def escape_quotes(s):
|
||||||
@ -243,4 +243,4 @@ class MethodRequest(urllib2.Request):
|
|||||||
return urllib2.Request.__init__(self, *args, **kwargs)
|
return urllib2.Request.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
def get_method(self):
|
def get_method(self):
|
||||||
return getattr(self, 'method', urllib2.Request.get_method(self))
|
return getattr(self, 'method', urllib2.Request.get_method(self))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user