Skip to content

Commit e13e1f3

Browse files
add public api for deserializing models (Chaffelson#236)
* update dump so that it supports dumping swagger entities this pr also fixes a bug where the nifi style of xml was resulting in incomplete objects * update to sync with next branch
1 parent 8e770c5 commit e13e1f3

File tree

7 files changed

+1427
-1308
lines changed

7 files changed

+1427
-1308
lines changed

nipyapi/nifi/api_client.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ def sanitize_for_serialization(self, obj):
194194
elif isinstance(obj, list):
195195
return [self.sanitize_for_serialization(sub_obj)
196196
for sub_obj in obj]
197+
elif isinstance(obj, set):
198+
return {self.sanitize_for_serialization(sub_obj)
199+
for sub_obj in obj}
197200
elif isinstance(obj, tuple):
198201
return tuple(self.sanitize_for_serialization(sub_obj)
199202
for sub_obj in obj)
@@ -253,6 +256,9 @@ def __deserialize(self, data, klass):
253256
if type(klass) == str:
254257
if klass.startswith('list['):
255258
sub_kls = re.match('list\[(.*)\]', klass).group(1)
259+
if isinstance(data, dict):
260+
# ok, we got a single instance when we may have gotten a list
261+
return self.__deserialize(data, sub_kls)
256262
return [self.__deserialize(sub_data, sub_kls)
257263
for sub_data in data]
258264

@@ -609,6 +615,16 @@ def __deserialize_datatime(self, string):
609615
)
610616
)
611617

618+
def deserialize_model(self, data, klass):
619+
"""
620+
Deserializes list or dict to model.
621+
622+
:param data: dict, list.
623+
:param klass: class literal.
624+
:return: model object.
625+
"""
626+
return self.__deserialize_model(data, klass)
627+
612628
def __deserialize_model(self, data, klass):
613629
"""
614630
Deserializes list or dict to model.
@@ -626,7 +642,21 @@ def __deserialize_model(self, data, klass):
626642
and klass.attribute_map[attr] in data \
627643
and isinstance(data, (list, dict)):
628644
value = data[klass.attribute_map[attr]]
629-
kwargs[attr] = self.__deserialize(value, attr_type)
645+
if attr_type.startswith('list['):
646+
# if this is a list, we may get back a single item
647+
# or a list
648+
# create the list object if it doesn't exist
649+
# append the return
650+
if not kwargs.get(attr):
651+
kwargs[attr] = []
652+
deserialized_value = self.__deserialize(value, attr_type)
653+
if deserialized_value:
654+
if isinstance(deserialized_value, list):
655+
kwargs[attr].extend(deserialized_value)
656+
else:
657+
kwargs[attr].append(deserialized_value)
658+
else:
659+
kwargs[attr] = self.__deserialize(value, attr_type)
630660

631661
instance = klass(**kwargs)
632662

nipyapi/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,27 @@
3333

3434
def dump(obj, mode='json'):
3535
"""
36-
Dumps a native datatype object to json or yaml, defaults to json
36+
Dumps a native datatype object or swagger entity to json or yaml, defaults to json
3737
3838
Args:
39-
obj (varies): The native datatype object to serialise
39+
obj (varies): The native datatype object or swagger type to serialise
4040
mode (str): 'json' or 'yaml', the supported export modes
4141
4242
Returns (str): The serialised object
4343
4444
"""
4545
assert mode in ['json', 'yaml']
46+
unset = False
47+
if nipyapi.config.nifi_config.api_client is None:
48+
unset = True
49+
nipyapi.config.nifi_config.api_client = nipyapi.nifi.ApiClient()
50+
51+
prepared_obj = nipyapi.config.nifi_config.api_client.sanitize_for_serialization(obj)
52+
if unset:
53+
nipyapi.config.nifi_config.api_client = None
4654
try:
4755
out = json.dumps(
48-
obj=obj,
56+
obj=prepared_obj,
4957
sort_keys=True,
5058
indent=4
5159
# default=_json_default

resources/client_gen/swagger_templates/api_client.mustache

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ class ApiClient(object):
185185
elif isinstance(obj, list):
186186
return [self.sanitize_for_serialization(sub_obj)
187187
for sub_obj in obj]
188+
elif isinstance(obj, set):
189+
return {self.sanitize_for_serialization(sub_obj)
190+
for sub_obj in obj}
188191
elif isinstance(obj, tuple):
189192
return tuple(self.sanitize_for_serialization(sub_obj)
190193
for sub_obj in obj)
@@ -244,6 +247,9 @@ class ApiClient(object):
244247
if type(klass) == str:
245248
if klass.startswith('list['):
246249
sub_kls = re.match('list\[(.*)\]', klass).group(1)
250+
if isinstance(data, dict):
251+
# ok, we got a single instance when we may have gotten a list
252+
return self.__deserialize(data, sub_kls)
247253
return [self.__deserialize(sub_data, sub_kls)
248254
for sub_data in data]
249255

@@ -600,6 +606,16 @@ class ApiClient(object):
600606
)
601607
)
602608

609+
def deserialize_model(self, data, klass):
610+
"""
611+
Deserializes list or dict to model.
612+
613+
:param data: dict, list.
614+
:param klass: class literal.
615+
:return: model object.
616+
"""
617+
return self.__deserialize_model(data, klass)
618+
603619
def __deserialize_model(self, data, klass):
604620
"""
605621
Deserializes list or dict to model.
@@ -617,8 +633,22 @@ class ApiClient(object):
617633
and klass.attribute_map[attr] in data \
618634
and isinstance(data, (list, dict)):
619635
value = data[klass.attribute_map[attr]]
620-
kwargs[attr] = self.__deserialize(value, attr_type)
636+
if attr_type.startswith('list['):
637+
# if this is a list, we may get back a single item
638+
# or a list
639+
# create the list object if it doesn't exist
640+
# append the return
641+
if not kwargs.get(attr):
642+
kwargs[attr] = []
643+
deserialized_value = self.__deserialize(value, attr_type)
644+
if deserialized_value:
645+
if isinstance(deserialized_value, list):
646+
kwargs[attr].extend(deserialized_value)
647+
else:
648+
kwargs[attr].append(deserialized_value)
649+
else:
650+
kwargs[attr] = self.__deserialize(value, attr_type)
621651

622-
instance = klass(**kwargs)
652+
instance = klass(**kwargs)
623653

624654
return instance

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# Can't use skiptest with parametrize for Travis
5252
# Mostly because loading up all the environments takes too long
5353

54-
default_nifi_endpoints = ['http://localhost:8080/nifi-api']
54+
default_nifi_endpoints = ['http://' + test_host + ':8080/nifi-api']
5555
regress_nifi_endpoints = [
5656
'http://' + test_host + ':10112/nifi-api',
5757
'http://' + test_host + ':10120/nifi-api',
@@ -60,9 +60,9 @@
6060
]
6161
secure_nifi_endpoints = ['https://' + test_host + ':8443/nifi-api']
6262
default_registry_endpoints = [
63-
('http://localhost:18080/nifi-registry-api',
63+
('http://' + test_host + ':18080/nifi-registry-api',
6464
'http://registry:18080',
65-
'http://localhost:8080/nifi-api'
65+
'http://' + test_host + ':8080/nifi-api'
6666
)
6767
]
6868
regress_registry_endpoints = [

0 commit comments

Comments
 (0)