add datetime conversion to python

This commit is contained in:
Russell Horton 2012-09-10 01:50:54 -07:00
parent 5189a85966
commit 85a4aacefa
2 changed files with 22 additions and 17 deletions

View File

@ -11,6 +11,7 @@ import urllib
import urllib2 import urllib2
import httplib import httplib
import json import json
import datetime
from models import * from models import *
@ -43,21 +44,24 @@ class ApiClient:
data = None data = None
if method == 'GET':
if queryParams: if queryParams:
# Need to remove None values, these should not be sent
sentQueryParams = {} sentQueryParams = {}
for param, value in queryParams.iteritems(): for param, value in queryParams.items():
if value != None: if value != None:
sentQueryParams[param] = value sentQueryParams[param] = value
url = url + '?' + urllib.urlencode(sentQueryParams) url = url + '?' + urllib.urlencode(sentQueryParams)
if method in ['POST', 'PUT', 'DELETE']: elif method in ['POST', 'PUT', 'DELETE']:
if postData: if postData:
headers['Content-type'] = 'application/json' headers['Content-type'] = 'application/json'
data = self.sanitizeForSerialization(postData) data = self.sanitizeForSerialization(postData)
data = json.dumps(data) data = json.dumps(data)
elif method != 'GET': else:
raise Exception('Method ' + method + ' is not recognized.') raise Exception('Method ' + method + ' is not recognized.')
request = MethodRequest(method=method, url=url, headers=headers, request = MethodRequest(method=method, url=url, headers=headers,
@ -90,15 +94,15 @@ class ApiClient:
def sanitizeForSerialization(self, obj): def sanitizeForSerialization(self, obj):
"""Dump an object into JSON for POSTing.""" """Dump an object into JSON for POSTing."""
print 'obj:', obj
print 'type:', type(obj)
if not obj: if not obj:
return None return None
if type(obj) in [str, int, long, float, bool]: elif type(obj) in [str, int, long, float, bool]:
return obj return obj
elif type(obj) == list: elif type(obj) == list:
return [self.sanitizeForSerialization(subObj) for subObj in obj] return [self.sanitizeForSerialization(subObj) for subObj in obj]
elif type(obj) == datetime.datetime:
return obj.isoformat()
else: else:
if type(obj) == dict: if type(obj) == dict:
objDict = obj objDict = obj
@ -129,16 +133,12 @@ class ApiClient:
# Have to accept objClass as string or actual type. Type could be a # Have to accept objClass as string or actual type. Type could be a
# native Python type, or one of the model classes. # native Python type, or one of the model classes.
# print obj, objClass
if type(objClass) == str: if type(objClass) == str:
if 'list[' in objClass: if 'list[' in objClass:
match = re.match('list\[(.*)\]', objClass) match = re.match('list\[(.*)\]', objClass)
subClass = match.group(1) subClass = match.group(1)
return [self.deserialize(subObj, subClass) for subObj in obj] return [self.deserialize(subObj, subClass) for subObj in obj]
# print objClass + ' is str'
if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str']): if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str']):
objClass = eval(objClass) objClass = eval(objClass)
else: # not a native type, must be model class else: # not a native type, must be model class
@ -146,11 +146,16 @@ class ApiClient:
if objClass in [str, int, long, float, bool]: if objClass in [str, int, long, float, bool]:
return objClass(obj) 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() instance = objClass()
for attr, attrType in instance.swaggerTypes.iteritems(): for attr, attrType in instance.swaggerTypes.iteritems():
# print 'attr:', attr, 'obj:', obj
if attr in obj: if attr in obj:
value = obj[attr] value = obj[attr]
if attrType in ['str', 'int', 'long', 'float', 'bool']: if attrType in ['str', 'int', 'long', 'float', 'bool']:

View File

@ -104,7 +104,7 @@ class BasicPythonGenerator extends BasicGenerator {
"Double" -> "float", "Double" -> "float",
"Array" -> "list", "Array" -> "list",
"Boolean" -> "bool", "Boolean" -> "bool",
"Date" -> "str", "Date" -> "datetime",
"string" -> "str" "string" -> "str"
) )