|
11 | 11 | from __future__ import absolute_import
|
12 | 12 |
|
13 | 13 | import datetime
|
| 14 | +import inspect |
14 | 15 | import json
|
| 16 | +import logging |
15 | 17 | import mimetypes
|
16 | 18 | from multiprocessing.pool import ThreadPool
|
17 | 19 | import os
|
18 | 20 | import re
|
19 | 21 | import tempfile
|
| 22 | +from typing import get_type_hints |
20 | 23 |
|
21 | 24 | # python 2 and python 3 compatibility library
|
22 | 25 | import six
|
23 |
| -from basyx.aas.adapter.json import StrictAASFromJsonDecoder, AASToJsonEncoder |
| 26 | +from basyx.aas.adapter.json import StrictAASFromJsonDecoder, AASToJsonEncoder, AASFromJsonDecoder |
24 | 27 | from six.moves.urllib.parse import quote
|
25 | 28 |
|
26 | 29 | from aas_api_python_client.configuration import Configuration
|
@@ -74,11 +77,28 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
|
74 | 77 | self.cookie = cookie
|
75 | 78 | # Set default User-Agent.
|
76 | 79 | self.user_agent = 'Swagger-Codegen/1.0.0/python'
|
| 80 | + self.basyx_decoders = self._get_basyx_decoder_funcs() |
77 | 81 |
|
78 | 82 | def __del__(self):
|
79 | 83 | self.pool.close()
|
80 | 84 | self.pool.join()
|
81 | 85 |
|
| 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 | + |
82 | 102 | @property
|
83 | 103 | def user_agent(self):
|
84 | 104 | """User agent for this API client"""
|
@@ -217,30 +237,10 @@ def deserialize(self, response, response_type):
|
217 | 237 | if response_type == "file":
|
218 | 238 | return self.__deserialize_file(response)
|
219 | 239 |
|
220 |
| - data = self.__deserialize_with_basyx_json_decoder(response, response_type) |
| 240 | + data = json.loads(response.data.decode('utf-8'), cls=StrictAASFromJsonDecoder) |
221 | 241 |
|
222 | 242 | return self.__deserialize(data, response_type)
|
223 | 243 |
|
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 |
| - |
244 | 244 | def __deserialize(self, data, klass):
|
245 | 245 | """Deserializes dict, list, str into an object.
|
246 | 246 |
|
@@ -618,6 +618,10 @@ def __deserialize_model(self, data, klass):
|
618 | 618 | :return: model object.
|
619 | 619 | """
|
620 | 620 |
|
| 621 | + if klass.__name__ in self.basyx_decoders: |
| 622 | + data = self.basyx_decoders[klass.__name__](data) |
| 623 | + return data |
| 624 | + |
621 | 625 | if not klass.swagger_types and not self.__hasattr(klass, 'get_real_child_model'):
|
622 | 626 | return data
|
623 | 627 |
|
|
0 commit comments