forked from loafle/openapi-generator-original
add datetime conversion to python
This commit is contained in:
parent
5189a85966
commit
85a4aacefa
@ -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 method == 'GET':
|
||||
|
||||
if queryParams:
|
||||
# Need to remove None values, these should not be sent
|
||||
sentQueryParams = {}
|
||||
for param, value in queryParams.iteritems():
|
||||
for param, value in queryParams.items():
|
||||
if value != None:
|
||||
sentQueryParams[param] = value
|
||||
url = url + '?' + urllib.urlencode(sentQueryParams)
|
||||
|
||||
if method in ['POST', 'PUT', 'DELETE']:
|
||||
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']:
|
||||
|
@ -104,7 +104,7 @@ class BasicPythonGenerator extends BasicGenerator {
|
||||
"Double" -> "float",
|
||||
"Array" -> "list",
|
||||
"Boolean" -> "bool",
|
||||
"Date" -> "str",
|
||||
"Date" -> "datetime",
|
||||
"string" -> "str"
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user