Skip to content

Commit

Permalink
move serde from api client into separate class: p2
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed May 13, 2021
1 parent 8c100cf commit 5793bec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
40 changes: 20 additions & 20 deletions generator-templates/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,7 @@ class ModelSerDe:
return {key: self.sanitize_for_serialization(val)
for key, val in six.iteritems(obj_dict)}

def deserialize(self, response, response_type):
"""Deserializes response into an object.

:param response: RESTResponse object to be deserialized.
:param response_type: class literal for
deserialized object, or string of class name.

:return: deserialized object.
"""
try:
data = json.loads(response.data)
except ValueError:
data = response.data
return self.__deserialize(data, response_type)

def __deserialize(self, data, klass):
def deserialize_raw(self, data, klass):
"""Deserializes dict, list, str into an object.

:param data: dict, list or str.
Expand All @@ -110,12 +95,12 @@ class ModelSerDe:
if type(klass) == str:
if klass.startswith('list['):
sub_kls = re.match(r'list\[(.*)\]', klass).group(1)
return [self.__deserialize(sub_data, sub_kls)
return [self.deserialize_raw(sub_data, sub_kls)
for sub_data in data]

if klass.startswith('dict('):
sub_kls = re.match(r'dict\(([^,]*), (.*)\)', klass).group(2)
return {k: self.__deserialize(v, sub_kls)
return {k: self.deserialize_raw(v, sub_kls)
for k, v in six.iteritems(data)}

# convert str to class
Expand Down Expand Up @@ -222,14 +207,14 @@ class ModelSerDe:
for attr, attr_type in six.iteritems(klass.openapi_types):
if klass.attribute_map[attr] in data:
value = data[klass.attribute_map[attr]]
kwargs[attr] = self.__deserialize(value, attr_type)
kwargs[attr] = self.deserialize_raw(value, attr_type)

instance = klass(**kwargs)

if has_discriminator:
klass_name = instance.get_real_child_model(data)
if klass_name:
instance = self.__deserialize(data, klass_name)
instance = self.deserialize_raw(data, klass_name)
return instance


Expand Down Expand Up @@ -441,6 +426,21 @@ class ApiClient(ModelSerDe, object):
response_data.getheaders()))
{{/tornado}}

def deserialize(self, response, response_type):
"""Deserializes response into an object.

:param response: RESTResponse object to be deserialized.
:param response_type: class literal for
deserialized object, or string of class name.

:return: deserialized object.
"""
try:
data = json.loads(response.data)
except ValueError:
data = response.data
return self.deserialize_raw(data, response_type)

def call_api(self, resource_path, method,
path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None,
Expand Down
40 changes: 20 additions & 20 deletions regula/documentreader/webclient/gen/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,7 @@ def sanitize_for_serialization(self, obj):
return {key: self.sanitize_for_serialization(val)
for key, val in six.iteritems(obj_dict)}

def deserialize(self, response, response_type):
"""Deserializes response into an object.
:param response: RESTResponse object to be deserialized.
:param response_type: class literal for
deserialized object, or string of class name.
:return: deserialized object.
"""
try:
data = json.loads(response.data)
except ValueError:
data = response.data
return self.__deserialize(data, response_type)

def __deserialize(self, data, klass):
def deserialize_raw(self, data, klass):
"""Deserializes dict, list, str into an object.
:param data: dict, list or str.
Expand All @@ -109,12 +94,12 @@ def __deserialize(self, data, klass):
if type(klass) == str:
if klass.startswith('list['):
sub_kls = re.match(r'list\[(.*)\]', klass).group(1)
return [self.__deserialize(sub_data, sub_kls)
return [self.deserialize_raw(sub_data, sub_kls)
for sub_data in data]

if klass.startswith('dict('):
sub_kls = re.match(r'dict\(([^,]*), (.*)\)', klass).group(2)
return {k: self.__deserialize(v, sub_kls)
return {k: self.deserialize_raw(v, sub_kls)
for k, v in six.iteritems(data)}

# convert str to class
Expand Down Expand Up @@ -221,14 +206,14 @@ def __deserialize_model(self, data, klass):
for attr, attr_type in six.iteritems(klass.openapi_types):
if klass.attribute_map[attr] in data:
value = data[klass.attribute_map[attr]]
kwargs[attr] = self.__deserialize(value, attr_type)
kwargs[attr] = self.deserialize_raw(value, attr_type)

instance = klass(**kwargs)

if has_discriminator:
klass_name = instance.get_real_child_model(data)
if klass_name:
instance = self.__deserialize(data, klass_name)
instance = self.deserialize_raw(data, klass_name)
return instance


Expand Down Expand Up @@ -411,6 +396,21 @@ def __call_api(
return (return_data, response_data.status,
response_data.getheaders())

def deserialize(self, response, response_type):
"""Deserializes response into an object.
:param response: RESTResponse object to be deserialized.
:param response_type: class literal for
deserialized object, or string of class name.
:return: deserialized object.
"""
try:
data = json.loads(response.data)
except ValueError:
data = response.data
return self.deserialize_raw(data, response_type)

def call_api(self, resource_path, method,
path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None,
Expand Down

0 comments on commit 5793bec

Please sign in to comment.