Skip to content

Commit 0b25e87

Browse files
committed
Use single basyx decoders for each class
1 parent c45fb45 commit 0b25e87

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

aas_api_python_client/api_client.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
from __future__ import absolute_import
1212

1313
import datetime
14+
import inspect
1415
import json
16+
import logging
1517
import mimetypes
1618
from multiprocessing.pool import ThreadPool
1719
import os
1820
import re
1921
import tempfile
22+
from typing import get_type_hints
2023

2124
# python 2 and python 3 compatibility library
2225
import six
23-
from basyx.aas.adapter.json import StrictAASFromJsonDecoder, AASToJsonEncoder
26+
from basyx.aas.adapter.json import StrictAASFromJsonDecoder, AASToJsonEncoder, AASFromJsonDecoder
2427
from six.moves.urllib.parse import quote
2528

2629
from aas_api_python_client.configuration import Configuration
@@ -74,11 +77,28 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
7477
self.cookie = cookie
7578
# Set default User-Agent.
7679
self.user_agent = 'Swagger-Codegen/1.0.0/python'
80+
self.basyx_decoders = self._get_basyx_decoder_funcs()
7781

7882
def __del__(self):
7983
self.pool.close()
8084
self.pool.join()
8185

86+
def _get_basyx_decoder_funcs(self):
87+
members = inspect.getmembers(AASFromJsonDecoder)
88+
constructors = [member for member in members if member[0].startswith('_construct')]
89+
# Get the return types of these methods
90+
method_return_types = {}
91+
for constructor_name, constructor in constructors:
92+
print(constructor_name, str(constructor))
93+
try:
94+
return_type = get_type_hints(constructor).get('return', 'Unknown')
95+
except NameError as e:
96+
logging.error(f"Failed to get return type of method {constructor_name}: {e}")
97+
return_type = 'Unknown'
98+
return_type = return_type if return_type == 'Unknown' else return_type.__name__
99+
method_return_types[return_type] = constructor
100+
return method_return_types
101+
82102
@property
83103
def user_agent(self):
84104
"""User agent for this API client"""
@@ -217,30 +237,10 @@ def deserialize(self, response, response_type):
217237
if response_type == "file":
218238
return self.__deserialize_file(response)
219239

220-
data = self.__deserialize_with_basyx_json_decoder(response, response_type)
240+
data = json.loads(response.data.decode('utf-8'), cls=StrictAASFromJsonDecoder)
221241

222242
return self.__deserialize(data, response_type)
223243

224-
225-
def __deserialize_with_basyx_json_decoder(self, response, response_type):
226-
decoded_response_data = response.data.decode('utf-8')
227-
data = json.loads(decoded_response_data)
228-
229-
basyx_data = data["result"] if isinstance(data, dict) and "result" in data else data
230-
231-
if isinstance(basyx_data, dict) and "modelType" not in data:
232-
basyx_data["modelType"] = response_type
233-
234-
basyx_data = json.loads(json.dumps(basyx_data), cls=StrictAASFromJsonDecoder)
235-
236-
if isinstance(data, dict) and "result" in data:
237-
data["result"] = basyx_data
238-
else:
239-
data = basyx_data
240-
241-
return data
242-
243-
244244
def __deserialize(self, data, klass):
245245
"""Deserializes dict, list, str into an object.
246246
@@ -618,6 +618,10 @@ def __deserialize_model(self, data, klass):
618618
:return: model object.
619619
"""
620620

621+
if klass.__name__ in self.basyx_decoders:
622+
data = self.basyx_decoders[klass.__name__](data)
623+
return data
624+
621625
if not klass.swagger_types and not self.__hasattr(klass, 'get_real_child_model'):
622626
return data
623627

0 commit comments

Comments
 (0)