Update deserialize response of Python client.

This commit is contained in:
geekerzp 2015-06-29 21:50:19 +08:00
parent 6df6c079ee
commit 2912ee8ae1
10 changed files with 309 additions and 239 deletions

View File

@ -107,7 +107,7 @@ class {{classname}}(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings)
response_type={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings)
{{#returnType}}
return response
{{/returnType}}{{/operation}}

View File

@ -60,7 +60,7 @@ class ApiClient(object):
self.default_headers[header_name] = header_value
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None, response=None, auth_settings=None):
body=None, post_params=None, files=None, response_type=None, auth_settings=None):
# headers parameters
header_params = header_params or {}
@ -97,17 +97,15 @@ class ApiClient(object):
# request url
url = self.host + resource_path
if response == "file":
# perform request and return response
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body, raw=True)
else:
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body)
# perform request and return response
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body)
self.last_response = response_data
# deserialize response data
if response:
return self.deserialize(response_data, response)
if response_type:
return self.deserialize(response_data, response_type)
else:
return None
@ -156,93 +154,66 @@ class ApiClient(object):
return {key: self.sanitize_for_serialization(val)
for key, val in iteritems(obj_dict)}
def deserialize(self, obj, obj_class):
def deserialize(self, response, response_type):
"""
Derialize a JSON string into an object.
Derialize response into an object.
:param obj: string or object to be deserialized
:param obj_class: class literal for deserialzied object, or string of class name
:param response: RESTResponse object to be deserialized
:param response_type: class literal for deserialzied object, or string of class name
:return object: deserialized object
:return: deserialized object
"""
# Have to accept obj_class as string or actual type. Type could be a
# native Python type, or one of the model classes.
if type(obj_class) == str:
if 'list[' in obj_class:
match = re.match('list\[(.*)\]', obj_class)
sub_class = match.group(1)
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
# handle file downloading - save response body into a tmp file and return the instance
if "file" == response_type:
return self.__deserialize_file(response)
if 'dict(' in obj_class:
match = re.match('dict\((.*), (.*)\)', obj_class)
sub_class = match.group(2)
return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)}
# handle file downloading - save response body into a tmp file and return the instance
if "file" == obj_class:
return self.download_file(obj)
if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]:
obj_class = eval(obj_class)
else: # not a native type, must be model class
obj_class = eval('models.' + obj_class)
if obj_class in [int, float, dict, list, str, bool]:
return obj_class(obj)
elif obj_class == object:
return object()
elif obj_class == datetime:
return self.__parse_string_to_datetime(obj)
instance = obj_class()
for attr, attr_type in iteritems(instance.swagger_types):
if obj is not None and instance.attribute_map[attr] in obj and type(obj) in [list, dict]:
value = obj[instance.attribute_map[attr]]
if attr_type in ['str', 'int', 'float', 'bool']:
attr_type = eval(attr_type)
try:
value = attr_type(value)
except UnicodeEncodeError:
value = unicode(value)
except TypeError:
value = value
setattr(instance, attr, value)
elif attr_type == 'datetime':
setattr(instance, attr, self.__parse_string_to_datetime(value))
elif 'list[' in attr_type:
match = re.match('list\[(.*)\]', attr_type)
sub_class = match.group(1)
sub_values = []
if not value:
setattr(instance, attr, None)
else:
for sub_value in value:
sub_values.append(self.deserialize(sub_value, sub_class))
setattr(instance, attr, sub_values)
else:
setattr(instance, attr, self.deserialize(value, attr_type))
return instance
def __parse_string_to_datetime(self, string):
"""
Parse datetime in string to datetime.
The string should be in iso8601 datetime format.
"""
# fetch data from response object
try:
from dateutil.parser import parse
return parse(string)
except ImportError:
return string
data = json.loads(response.data)
except ValueError:
data = response.data
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None, raw=False):
return self.__deserialize(data, response_type)
def __deserialize(self, data, klass):
"""
:param data: dict, list or str
:param klass: class literal, or string of class name
:return: object
"""
if type(klass) == str:
if 'list[' in klass:
sub_kls = re.match('list\[(.*)\]', klass).group(1)
return [self.__deserialize(sub_data, sub_kls) for sub_data in data]
if 'dict(' in klass:
sub_kls = re.match('dict\((.*), (.*)\)', klass).group(2)
return {k: self.__deserialize(v, sub_kls) for k, v in iteritems(data)}
# convert str to class
# for native types
if klass in ['int', 'float', 'str', 'bool', 'datetime', "object"]:
klass = eval(klass)
# for model types
else:
klass = eval('models.' + klass)
if klass in [int, float, str, bool]:
return self.__deserialize_primitive(data, klass)
elif klass == object:
return self.__deserialize_object()
elif klass == datetime:
return self.__deserialize_datatime(data)
else:
return self.__deserialize_model(data, klass)
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
"""
Perform http request using RESTClient.
"""
if method == "GET":
return RESTClient.GET(url, query_params=query_params, headers=headers, raw=raw)
return RESTClient.GET(url, query_params=query_params, headers=headers)
elif method == "HEAD":
return RESTClient.HEAD(url, query_params=query_params, headers=headers)
elif method == "POST":
@ -318,7 +289,7 @@ class ApiClient(object):
else:
raise ValueError('Authentication token must be in `query` or `header`')
def download_file(self, response):
def __deserialize_file(self, response):
"""
Save response body into a file in (the defined) temporary folder, using the filename
from the `Content-Disposition` header if provided, otherwise a random filename.
@ -340,4 +311,78 @@ class ApiClient(object):
return path
def __deserialize_primitive(self, data, klass):
"""
Deserialize string to primitive type
:param data: str
:param klass: class literal
:return: int, float, str, bool
"""
try:
value = klass(data)
except UnicodeEncodeError:
value = unicode(data)
except TypeError:
value = data
return value
def __deserialize_object(self):
"""
Deserialize empty object
"""
return object()
def __deserialize_datatime(self, string):
"""
Deserialize string to datetime.
The string should be in iso8601 datetime format.
:param string: str
:return: datetime
"""
try:
from dateutil.parser import parse
return parse(string)
except ImportError:
return string
def __deserialize_model(self, data, klass):
"""
Deserialize list or dict to model
:param data: dict, list
:param klass: class literal
"""
instance = klass()
for attr, attr_type in iteritems(instance.swagger_types):
if data is not None \
and instance.attribute_map[attr] in data\
and isinstance(data, (list, dict)):
value = data[instance.attribute_map[attr]]
if attr_type in ['str', 'int', 'float', 'bool']:
attr_type = eval(attr_type)
setattr(instance, attr, self.__deserialize_primitive(value, attr_type))
elif attr_type == 'datetime':
setattr(instance, attr, self.__deserialize_datatime(value))
elif 'list[' in attr_type:
if not value:
setattr(instance, attr, None)
else:
sub_kls = re.match('list\[(.*)\]', attr_type).group(1)
setattr(instance, attr, [self.__deserialize(v, sub_kls) for v in value])
else:
setattr(instance, attr, self.__deserialize(value, attr_type))
return instance

View File

@ -75,7 +75,7 @@ class RESTClientObject(object):
return self.pool_manager
def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, raw=False):
body=None, post_params=None):
"""
:param method: http request method
:param url: http request url
@ -125,30 +125,18 @@ class RESTClientObject(object):
headers=headers)
r = RESTResponse(r)
if r.status not in range(200, 206):
raise ApiException(r)
return self.process_response(r, raw)
def process_response(self, response, raw):
if raw:
return response
# In the python 3, the response.data is bytes.
# we need to decode it to string.
if sys.version_info > (3,):
data = response.data.decode('utf8')
else:
data = response.data
try:
resp = json.loads(data)
except ValueError:
resp = data
r.data = r.data.decode('utf8')
return resp
if r.status not in range(200, 206):
raise ApiException(r)
def GET(self, url, headers=None, query_params=None, raw=False):
return self.request("GET", url, headers=headers, query_params=query_params, raw=raw)
return r
def GET(self, url, headers=None, query_params=None):
return self.request("GET", url, headers=headers, query_params=query_params)
def HEAD(self, url, headers=None, query_params=None):
return self.request("HEAD", url, headers=headers, query_params=query_params)

