From 85a4aacefa13e7ae6fa8c9f76b1a5cebb2f9b810 Mon Sep 17 00:00:00 2001 From: Russell Horton Date: Mon, 10 Sep 2012 01:50:54 -0700 Subject: [PATCH] add datetime conversion to python --- src/main/resources/python/swagger.mustache | 37 +++++++++++-------- .../codegen/BasicPythonGenerator.scala | 2 +- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/resources/python/swagger.mustache b/src/main/resources/python/swagger.mustache index 3743acdae88..7b903f5347d 100644 --- a/src/main/resources/python/swagger.mustache +++ b/src/main/resources/python/swagger.mustache @@ -11,6 +11,7 @@ import urllib import urllib2 import httplib import json +import datetime from models import * @@ -43,21 +44,24 @@ class ApiClient: data = None - if queryParams: - sentQueryParams = {} - for param, value in queryParams.iteritems(): - if value != None: - sentQueryParams[param] = value - url = url + '?' + urllib.urlencode(sentQueryParams) + if method == 'GET': - if method in ['POST', 'PUT', 'DELETE']: + if queryParams: + # Need to remove None values, these should not be sent + sentQueryParams = {} + for param, value in queryParams.items(): + if value != None: + sentQueryParams[param] = value + url = url + '?' + urllib.urlencode(sentQueryParams) + + elif method in ['POST', 'PUT', 'DELETE']: if postData: headers['Content-type'] = 'application/json' data = self.sanitizeForSerialization(postData) data = json.dumps(data) - elif method != 'GET': + else: raise Exception('Method ' + method + ' is not recognized.') request = MethodRequest(method=method, url=url, headers=headers, @@ -90,15 +94,15 @@ class ApiClient: def sanitizeForSerialization(self, obj): """Dump an object into JSON for POSTing.""" - print 'obj:', obj - print 'type:', type(obj) if not obj: return None - if type(obj) in [str, int, long, float, bool]: + elif type(obj) in [str, int, long, float, bool]: return obj elif type(obj) == list: return [self.sanitizeForSerialization(subObj) for subObj in obj] + elif type(obj) == datetime.datetime: + return obj.isoformat() else: if type(obj) == dict: objDict = obj @@ -129,16 +133,12 @@ class ApiClient: # Have to accept objClass as string or actual type. Type could be a # native Python type, or one of the model classes. - - # print obj, objClass - if type(objClass) == str: if 'list[' in objClass: match = re.match('list\[(.*)\]', objClass) subClass = match.group(1) return [self.deserialize(subObj, subClass) for subObj in obj] - # print objClass + ' is str' if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str']): objClass = eval(objClass) else: # not a native type, must be model class @@ -146,11 +146,16 @@ class ApiClient: if objClass in [str, int, long, float, bool]: return objClass(obj) + elif objClass == datetime: + # Server will always return a time stamp in UTC, but with + # trailing +0000 indicating no offset from UTC. So don't process + # last 5 characters. + return datetime.datetime.strptime(obj[:-5], + "%Y-%m-%dT%H:%M:%S.%f") instance = objClass() for attr, attrType in instance.swaggerTypes.iteritems(): - # print 'attr:', attr, 'obj:', obj if attr in obj: value = obj[attr] if attrType in ['str', 'int', 'long', 'float', 'bool']: diff --git a/src/main/scala/com/wordnik/swagger/codegen/BasicPythonGenerator.scala b/src/main/scala/com/wordnik/swagger/codegen/BasicPythonGenerator.scala index 2baa205da62..90de831c25f 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/BasicPythonGenerator.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/BasicPythonGenerator.scala @@ -104,7 +104,7 @@ class BasicPythonGenerator extends BasicGenerator { "Double" -> "float", "Array" -> "list", "Boolean" -> "bool", - "Date" -> "str", + "Date" -> "datetime", "string" -> "str" )