From f085860eb1a53742c3686c44714d4c17f70fa874 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Sat, 21 Mar 2015 14:58:32 +0800 Subject: [PATCH] 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. --- .../main/resources/python/swagger.mustache | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 5a5a7058bd..e1210a7e26 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -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)) \ No newline at end of file + return getattr(self, 'method', urllib2.Request.get_method(self))