View File

@ -4,7 +4,7 @@ verbosity=2
randomize=true
exe=true
with-coverage=true
cover-package=SwaggerPetstore
cover-package=swagger_petstore
cover-erase=true
[flake8]

View File

@ -60,7 +60,7 @@ class ApiClient(object):
self.default_headers[header_name] = header_value
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None, response=None, auth_settings=None):
body=None, post_params=None, files=None, response_type=None, auth_settings=None):
# headers parameters
header_params = header_params or {}
@ -97,17 +97,15 @@ class ApiClient(object):
# request url
url = self.host + resource_path
if response == "file":
# perform request and return response
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body, raw=True)
else:
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body)
# perform request and return response
response_data = self.request(method, url, query_params=query_params, headers=header_params,
post_params=post_params, body=body)
self.last_response = response_data
# deserialize response data
if response:
return self.deserialize(response_data, response)
if response_type:
return self.deserialize(response_data, response_type)
else:
return None
@ -156,93 +154,66 @@ class ApiClient(object):
return {key: self.sanitize_for_serialization(val)
for key, val in iteritems(obj_dict)}
def deserialize(self, obj, obj_class):
def deserialize(self, response, response_type):
"""
Derialize a JSON string into an object.
Derialize response into an object.
:param obj: string or object to be deserialized
:param obj_class: class literal for deserialzied object, or string of class name
:param response: RESTResponse object to be deserialized
:param response_type: class literal for deserialzied object, or string of class name
:return object: deserialized object
:return: deserialized object
"""
# Have to accept obj_class as string or actual type. Type could be a
# native Python type, or one of the model classes.
if type(obj_class) == str:
if 'list[' in obj_class:
match = re.match('list\[(.*)\]', obj_class)
sub_class = match.group(1)
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
# handle file downloading - save response body into a tmp file and return the instance
if "file" == response_type:
return self.__deserialize_file(response)
if 'dict(' in obj_class:
match = re.match('dict\((.*), (.*)\)', obj_class)
sub_class = match.group(2)
return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)}
# handle file downloading - save response body into a tmp file and return the instance
if "file" == obj_class:
return self.download_file(obj)
if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]:
obj_class = eval(obj_class)
else: # not a native type, must be model class
obj_class = eval('models.' + obj_class)
if obj_class in [int, float, dict, list, str, bool]:
return obj_class(obj)
elif obj_class == object:
return object()
elif obj_class == datetime:
return self.__parse_string_to_datetime(obj)
instance = obj_class()
for attr, attr_type in iteritems(instance.swagger_types):
if obj is not None and instance.attribute_map[attr] in obj and type(obj) in [list, dict]:
value = obj[instance.attribute_map[attr]]
if attr_type in ['str', 'int', 'float', 'bool']:
attr_type = eval(attr_type)
try:
value = attr_type(value)
except UnicodeEncodeError:
value = unicode(value)
except TypeError:
value = value
setattr(instance, attr, value)
elif attr_type == 'datetime':
setattr(instance, attr, self.__parse_string_to_datetime(value))
elif 'list[' in attr_type:
match = re.match('list\[(.*)\]', attr_type)
sub_class = match.group(1)
sub_values = []
if not value:
setattr(instance, attr, None)
else:
for sub_value in value:
sub_values.append(self.deserialize(sub_value, sub_class))
setattr(instance, attr, sub_values)
else:
setattr(instance, attr, self.deserialize(value, attr_type))
return instance
def __parse_string_to_datetime(self, string):
"""
Parse datetime in string to datetime.
The string should be in iso8601 datetime format.
"""
# fetch data from response object
try:
from dateutil.parser import parse
return parse(string)
except ImportError:
return string
data = json.loads(response.data)
except ValueError:
data = response.data
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None, raw=False):
return self.__deserialize(data, response_type)
def __deserialize(self, data, klass):
"""
:param data: dict, list or str
:param klass: class literal, or string of class name
:return: object
"""
if type(klass) == str:
if 'list[' in klass:
sub_kls = re.match('list\[(.*)\]', klass).group(1)
return [self.__deserialize(sub_data, sub_kls) for sub_data in data]
if 'dict(' in klass:
sub_kls = re.match('dict\((.*), (.*)\)', klass).group(2)
return {k: self.__deserialize(v, sub_kls) for k, v in iteritems(data)}
# convert str to class
# for native types
if klass in ['int', 'float', 'str', 'bool', 'datetime', "object"]:
klass = eval(klass)
# for model types
else:
klass = eval('models.' + klass)
if klass in [int, float, str, bool]:
return self.__deserialize_primitive(data, klass)
elif klass == object:
return self.__deserialize_object()
elif klass == datetime:
return self.__deserialize_datatime(data)
else:
return self.__deserialize_model(data, klass)
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
"""
Perform http request using RESTClient.
"""
if method == "GET":
return RESTClient.GET(url, query_params=query_params, headers=headers, raw=raw)
return RESTClient.GET(url, query_params=query_params, headers=headers)
elif method == "HEAD":
return RESTClient.HEAD(url, query_params=query_params, headers=headers)
elif method == "POST":
@ -318,7 +289,7 @@ class ApiClient(object):
else:
raise ValueError('Authentication token must be in `query` or `header`')
def download_file(self, response):
def __deserialize_file(self, response):
"""
Save response body into a file in (the defined) temporary folder, using the filename
from the `Content-Disposition` header if provided, otherwise a random filename.
@ -340,4 +311,78 @@ class ApiClient(object):
return path
def __deserialize_primitive(self, data, klass):
"""
Deserialize string to primitive type
:param data: str
:param klass: class literal
:return: int, float, str, bool
"""
try:
value = klass(data)
except UnicodeEncodeError:
value = unicode(data)
except TypeError:
value = data
return value
def __deserialize_object(self):
"""
Deserialize empty object
"""
return object()
def __deserialize_datatime(self, string):
"""
Deserialize string to datetime.
The string should be in iso8601 datetime format.
:param string: str
:return: datetime
"""
try:
from dateutil.parser import parse
return parse(string)
except ImportError:
return string
def __deserialize_model(self, data, klass):
"""
Deserialize list or dict to model
:param data: dict, list
:param klass: class literal
"""
instance = klass()
for attr, attr_type in iteritems(instance.swagger_types):
if data is not None \
and instance.attribute_map[attr] in data\
and isinstance(data, (list, dict)):
value = data[instance.attribute_map[attr]]
if attr_type in ['str', 'int', 'float', 'bool']:
attr_type = eval(attr_type)
setattr(instance, attr, self.__deserialize_primitive(value, attr_type))
elif attr_type == 'datetime':
setattr(instance, attr, self.__deserialize_datatime(value))
elif 'list[' in attr_type:
if not value:
setattr(instance, attr, None)
else:
sub_kls = re.match('list\[(.*)\]', attr_type).group(1)
setattr(instance, attr, [self.__deserialize(v, sub_kls) for v in value])
else:
setattr(instance, attr, self.__deserialize(value, attr_type))
return instance

View File

@ -90,7 +90,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def add_pet(self, **kwargs):
"""
@ -141,7 +141,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def find_pets_by_status(self, **kwargs):
"""
@ -192,7 +192,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='list[Pet]', auth_settings=auth_settings)
response_type='list[Pet]', auth_settings=auth_settings)
return response
@ -245,7 +245,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='list[Pet]', auth_settings=auth_settings)
response_type='list[Pet]', auth_settings=auth_settings)
return response
@ -302,7 +302,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='Pet', auth_settings=auth_settings)
response_type='Pet', auth_settings=auth_settings)
return response
@ -367,7 +367,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def delete_pet(self, pet_id, **kwargs):
"""
@ -426,7 +426,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def upload_file(self, pet_id, **kwargs):
"""
@ -489,7 +489,7 @@ class PetApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)

View File

@ -86,7 +86,7 @@ class StoreApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='dict(str, int)', auth_settings=auth_settings)
response_type='dict(str, int)', auth_settings=auth_settings)
return response
@ -139,7 +139,7 @@ class StoreApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='Order', auth_settings=auth_settings)
response_type='Order', auth_settings=auth_settings)
return response
@ -196,7 +196,7 @@ class StoreApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='Order', auth_settings=auth_settings)
response_type='Order', auth_settings=auth_settings)
return response
@ -253,7 +253,7 @@ class StoreApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)

View File

@ -90,7 +90,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def create_users_with_array_input(self, **kwargs):
"""
@ -141,7 +141,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def create_users_with_list_input(self, **kwargs):
"""
@ -192,7 +192,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def login_user(self, **kwargs):
"""
@ -247,7 +247,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='str', auth_settings=auth_settings)
response_type='str', auth_settings=auth_settings)
return response
@ -296,7 +296,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def get_user_by_name(self, username, **kwargs):
"""
@ -351,7 +351,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response='User', auth_settings=auth_settings)
response_type='User', auth_settings=auth_settings)
return response
@ -412,7 +412,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)
def delete_user(self, username, **kwargs):
"""
@ -467,7 +467,7 @@ class UserApi(object):
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,
response=None, auth_settings=auth_settings)
response_type=None, auth_settings=auth_settings)

View File

@ -75,7 +75,7 @@ class RESTClientObject(object):
return self.pool_manager
def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, raw=False):
body=None, post_params=None):
"""
:param method: http request method
:param url: http request url
@ -125,30 +125,18 @@ class RESTClientObject(object):
headers=headers)
r = RESTResponse(r)
if r.status not in range(200, 206):
raise ApiException(r)
return self.process_response(r, raw)
def process_response(self, response, raw):
if raw:
return response
# In the python 3, the response.data is bytes.
# we need to decode it to string.
if sys.version_info > (3,):
data = response.data.decode('utf8')
else:
data = response.data
try:
resp = json.loads(data)
except ValueError:
resp = data
r.data = r.data.decode('utf8')
return resp
if r.status not in range(200, 206):
raise ApiException(r)
def GET(self, url, headers=None, query_params=None, raw=False):
return self.request("GET", url, headers=headers, query_params=query_params, raw=raw)
return r
def GET(self, url, headers=None, query_params=None):
return self.request("GET", url, headers=headers, query_params=query_params)
def HEAD(self, url, headers=None, query_params=None):
return self.request("HEAD", url, headers=headers, query_params=query_params)

View File

@ -112,7 +112,7 @@ class ApiClientTests(unittest.TestCase):
}
}
data = self.api_client.deserialize(json, 'dict(str, Pet)')
data = self.api_client._ApiClient__deserialize(json, 'dict(str, Pet)')
self.assertTrue(isinstance(data, dict))
self.assertTrue(isinstance(data['pet'], swagger_client.Pet))
@ -121,10 +121,14 @@ class ApiClientTests(unittest.TestCase):
'integer': 1
}
data = self.api_client.deserialize(json, 'dict(str, int)')
data = self.api_client._ApiClient__deserialize(json, 'dict(str, int)')
self.assertTrue(isinstance(data, dict))
self.assertTrue(isinstance(data['integer'], int))
def test_deserialize_to_object(self):
data = self.api_client.deserialize("", "object")
data = self.api_client._ApiClient__deserialize("", "object")
self.assertTrue(type(data) == object)