azure-ai-agentserver-core 1.0.0b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. azure/ai/agentserver/__init__.py +1 -0
  2. azure/ai/agentserver/core/__init__.py +14 -0
  3. azure/ai/agentserver/core/_version.py +9 -0
  4. azure/ai/agentserver/core/constants.py +13 -0
  5. azure/ai/agentserver/core/logger.py +161 -0
  6. azure/ai/agentserver/core/models/__init__.py +7 -0
  7. azure/ai/agentserver/core/models/_create_response.py +12 -0
  8. azure/ai/agentserver/core/models/openai/__init__.py +16 -0
  9. azure/ai/agentserver/core/models/projects/__init__.py +820 -0
  10. azure/ai/agentserver/core/models/projects/_enums.py +767 -0
  11. azure/ai/agentserver/core/models/projects/_models.py +15049 -0
  12. azure/ai/agentserver/core/models/projects/_patch.py +39 -0
  13. azure/ai/agentserver/core/models/projects/_patch_evaluations.py +48 -0
  14. azure/ai/agentserver/core/models/projects/_utils/__init__.py +6 -0
  15. azure/ai/agentserver/core/models/projects/_utils/model_base.py +1237 -0
  16. azure/ai/agentserver/core/models/projects/_utils/serialization.py +2030 -0
  17. azure/ai/agentserver/core/py.typed +0 -0
  18. azure/ai/agentserver/core/server/__init__.py +1 -0
  19. azure/ai/agentserver/core/server/base.py +324 -0
  20. azure/ai/agentserver/core/server/common/__init__.py +1 -0
  21. azure/ai/agentserver/core/server/common/agent_run_context.py +76 -0
  22. azure/ai/agentserver/core/server/common/id_generator/__init__.py +5 -0
  23. azure/ai/agentserver/core/server/common/id_generator/foundry_id_generator.py +136 -0
  24. azure/ai/agentserver/core/server/common/id_generator/id_generator.py +19 -0
  25. azure_ai_agentserver_core-1.0.0b2.dist-info/METADATA +149 -0
  26. azure_ai_agentserver_core-1.0.0b2.dist-info/RECORD +29 -0
  27. azure_ai_agentserver_core-1.0.0b2.dist-info/WHEEL +5 -0
  28. azure_ai_agentserver_core-1.0.0b2.dist-info/licenses/LICENSE +21 -0
  29. azure_ai_agentserver_core-1.0.0b2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2030 @@
1
+ # pylint: disable=line-too-long,useless-suppression,too-many-lines
2
+ # coding=utf-8
3
+ # --------------------------------------------------------------------------
4
+ # Copyright (c) Microsoft Corporation. All rights reserved.
5
+ # Licensed under the MIT License. See License.txt in the project root for license information.
6
+ # Code generated by Microsoft (R) Python Code Generator.
7
+ # Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
+ # --------------------------------------------------------------------------
9
+
10
+ # pyright: reportUnnecessaryTypeIgnoreComment=false
11
+
12
+ from base64 import b64decode, b64encode
13
+ import calendar
14
+ import datetime
15
+ import decimal
16
+ import email
17
+ from enum import Enum
18
+ import json
19
+ import logging
20
+ import re
21
+ import sys
22
+ import codecs
23
+ from typing import (
24
+ Any,
25
+ cast,
26
+ Optional,
27
+ Union,
28
+ AnyStr,
29
+ IO,
30
+ Mapping,
31
+ Callable,
32
+ MutableMapping,
33
+ )
34
+
35
+ try:
36
+ from urllib import quote # type: ignore
37
+ except ImportError:
38
+ from urllib.parse import quote
39
+ import xml.etree.ElementTree as ET
40
+
41
+ import isodate # type: ignore
42
+ from typing_extensions import Self
43
+
44
+ from azure.core.exceptions import DeserializationError, SerializationError
45
+ from azure.core.serialization import NULL as CoreNull
46
+
47
+ _BOM = codecs.BOM_UTF8.decode(encoding="utf-8")
48
+
49
+ JSON = MutableMapping[str, Any]
50
+
51
+
52
+ class RawDeserializer:
53
+
54
+ # Accept "text" because we're open minded people...
55
+ JSON_REGEXP = re.compile(r"^(application|text)/([a-z+.]+\+)?json$")
56
+
57
+ # Name used in context
58
+ CONTEXT_NAME = "deserialized_data"
59
+
60
+ @classmethod
61
+ def deserialize_from_text(cls, data: Optional[Union[AnyStr, IO]], content_type: Optional[str] = None) -> Any:
62
+ """Decode data according to content-type.
63
+
64
+ Accept a stream of data as well, but will be load at once in memory for now.
65
+
66
+ If no content-type, will return the string version (not bytes, not stream)
67
+
68
+ :param data: Input, could be bytes or stream (will be decoded with UTF8) or text
69
+ :type data: str or bytes or IO
70
+ :param str content_type: The content type.
71
+ :return: The deserialized data.
72
+ :rtype: object
73
+ """
74
+ if hasattr(data, "read"):
75
+ # Assume a stream
76
+ data = cast(IO, data).read()
77
+
78
+ if isinstance(data, bytes):
79
+ data_as_str = data.decode(encoding="utf-8-sig")
80
+ else:
81
+ # Explain to mypy the correct type.
82
+ data_as_str = cast(str, data)
83
+
84
+ # Remove Byte Order Mark if present in string
85
+ data_as_str = data_as_str.lstrip(_BOM)
86
+
87
+ if content_type is None:
88
+ return data
89
+
90
+ if cls.JSON_REGEXP.match(content_type):
91
+ try:
92
+ return json.loads(data_as_str)
93
+ except ValueError as err:
94
+ raise DeserializationError("JSON is invalid: {}".format(err), err) from err
95
+ elif "xml" in (content_type or []):
96
+ try:
97
+
98
+ try:
99
+ if isinstance(data, unicode): # type: ignore
100
+ # If I'm Python 2.7 and unicode XML will scream if I try a "fromstring" on unicode string
101
+ data_as_str = data_as_str.encode(encoding="utf-8") # type: ignore
102
+ except NameError:
103
+ pass
104
+
105
+ return ET.fromstring(data_as_str) # nosec
106
+ except ET.ParseError as err:
107
+ # It might be because the server has an issue, and returned JSON with
108
+ # content-type XML....
109
+ # So let's try a JSON load, and if it's still broken
110
+ # let's flow the initial exception
111
+ def _json_attemp(data):
112
+ try:
113
+ return True, json.loads(data)
114
+ except ValueError:
115
+ return False, None # Don't care about this one
116
+
117
+ success, json_result = _json_attemp(data)
118
+ if success:
119
+ return json_result
120
+ # If i'm here, it's not JSON, it's not XML, let's scream
121
+ # and raise the last context in this block (the XML exception)
122
+ # The function hack is because Py2.7 messes up with exception
123
+ # context otherwise.
124
+ _LOGGER.critical("Wasn't XML not JSON, failing")
125
+ raise DeserializationError("XML is invalid") from err
126
+ elif content_type.startswith("text/"):
127
+ return data_as_str
128
+ raise DeserializationError("Cannot deserialize content-type: {}".format(content_type))
129
+
130
+ @classmethod
131
+ def deserialize_from_http_generics(cls, body_bytes: Optional[Union[AnyStr, IO]], headers: Mapping) -> Any:
132
+ """Deserialize from HTTP response.
133
+
134
+ Use bytes and headers to NOT use any requests/aiohttp or whatever
135
+ specific implementation.
136
+ Headers will tested for "content-type"
137
+
138
+ :param bytes body_bytes: The body of the response.
139
+ :param dict headers: The headers of the response.
140
+ :returns: The deserialized data.
141
+ :rtype: object
142
+ """
143
+ # Try to use content-type from headers if available
144
+ content_type = None
145
+ if "content-type" in headers:
146
+ content_type = headers["content-type"].split(";")[0].strip().lower()
147
+ # Ouch, this server did not declare what it sent...
148
+ # Let's guess it's JSON...
149
+ # Also, since Autorest was considering that an empty body was a valid JSON,
150
+ # need that test as well....
151
+ else:
152
+ content_type = "application/json"
153
+
154
+ if body_bytes:
155
+ return cls.deserialize_from_text(body_bytes, content_type)
156
+ return None
157
+
158
+
159
+ _LOGGER = logging.getLogger(__name__)
160
+
161
+ try:
162
+ _long_type = long # type: ignore
163
+ except NameError:
164
+ _long_type = int
165
+
166
+ TZ_UTC = datetime.timezone.utc
167
+
168
+ _FLATTEN = re.compile(r"(?<!\\)\.")
169
+
170
+
171
+ def attribute_transformer(key, attr_desc, value): # pylint: disable=unused-argument
172
+ """A key transformer that returns the Python attribute.
173
+
174
+ :param str key: The attribute name
175
+ :param dict attr_desc: The attribute metadata
176
+ :param object value: The value
177
+ :returns: A key using attribute name
178
+ :rtype: str
179
+ """
180
+ return (key, value)
181
+
182
+
183
+ def full_restapi_key_transformer(key, attr_desc, value): # pylint: disable=unused-argument
184
+ """A key transformer that returns the full RestAPI key path.
185
+
186
+ :param str key: The attribute name
187
+ :param dict attr_desc: The attribute metadata
188
+ :param object value: The value
189
+ :returns: A list of keys using RestAPI syntax.
190
+ :rtype: list
191
+ """
192
+ keys = _FLATTEN.split(attr_desc["key"])
193
+ return ([_decode_attribute_map_key(k) for k in keys], value)
194
+
195
+
196
+ def last_restapi_key_transformer(key, attr_desc, value):
197
+ """A key transformer that returns the last RestAPI key.
198
+
199
+ :param str key: The attribute name
200
+ :param dict attr_desc: The attribute metadata
201
+ :param object value: The value
202
+ :returns: The last RestAPI key.
203
+ :rtype: str
204
+ """
205
+ key, value = full_restapi_key_transformer(key, attr_desc, value)
206
+ return (key[-1], value)
207
+
208
+
209
+ def _create_xml_node(tag, prefix=None, ns=None):
210
+ """Create a XML node.
211
+
212
+ :param str tag: The tag name
213
+ :param str prefix: The prefix
214
+ :param str ns: The namespace
215
+ :return: The XML node
216
+ :rtype: xml.etree.ElementTree.Element
217
+ """
218
+ if prefix and ns:
219
+ ET.register_namespace(prefix, ns)
220
+ if ns:
221
+ return ET.Element("{" + ns + "}" + tag)
222
+ return ET.Element(tag)
223
+
224
+
225
+ class Model:
226
+ """Mixin for all client request body/response body models to support
227
+ serialization and deserialization.
228
+ """
229
+
230
+ _subtype_map: dict[str, dict[str, Any]] = {}
231
+ _attribute_map: dict[str, dict[str, Any]] = {}
232
+ _validation: dict[str, dict[str, Any]] = {}
233
+
234
+ def __init__(self, **kwargs: Any) -> None:
235
+ self.additional_properties: Optional[dict[str, Any]] = {}
236
+ for k in kwargs: # pylint: disable=consider-using-dict-items
237
+ if k not in self._attribute_map:
238
+ _LOGGER.warning("%s is not a known attribute of class %s and will be ignored", k, self.__class__)
239
+ elif k in self._validation and self._validation[k].get("readonly", False):
240
+ _LOGGER.warning("Readonly attribute %s will be ignored in class %s", k, self.__class__)
241
+ else:
242
+ setattr(self, k, kwargs[k])
243
+
244
+ def __eq__(self, other: Any) -> bool:
245
+ """Compare objects by comparing all attributes.
246
+
247
+ :param object other: The object to compare
248
+ :returns: True if objects are equal
249
+ :rtype: bool
250
+ """
251
+ if isinstance(other, self.__class__):
252
+ return self.__dict__ == other.__dict__
253
+ return False
254
+
255
+ def __ne__(self, other: Any) -> bool:
256
+ """Compare objects by comparing all attributes.
257
+
258
+ :param object other: The object to compare
259
+ :returns: True if objects are not equal
260
+ :rtype: bool
261
+ """
262
+ return not self.__eq__(other)
263
+
264
+ def __str__(self) -> str:
265
+ return str(self.__dict__)
266
+
267
+ @classmethod
268
+ def enable_additional_properties_sending(cls) -> None:
269
+ cls._attribute_map["additional_properties"] = {"key": "", "type": "{object}"}
270
+
271
+ @classmethod
272
+ def is_xml_model(cls) -> bool:
273
+ try:
274
+ cls._xml_map # type: ignore
275
+ except AttributeError:
276
+ return False
277
+ return True
278
+
279
+ @classmethod
280
+ def _create_xml_node(cls):
281
+ """Create XML node.
282
+
283
+ :returns: The XML node
284
+ :rtype: xml.etree.ElementTree.Element
285
+ """
286
+ try:
287
+ xml_map = cls._xml_map # type: ignore
288
+ except AttributeError:
289
+ xml_map = {}
290
+
291
+ return _create_xml_node(xml_map.get("name", cls.__name__), xml_map.get("prefix", None), xml_map.get("ns", None))
292
+
293
+ def serialize(self, keep_readonly: bool = False, **kwargs: Any) -> JSON:
294
+ """Return the JSON that would be sent to server from this model.
295
+
296
+ This is an alias to `as_dict(full_restapi_key_transformer, keep_readonly=False)`.
297
+
298
+ If you want XML serialization, you can pass the kwargs is_xml=True.
299
+
300
+ :param bool keep_readonly: If you want to serialize the readonly attributes
301
+ :returns: A dict JSON compatible object
302
+ :rtype: dict
303
+ """
304
+ serializer = Serializer(self._infer_class_models())
305
+ return serializer._serialize( # type: ignore # pylint: disable=protected-access
306
+ self, keep_readonly=keep_readonly, **kwargs
307
+ )
308
+
309
+ def as_dict(
310
+ self,
311
+ keep_readonly: bool = True,
312
+ key_transformer: Callable[[str, dict[str, Any], Any], Any] = attribute_transformer,
313
+ **kwargs: Any
314
+ ) -> JSON:
315
+ """Return a dict that can be serialized using json.dump.
316
+
317
+ Advanced usage might optionally use a callback as parameter:
318
+
319
+ .. code::python
320
+
321
+ def my_key_transformer(key, attr_desc, value):
322
+ return key
323
+
324
+ Key is the attribute name used in Python. Attr_desc
325
+ is a dict of metadata. Currently contains 'type' with the
326
+ msrest type and 'key' with the RestAPI encoded key.
327
+ Value is the current value in this object.
328
+
329
+ The string returned will be used to serialize the key.
330
+ If the return type is a list, this is considered hierarchical
331
+ result dict.
332
+
333
+ See the three examples in this file:
334
+
335
+ - attribute_transformer
336
+ - full_restapi_key_transformer
337
+ - last_restapi_key_transformer
338
+
339
+ If you want XML serialization, you can pass the kwargs is_xml=True.
340
+
341
+ :param bool keep_readonly: If you want to serialize the readonly attributes
342
+ :param function key_transformer: A key transformer function.
343
+ :returns: A dict JSON compatible object
344
+ :rtype: dict
345
+ """
346
+ serializer = Serializer(self._infer_class_models())
347
+ return serializer._serialize( # type: ignore # pylint: disable=protected-access
348
+ self, key_transformer=key_transformer, keep_readonly=keep_readonly, **kwargs
349
+ )
350
+
351
+ @classmethod
352
+ def _infer_class_models(cls):
353
+ try:
354
+ str_models = cls.__module__.rsplit(".", 1)[0]
355
+ models = sys.modules[str_models]
356
+ client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
357
+ if cls.__name__ not in client_models:
358
+ raise ValueError("Not Autorest generated code")
359
+ except Exception: # pylint: disable=broad-exception-caught
360
+ # Assume it's not Autorest generated (tests?). Add ourselves as dependencies.
361
+ client_models = {cls.__name__: cls}
362
+ return client_models
363
+
364
+ @classmethod
365
+ def deserialize(cls, data: Any, content_type: Optional[str] = None) -> Self:
366
+ """Parse a str using the RestAPI syntax and return a model.
367
+
368
+ :param str data: A str using RestAPI structure. JSON by default.
369
+ :param str content_type: JSON by default, set application/xml if XML.
370
+ :returns: An instance of this model
371
+ :raises DeserializationError: if something went wrong
372
+ :rtype: Self
373
+ """
374
+ deserializer = Deserializer(cls._infer_class_models())
375
+ return deserializer(cls.__name__, data, content_type=content_type) # type: ignore
376
+
377
+ @classmethod
378
+ def from_dict(
379
+ cls,
380
+ data: Any,
381
+ key_extractors: Optional[Callable[[str, dict[str, Any], Any], Any]] = None,
382
+ content_type: Optional[str] = None,
383
+ ) -> Self:
384
+ """Parse a dict using given key extractor return a model.
385
+
386
+ By default consider key
387
+ extractors (rest_key_case_insensitive_extractor, attribute_key_case_insensitive_extractor
388
+ and last_rest_key_case_insensitive_extractor)
389
+
390
+ :param dict data: A dict using RestAPI structure
391
+ :param function key_extractors: A key extractor function.
392
+ :param str content_type: JSON by default, set application/xml if XML.
393
+ :returns: An instance of this model
394
+ :raises DeserializationError: if something went wrong
395
+ :rtype: Self
396
+ """
397
+ deserializer = Deserializer(cls._infer_class_models())
398
+ deserializer.key_extractors = ( # type: ignore
399
+ [ # type: ignore
400
+ attribute_key_case_insensitive_extractor,
401
+ rest_key_case_insensitive_extractor,
402
+ last_rest_key_case_insensitive_extractor,
403
+ ]
404
+ if key_extractors is None
405
+ else key_extractors
406
+ )
407
+ return deserializer(cls.__name__, data, content_type=content_type) # type: ignore
408
+
409
+ @classmethod
410
+ def _flatten_subtype(cls, key, objects):
411
+ if "_subtype_map" not in cls.__dict__:
412
+ return {}
413
+ result = dict(cls._subtype_map[key])
414
+ for valuetype in cls._subtype_map[key].values():
415
+ result |= objects[valuetype]._flatten_subtype(key, objects) # pylint: disable=protected-access
416
+ return result
417
+
418
+ @classmethod
419
+ def _classify(cls, response, objects):
420
+ """Check the class _subtype_map for any child classes.
421
+ We want to ignore any inherited _subtype_maps.
422
+
423
+ :param dict response: The initial data
424
+ :param dict objects: The class objects
425
+ :returns: The class to be used
426
+ :rtype: class
427
+ """
428
+ for subtype_key in cls.__dict__.get("_subtype_map", {}).keys():
429
+ subtype_value = None
430
+
431
+ if not isinstance(response, ET.Element):
432
+ rest_api_response_key = cls._get_rest_key_parts(subtype_key)[-1]
433
+ subtype_value = response.get(rest_api_response_key, None) or response.get(subtype_key, None)
434
+ else:
435
+ subtype_value = xml_key_extractor(subtype_key, cls._attribute_map[subtype_key], response)
436
+ if subtype_value:
437
+ # Try to match base class. Can be class name only
438
+ # (bug to fix in Autorest to support x-ms-discriminator-name)
439
+ if cls.__name__ == subtype_value:
440
+ return cls
441
+ flatten_mapping_type = cls._flatten_subtype(subtype_key, objects)
442
+ try:
443
+ return objects[flatten_mapping_type[subtype_value]] # type: ignore
444
+ except KeyError:
445
+ _LOGGER.warning(
446
+ "Subtype value %s has no mapping, use base class %s.",
447
+ subtype_value,
448
+ cls.__name__,
449
+ )
450
+ break
451
+ else:
452
+ _LOGGER.warning("Discriminator %s is absent or null, use base class %s.", subtype_key, cls.__name__)
453
+ break
454
+ return cls
455
+
456
+ @classmethod
457
+ def _get_rest_key_parts(cls, attr_key):
458
+ """Get the RestAPI key of this attr, split it and decode part
459
+ :param str attr_key: Attribute key must be in attribute_map.
460
+ :returns: A list of RestAPI part
461
+ :rtype: list
462
+ """
463
+ rest_split_key = _FLATTEN.split(cls._attribute_map[attr_key]["key"])
464
+ return [_decode_attribute_map_key(key_part) for key_part in rest_split_key]
465
+
466
+
467
+ def _decode_attribute_map_key(key):
468
+ """This decode a key in an _attribute_map to the actual key we want to look at
469
+ inside the received data.
470
+
471
+ :param str key: A key string from the generated code
472
+ :returns: The decoded key
473
+ :rtype: str
474
+ """
475
+ return key.replace("\\.", ".")
476
+
477
+
478
+ class Serializer: # pylint: disable=too-many-public-methods
479
+ """Request object model serializer."""
480
+
481
+ basic_types = {str: "str", int: "int", bool: "bool", float: "float"}
482
+
483
+ _xml_basic_types_serializers = {"bool": lambda x: str(x).lower()}
484
+ days = {0: "Mon", 1: "Tue", 2: "Wed", 3: "Thu", 4: "Fri", 5: "Sat", 6: "Sun"}
485
+ months = {
486
+ 1: "Jan",
487
+ 2: "Feb",
488
+ 3: "Mar",
489
+ 4: "Apr",
490
+ 5: "May",
491
+ 6: "Jun",
492
+ 7: "Jul",
493
+ 8: "Aug",
494
+ 9: "Sep",
495
+ 10: "Oct",
496
+ 11: "Nov",
497
+ 12: "Dec",
498
+ }
499
+ validation = {
500
+ "min_length": lambda x, y: len(x) < y,
501
+ "max_length": lambda x, y: len(x) > y,
502
+ "minimum": lambda x, y: x < y,
503
+ "maximum": lambda x, y: x > y,
504
+ "minimum_ex": lambda x, y: x <= y,
505
+ "maximum_ex": lambda x, y: x >= y,
506
+ "min_items": lambda x, y: len(x) < y,
507
+ "max_items": lambda x, y: len(x) > y,
508
+ "pattern": lambda x, y: not re.match(y, x, re.UNICODE),
509
+ "unique": lambda x, y: len(x) != len(set(x)),
510
+ "multiple": lambda x, y: x % y != 0,
511
+ }
512
+
513
+ def __init__(self, classes: Optional[Mapping[str, type]] = None) -> None:
514
+ self.serialize_type = {
515
+ "iso-8601": Serializer.serialize_iso,
516
+ "rfc-1123": Serializer.serialize_rfc,
517
+ "unix-time": Serializer.serialize_unix,
518
+ "duration": Serializer.serialize_duration,
519
+ "date": Serializer.serialize_date,
520
+ "time": Serializer.serialize_time,
521
+ "decimal": Serializer.serialize_decimal,
522
+ "long": Serializer.serialize_long,
523
+ "bytearray": Serializer.serialize_bytearray,
524
+ "base64": Serializer.serialize_base64,
525
+ "object": self.serialize_object,
526
+ "[]": self.serialize_iter,
527
+ "{}": self.serialize_dict,
528
+ }
529
+ self.dependencies: dict[str, type] = dict(classes) if classes else {}
530
+ self.key_transformer = full_restapi_key_transformer
531
+ self.client_side_validation = True
532
+
533
+ def _serialize( # pylint: disable=too-many-nested-blocks, too-many-branches, too-many-statements, too-many-locals
534
+ self, target_obj, data_type=None, **kwargs
535
+ ):
536
+ """Serialize data into a string according to type.
537
+
538
+ :param object target_obj: The data to be serialized.
539
+ :param str data_type: The type to be serialized from.
540
+ :rtype: str, dict
541
+ :raises SerializationError: if serialization fails.
542
+ :returns: The serialized data.
543
+ """
544
+ key_transformer = kwargs.get("key_transformer", self.key_transformer)
545
+ keep_readonly = kwargs.get("keep_readonly", False)
546
+ if target_obj is None:
547
+ return None
548
+
549
+ attr_name = None
550
+ class_name = target_obj.__class__.__name__
551
+
552
+ if data_type:
553
+ return self.serialize_data(target_obj, data_type, **kwargs)
554
+
555
+ if not hasattr(target_obj, "_attribute_map"):
556
+ data_type = type(target_obj).__name__
557
+ if data_type in self.basic_types.values():
558
+ return self.serialize_data(target_obj, data_type, **kwargs)
559
+
560
+ # Force "is_xml" kwargs if we detect a XML model
561
+ try:
562
+ is_xml_model_serialization = kwargs["is_xml"]
563
+ except KeyError:
564
+ is_xml_model_serialization = kwargs.setdefault("is_xml", target_obj.is_xml_model())
565
+
566
+ serialized = {}
567
+ if is_xml_model_serialization:
568
+ serialized = target_obj._create_xml_node() # pylint: disable=protected-access
569
+ try:
570
+ attributes = target_obj._attribute_map # pylint: disable=protected-access
571
+ for attr, attr_desc in attributes.items():
572
+ attr_name = attr
573
+ if not keep_readonly and target_obj._validation.get( # pylint: disable=protected-access
574
+ attr_name, {}
575
+ ).get("readonly", False):
576
+ continue
577
+
578
+ if attr_name == "additional_properties" and attr_desc["key"] == "":
579
+ if target_obj.additional_properties is not None:
580
+ serialized |= target_obj.additional_properties
581
+ continue
582
+ try:
583
+
584
+ orig_attr = getattr(target_obj, attr)
585
+ if is_xml_model_serialization:
586
+ pass # Don't provide "transformer" for XML for now. Keep "orig_attr"
587
+ else: # JSON
588
+ keys, orig_attr = key_transformer(attr, attr_desc.copy(), orig_attr)
589
+ keys = keys if isinstance(keys, list) else [keys]
590
+
591
+ kwargs["serialization_ctxt"] = attr_desc
592
+ new_attr = self.serialize_data(orig_attr, attr_desc["type"], **kwargs)
593
+
594
+ if is_xml_model_serialization:
595
+ xml_desc = attr_desc.get("xml", {})
596
+ xml_name = xml_desc.get("name", attr_desc["key"])
597
+ xml_prefix = xml_desc.get("prefix", None)
598
+ xml_ns = xml_desc.get("ns", None)
599
+ if xml_desc.get("attr", False):
600
+ if xml_ns:
601
+ ET.register_namespace(xml_prefix, xml_ns)
602
+ xml_name = "{{{}}}{}".format(xml_ns, xml_name)
603
+ serialized.set(xml_name, new_attr) # type: ignore
604
+ continue
605
+ if xml_desc.get("text", False):
606
+ serialized.text = new_attr # type: ignore
607
+ continue
608
+ if isinstance(new_attr, list):
609
+ serialized.extend(new_attr) # type: ignore
610
+ elif isinstance(new_attr, ET.Element):
611
+ # If the down XML has no XML/Name,
612
+ # we MUST replace the tag with the local tag. But keeping the namespaces.
613
+ if "name" not in getattr(orig_attr, "_xml_map", {}):
614
+ splitted_tag = new_attr.tag.split("}")
615
+ if len(splitted_tag) == 2: # Namespace
616
+ new_attr.tag = "}".join([splitted_tag[0], xml_name])
617
+ else:
618
+ new_attr.tag = xml_name
619
+ serialized.append(new_attr) # type: ignore
620
+ else: # That's a basic type
621
+ # Integrate namespace if necessary
622
+ local_node = _create_xml_node(xml_name, xml_prefix, xml_ns)
623
+ local_node.text = str(new_attr)
624
+ serialized.append(local_node) # type: ignore
625
+ else: # JSON
626
+ for k in reversed(keys): # type: ignore
627
+ new_attr = {k: new_attr}
628
+
629
+ _new_attr = new_attr
630
+ _serialized = serialized
631
+ for k in keys: # type: ignore
632
+ if k not in _serialized:
633
+ _serialized.update(_new_attr) # type: ignore
634
+ _new_attr = _new_attr[k] # type: ignore
635
+ _serialized = _serialized[k]
636
+ except ValueError as err:
637
+ if isinstance(err, SerializationError):
638
+ raise
639
+
640
+ except (AttributeError, KeyError, TypeError) as err:
641
+ msg = "Attribute {} in object {} cannot be serialized.\n{}".format(attr_name, class_name, str(target_obj))
642
+ raise SerializationError(msg) from err
643
+ return serialized
644
+
645
+ def body(self, data, data_type, **kwargs):
646
+ """Serialize data intended for a request body.
647
+
648
+ :param object data: The data to be serialized.
649
+ :param str data_type: The type to be serialized from.
650
+ :rtype: dict
651
+ :raises SerializationError: if serialization fails.
652
+ :raises ValueError: if data is None
653
+ :returns: The serialized request body
654
+ """
655
+
656
+ # Just in case this is a dict
657
+ internal_data_type_str = data_type.strip("[]{}")
658
+ internal_data_type = self.dependencies.get(internal_data_type_str, None)
659
+ try:
660
+ is_xml_model_serialization = kwargs["is_xml"]
661
+ except KeyError:
662
+ if internal_data_type and issubclass(internal_data_type, Model):
663
+ is_xml_model_serialization = kwargs.setdefault("is_xml", internal_data_type.is_xml_model())
664
+ else:
665
+ is_xml_model_serialization = False
666
+ if internal_data_type and not isinstance(internal_data_type, Enum):
667
+ try:
668
+ deserializer = Deserializer(self.dependencies)
669
+ # Since it's on serialization, it's almost sure that format is not JSON REST
670
+ # We're not able to deal with additional properties for now.
671
+ deserializer.additional_properties_detection = False
672
+ if is_xml_model_serialization:
673
+ deserializer.key_extractors = [ # type: ignore
674
+ attribute_key_case_insensitive_extractor,
675
+ ]
676
+ else:
677
+ deserializer.key_extractors = [
678
+ rest_key_case_insensitive_extractor,
679
+ attribute_key_case_insensitive_extractor,
680
+ last_rest_key_case_insensitive_extractor,
681
+ ]
682
+ data = deserializer._deserialize(data_type, data) # pylint: disable=protected-access
683
+ except DeserializationError as err:
684
+ raise SerializationError("Unable to build a model: " + str(err)) from err
685
+
686
+ return self._serialize(data, data_type, **kwargs)
687
+
688
+ def url(self, name, data, data_type, **kwargs):
689
+ """Serialize data intended for a URL path.
690
+
691
+ :param str name: The name of the URL path parameter.
692
+ :param object data: The data to be serialized.
693
+ :param str data_type: The type to be serialized from.
694
+ :rtype: str
695
+ :returns: The serialized URL path
696
+ :raises TypeError: if serialization fails.
697
+ :raises ValueError: if data is None
698
+ """
699
+ try:
700
+ output = self.serialize_data(data, data_type, **kwargs)
701
+ if data_type == "bool":
702
+ output = json.dumps(output)
703
+
704
+ if kwargs.get("skip_quote") is True:
705
+ output = str(output)
706
+ output = output.replace("{", quote("{")).replace("}", quote("}"))
707
+ else:
708
+ output = quote(str(output), safe="")
709
+ except SerializationError as exc:
710
+ raise TypeError("{} must be type {}.".format(name, data_type)) from exc
711
+ return output
712
+
713
+ def query(self, name, data, data_type, **kwargs):
714
+ """Serialize data intended for a URL query.
715
+
716
+ :param str name: The name of the query parameter.
717
+ :param object data: The data to be serialized.
718
+ :param str data_type: The type to be serialized from.
719
+ :rtype: str, list
720
+ :raises TypeError: if serialization fails.
721
+ :raises ValueError: if data is None
722
+ :returns: The serialized query parameter
723
+ """
724
+ try:
725
+ # Treat the list aside, since we don't want to encode the div separator
726
+ if data_type.startswith("["):
727
+ internal_data_type = data_type[1:-1]
728
+ do_quote = not kwargs.get("skip_quote", False)
729
+ return self.serialize_iter(data, internal_data_type, do_quote=do_quote, **kwargs)
730
+
731
+ # Not a list, regular serialization
732
+ output = self.serialize_data(data, data_type, **kwargs)
733
+ if data_type == "bool":
734
+ output = json.dumps(output)
735
+ if kwargs.get("skip_quote") is True:
736
+ output = str(output)
737
+ else:
738
+ output = quote(str(output), safe="")
739
+ except SerializationError as exc:
740
+ raise TypeError("{} must be type {}.".format(name, data_type)) from exc
741
+ return str(output)
742
+
743
+ def header(self, name, data, data_type, **kwargs):
744
+ """Serialize data intended for a request header.
745
+
746
+ :param str name: The name of the header.
747
+ :param object data: The data to be serialized.
748
+ :param str data_type: The type to be serialized from.
749
+ :rtype: str
750
+ :raises TypeError: if serialization fails.
751
+ :raises ValueError: if data is None
752
+ :returns: The serialized header
753
+ """
754
+ try:
755
+ if data_type in ["[str]"]:
756
+ data = ["" if d is None else d for d in data]
757
+
758
+ output = self.serialize_data(data, data_type, **kwargs)
759
+ if data_type == "bool":
760
+ output = json.dumps(output)
761
+ except SerializationError as exc:
762
+ raise TypeError("{} must be type {}.".format(name, data_type)) from exc
763
+ return str(output)
764
+
765
+ def serialize_data(self, data, data_type, **kwargs):
766
+ """Serialize generic data according to supplied data type.
767
+
768
+ :param object data: The data to be serialized.
769
+ :param str data_type: The type to be serialized from.
770
+ :raises AttributeError: if required data is None.
771
+ :raises ValueError: if data is None
772
+ :raises SerializationError: if serialization fails.
773
+ :returns: The serialized data.
774
+ :rtype: str, int, float, bool, dict, list
775
+ """
776
+ if data is None:
777
+ raise ValueError("No value for given attribute")
778
+
779
+ try:
780
+ if data is CoreNull:
781
+ return None
782
+ if data_type in self.basic_types.values():
783
+ return self.serialize_basic(data, data_type, **kwargs)
784
+
785
+ if data_type in self.serialize_type:
786
+ return self.serialize_type[data_type](data, **kwargs)
787
+
788
+ # If dependencies is empty, try with current data class
789
+ # It has to be a subclass of Enum anyway
790
+ enum_type = self.dependencies.get(data_type, cast(type, data.__class__))
791
+ if issubclass(enum_type, Enum):
792
+ return Serializer.serialize_enum(data, enum_obj=enum_type)
793
+
794
+ iter_type = data_type[0] + data_type[-1]
795
+ if iter_type in self.serialize_type:
796
+ return self.serialize_type[iter_type](data, data_type[1:-1], **kwargs)
797
+
798
+ except (ValueError, TypeError) as err:
799
+ msg = "Unable to serialize value: {!r} as type: {!r}."
800
+ raise SerializationError(msg.format(data, data_type)) from err
801
+ return self._serialize(data, **kwargs)
802
+
803
+ @classmethod
804
+ def _get_custom_serializers(cls, data_type, **kwargs): # pylint: disable=inconsistent-return-statements
805
+ custom_serializer = kwargs.get("basic_types_serializers", {}).get(data_type)
806
+ if custom_serializer:
807
+ return custom_serializer
808
+ if kwargs.get("is_xml", False):
809
+ return cls._xml_basic_types_serializers.get(data_type)
810
+
811
+ @classmethod
812
+ def serialize_basic(cls, data, data_type, **kwargs):
813
+ """Serialize basic builting data type.
814
+ Serializes objects to str, int, float or bool.
815
+
816
+ Possible kwargs:
817
+ - basic_types_serializers dict[str, callable] : If set, use the callable as serializer
818
+ - is_xml bool : If set, use xml_basic_types_serializers
819
+
820
+ :param obj data: Object to be serialized.
821
+ :param str data_type: Type of object in the iterable.
822
+ :rtype: str, int, float, bool
823
+ :return: serialized object
824
+ """
825
+ custom_serializer = cls._get_custom_serializers(data_type, **kwargs)
826
+ if custom_serializer:
827
+ return custom_serializer(data)
828
+ if data_type == "str":
829
+ return cls.serialize_unicode(data)
830
+ return eval(data_type)(data) # nosec # pylint: disable=eval-used
831
+
832
+ @classmethod
833
+ def serialize_unicode(cls, data):
834
+ """Special handling for serializing unicode strings in Py2.
835
+ Encode to UTF-8 if unicode, otherwise handle as a str.
836
+
837
+ :param str data: Object to be serialized.
838
+ :rtype: str
839
+ :return: serialized object
840
+ """
841
+ try: # If I received an enum, return its value
842
+ return data.value
843
+ except AttributeError:
844
+ pass
845
+
846
+ try:
847
+ if isinstance(data, unicode): # type: ignore
848
+ # Don't change it, JSON and XML ElementTree are totally able
849
+ # to serialize correctly u'' strings
850
+ return data
851
+ except NameError:
852
+ return str(data)
853
+ return str(data)
854
+
855
+ def serialize_iter(self, data, iter_type, div=None, **kwargs):
856
+ """Serialize iterable.
857
+
858
+ Supported kwargs:
859
+ - serialization_ctxt dict : The current entry of _attribute_map, or same format.
860
+ serialization_ctxt['type'] should be same as data_type.
861
+ - is_xml bool : If set, serialize as XML
862
+
863
+ :param list data: Object to be serialized.
864
+ :param str iter_type: Type of object in the iterable.
865
+ :param str div: If set, this str will be used to combine the elements
866
+ in the iterable into a combined string. Default is 'None'.
867
+ Defaults to False.
868
+ :rtype: list, str
869
+ :return: serialized iterable
870
+ """
871
+ if isinstance(data, str):
872
+ raise SerializationError("Refuse str type as a valid iter type.")
873
+
874
+ serialization_ctxt = kwargs.get("serialization_ctxt", {})
875
+ is_xml = kwargs.get("is_xml", False)
876
+
877
+ serialized = []
878
+ for d in data:
879
+ try:
880
+ serialized.append(self.serialize_data(d, iter_type, **kwargs))
881
+ except ValueError as err:
882
+ if isinstance(err, SerializationError):
883
+ raise
884
+ serialized.append(None)
885
+
886
+ if kwargs.get("do_quote", False):
887
+ serialized = ["" if s is None else quote(str(s), safe="") for s in serialized]
888
+
889
+ if div:
890
+ serialized = ["" if s is None else str(s) for s in serialized]
891
+ serialized = div.join(serialized)
892
+
893
+ if "xml" in serialization_ctxt or is_xml:
894
+ # XML serialization is more complicated
895
+ xml_desc = serialization_ctxt.get("xml", {})
896
+ xml_name = xml_desc.get("name")
897
+ if not xml_name:
898
+ xml_name = serialization_ctxt["key"]
899
+
900
+ # Create a wrap node if necessary (use the fact that Element and list have "append")
901
+ is_wrapped = xml_desc.get("wrapped", False)
902
+ node_name = xml_desc.get("itemsName", xml_name)
903
+ if is_wrapped:
904
+ final_result = _create_xml_node(xml_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
905
+ else:
906
+ final_result = []
907
+ # All list elements to "local_node"
908
+ for el in serialized:
909
+ if isinstance(el, ET.Element):
910
+ el_node = el
911
+ else:
912
+ el_node = _create_xml_node(node_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
913
+ if el is not None: # Otherwise it writes "None" :-p
914
+ el_node.text = str(el)
915
+ final_result.append(el_node)
916
+ return final_result
917
+ return serialized
918
+
919
+ def serialize_dict(self, attr, dict_type, **kwargs):
920
+ """Serialize a dictionary of objects.
921
+
922
+ :param dict attr: Object to be serialized.
923
+ :param str dict_type: Type of object in the dictionary.
924
+ :rtype: dict
925
+ :return: serialized dictionary
926
+ """
927
+ serialization_ctxt = kwargs.get("serialization_ctxt", {})
928
+ serialized = {}
929
+ for key, value in attr.items():
930
+ try:
931
+ serialized[self.serialize_unicode(key)] = self.serialize_data(value, dict_type, **kwargs)
932
+ except ValueError as err:
933
+ if isinstance(err, SerializationError):
934
+ raise
935
+ serialized[self.serialize_unicode(key)] = None
936
+
937
+ if "xml" in serialization_ctxt:
938
+ # XML serialization is more complicated
939
+ xml_desc = serialization_ctxt["xml"]
940
+ xml_name = xml_desc["name"]
941
+
942
+ final_result = _create_xml_node(xml_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
943
+ for key, value in serialized.items():
944
+ ET.SubElement(final_result, key).text = value
945
+ return final_result
946
+
947
+ return serialized
948
+
949
+ def serialize_object(self, attr, **kwargs): # pylint: disable=too-many-return-statements
950
+ """Serialize a generic object.
951
+ This will be handled as a dictionary. If object passed in is not
952
+ a basic type (str, int, float, dict, list) it will simply be
953
+ cast to str.
954
+
955
+ :param dict attr: Object to be serialized.
956
+ :rtype: dict or str
957
+ :return: serialized object
958
+ """
959
+ if attr is None:
960
+ return None
961
+ if isinstance(attr, ET.Element):
962
+ return attr
963
+ obj_type = type(attr)
964
+ if obj_type in self.basic_types:
965
+ return self.serialize_basic(attr, self.basic_types[obj_type], **kwargs)
966
+ if obj_type is _long_type:
967
+ return self.serialize_long(attr)
968
+ if obj_type is str:
969
+ return self.serialize_unicode(attr)
970
+ if obj_type is datetime.datetime:
971
+ return self.serialize_iso(attr)
972
+ if obj_type is datetime.date:
973
+ return self.serialize_date(attr)
974
+ if obj_type is datetime.time:
975
+ return self.serialize_time(attr)
976
+ if obj_type is datetime.timedelta:
977
+ return self.serialize_duration(attr)
978
+ if obj_type is decimal.Decimal:
979
+ return self.serialize_decimal(attr)
980
+
981
+ # If it's a model or I know this dependency, serialize as a Model
982
+ if obj_type in self.dependencies.values() or isinstance(attr, Model):
983
+ return self._serialize(attr)
984
+
985
+ if obj_type == dict:
986
+ serialized = {}
987
+ for key, value in attr.items():
988
+ try:
989
+ serialized[self.serialize_unicode(key)] = self.serialize_object(value, **kwargs)
990
+ except ValueError:
991
+ serialized[self.serialize_unicode(key)] = None
992
+ return serialized
993
+
994
+ if obj_type == list:
995
+ serialized = []
996
+ for obj in attr:
997
+ try:
998
+ serialized.append(self.serialize_object(obj, **kwargs))
999
+ except ValueError:
1000
+ pass
1001
+ return serialized
1002
+ return str(attr)
1003
+
1004
+ @staticmethod
1005
+ def serialize_enum(attr, enum_obj=None):
1006
+ try:
1007
+ result = attr.value
1008
+ except AttributeError:
1009
+ result = attr
1010
+ try:
1011
+ enum_obj(result) # type: ignore
1012
+ return result
1013
+ except ValueError as exc:
1014
+ for enum_value in enum_obj: # type: ignore
1015
+ if enum_value.value.lower() == str(attr).lower():
1016
+ return enum_value.value
1017
+ error = "{!r} is not valid value for enum {!r}"
1018
+ raise SerializationError(error.format(attr, enum_obj)) from exc
1019
+
1020
+ @staticmethod
1021
+ def serialize_bytearray(attr, **kwargs): # pylint: disable=unused-argument
1022
+ """Serialize bytearray into base-64 string.
1023
+
1024
+ :param str attr: Object to be serialized.
1025
+ :rtype: str
1026
+ :return: serialized base64
1027
+ """
1028
+ return b64encode(attr).decode()
1029
+
1030
+ @staticmethod
1031
+ def serialize_base64(attr, **kwargs): # pylint: disable=unused-argument
1032
+ """Serialize str into base-64 string.
1033
+
1034
+ :param str attr: Object to be serialized.
1035
+ :rtype: str
1036
+ :return: serialized base64
1037
+ """
1038
+ encoded = b64encode(attr).decode("ascii")
1039
+ return encoded.strip("=").replace("+", "-").replace("/", "_")
1040
+
1041
+ @staticmethod
1042
+ def serialize_decimal(attr, **kwargs): # pylint: disable=unused-argument
1043
+ """Serialize Decimal object to float.
1044
+
1045
+ :param decimal attr: Object to be serialized.
1046
+ :rtype: float
1047
+ :return: serialized decimal
1048
+ """
1049
+ return float(attr)
1050
+
1051
+ @staticmethod
1052
+ def serialize_long(attr, **kwargs): # pylint: disable=unused-argument
1053
+ """Serialize long (Py2) or int (Py3).
1054
+
1055
+ :param int attr: Object to be serialized.
1056
+ :rtype: int/long
1057
+ :return: serialized long
1058
+ """
1059
+ return _long_type(attr)
1060
+
1061
+ @staticmethod
1062
+ def serialize_date(attr, **kwargs): # pylint: disable=unused-argument
1063
+ """Serialize Date object into ISO-8601 formatted string.
1064
+
1065
+ :param Date attr: Object to be serialized.
1066
+ :rtype: str
1067
+ :return: serialized date
1068
+ """
1069
+ if isinstance(attr, str):
1070
+ attr = isodate.parse_date(attr)
1071
+ t = "{:04}-{:02}-{:02}".format(attr.year, attr.month, attr.day)
1072
+ return t
1073
+
1074
+ @staticmethod
1075
+ def serialize_time(attr, **kwargs): # pylint: disable=unused-argument
1076
+ """Serialize Time object into ISO-8601 formatted string.
1077
+
1078
+ :param datetime.time attr: Object to be serialized.
1079
+ :rtype: str
1080
+ :return: serialized time
1081
+ """
1082
+ if isinstance(attr, str):
1083
+ attr = isodate.parse_time(attr)
1084
+ t = "{:02}:{:02}:{:02}".format(attr.hour, attr.minute, attr.second)
1085
+ if attr.microsecond:
1086
+ t += ".{:02}".format(attr.microsecond)
1087
+ return t
1088
+
1089
+ @staticmethod
1090
+ def serialize_duration(attr, **kwargs): # pylint: disable=unused-argument
1091
+ """Serialize TimeDelta object into ISO-8601 formatted string.
1092
+
1093
+ :param TimeDelta attr: Object to be serialized.
1094
+ :rtype: str
1095
+ :return: serialized duration
1096
+ """
1097
+ if isinstance(attr, str):
1098
+ attr = isodate.parse_duration(attr)
1099
+ return isodate.duration_isoformat(attr)
1100
+
1101
+ @staticmethod
1102
+ def serialize_rfc(attr, **kwargs): # pylint: disable=unused-argument
1103
+ """Serialize Datetime object into RFC-1123 formatted string.
1104
+
1105
+ :param Datetime attr: Object to be serialized.
1106
+ :rtype: str
1107
+ :raises TypeError: if format invalid.
1108
+ :return: serialized rfc
1109
+ """
1110
+ try:
1111
+ if not attr.tzinfo:
1112
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1113
+ utc = attr.utctimetuple()
1114
+ except AttributeError as exc:
1115
+ raise TypeError("RFC1123 object must be valid Datetime object.") from exc
1116
+
1117
+ return "{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT".format(
1118
+ Serializer.days[utc.tm_wday],
1119
+ utc.tm_mday,
1120
+ Serializer.months[utc.tm_mon],
1121
+ utc.tm_year,
1122
+ utc.tm_hour,
1123
+ utc.tm_min,
1124
+ utc.tm_sec,
1125
+ )
1126
+
1127
+ @staticmethod
1128
+ def serialize_iso(attr, **kwargs): # pylint: disable=unused-argument
1129
+ """Serialize Datetime object into ISO-8601 formatted string.
1130
+
1131
+ :param Datetime attr: Object to be serialized.
1132
+ :rtype: str
1133
+ :raises SerializationError: if format invalid.
1134
+ :return: serialized iso
1135
+ """
1136
+ if isinstance(attr, str):
1137
+ attr = isodate.parse_datetime(attr)
1138
+ try:
1139
+ if not attr.tzinfo:
1140
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1141
+ utc = attr.utctimetuple()
1142
+ if utc.tm_year > 9999 or utc.tm_year < 1:
1143
+ raise OverflowError("Hit max or min date")
1144
+
1145
+ microseconds = str(attr.microsecond).rjust(6, "0").rstrip("0").ljust(3, "0")
1146
+ if microseconds:
1147
+ microseconds = "." + microseconds
1148
+ date = "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}".format(
1149
+ utc.tm_year, utc.tm_mon, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec
1150
+ )
1151
+ return date + microseconds + "Z"
1152
+ except (ValueError, OverflowError) as err:
1153
+ msg = "Unable to serialize datetime object."
1154
+ raise SerializationError(msg) from err
1155
+ except AttributeError as err:
1156
+ msg = "ISO-8601 object must be valid Datetime object."
1157
+ raise TypeError(msg) from err
1158
+
1159
+ @staticmethod
1160
+ def serialize_unix(attr, **kwargs): # pylint: disable=unused-argument
1161
+ """Serialize Datetime object into IntTime format.
1162
+ This is represented as seconds.
1163
+
1164
+ :param Datetime attr: Object to be serialized.
1165
+ :rtype: int
1166
+ :raises SerializationError: if format invalid
1167
+ :return: serialied unix
1168
+ """
1169
+ if isinstance(attr, int):
1170
+ return attr
1171
+ try:
1172
+ if not attr.tzinfo:
1173
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1174
+ return int(calendar.timegm(attr.utctimetuple()))
1175
+ except AttributeError as exc:
1176
+ raise TypeError("Unix time object must be valid Datetime object.") from exc
1177
+
1178
+
1179
+ def rest_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument
1180
+ key = attr_desc["key"]
1181
+ working_data = data
1182
+
1183
+ while "." in key:
1184
+ # Need the cast, as for some reasons "split" is typed as list[str | Any]
1185
+ dict_keys = cast(list[str], _FLATTEN.split(key))
1186
+ if len(dict_keys) == 1:
1187
+ key = _decode_attribute_map_key(dict_keys[0])
1188
+ break
1189
+ working_key = _decode_attribute_map_key(dict_keys[0])
1190
+ working_data = working_data.get(working_key, data)
1191
+ if working_data is None:
1192
+ # If at any point while following flatten JSON path see None, it means
1193
+ # that all properties under are None as well
1194
+ return None
1195
+ key = ".".join(dict_keys[1:])
1196
+
1197
+ return working_data.get(key)
1198
+
1199
+
1200
+ def rest_key_case_insensitive_extractor( # pylint: disable=unused-argument, inconsistent-return-statements
1201
+ attr, attr_desc, data
1202
+ ):
1203
+ key = attr_desc["key"]
1204
+ working_data = data
1205
+
1206
+ while "." in key:
1207
+ dict_keys = _FLATTEN.split(key)
1208
+ if len(dict_keys) == 1:
1209
+ key = _decode_attribute_map_key(dict_keys[0])
1210
+ break
1211
+ working_key = _decode_attribute_map_key(dict_keys[0])
1212
+ working_data = attribute_key_case_insensitive_extractor(working_key, None, working_data)
1213
+ if working_data is None:
1214
+ # If at any point while following flatten JSON path see None, it means
1215
+ # that all properties under are None as well
1216
+ return None
1217
+ key = ".".join(dict_keys[1:])
1218
+
1219
+ if working_data:
1220
+ return attribute_key_case_insensitive_extractor(key, None, working_data)
1221
+
1222
+
1223
+ def last_rest_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument
1224
+ """Extract the attribute in "data" based on the last part of the JSON path key.
1225
+
1226
+ :param str attr: The attribute to extract
1227
+ :param dict attr_desc: The attribute description
1228
+ :param dict data: The data to extract from
1229
+ :rtype: object
1230
+ :returns: The extracted attribute
1231
+ """
1232
+ key = attr_desc["key"]
1233
+ dict_keys = _FLATTEN.split(key)
1234
+ return attribute_key_extractor(dict_keys[-1], None, data)
1235
+
1236
+
1237
+ def last_rest_key_case_insensitive_extractor(attr, attr_desc, data): # pylint: disable=unused-argument
1238
+ """Extract the attribute in "data" based on the last part of the JSON path key.
1239
+
1240
+ This is the case insensitive version of "last_rest_key_extractor"
1241
+ :param str attr: The attribute to extract
1242
+ :param dict attr_desc: The attribute description
1243
+ :param dict data: The data to extract from
1244
+ :rtype: object
1245
+ :returns: The extracted attribute
1246
+ """
1247
+ key = attr_desc["key"]
1248
+ dict_keys = _FLATTEN.split(key)
1249
+ return attribute_key_case_insensitive_extractor(dict_keys[-1], None, data)
1250
+
1251
+
1252
+ def attribute_key_extractor(attr, _, data):
1253
+ return data.get(attr)
1254
+
1255
+
1256
+ def attribute_key_case_insensitive_extractor(attr, _, data):
1257
+ found_key = None
1258
+ lower_attr = attr.lower()
1259
+ for key in data:
1260
+ if lower_attr == key.lower():
1261
+ found_key = key
1262
+ break
1263
+
1264
+ return data.get(found_key)
1265
+
1266
+
1267
+ def _extract_name_from_internal_type(internal_type):
1268
+ """Given an internal type XML description, extract correct XML name with namespace.
1269
+
1270
+ :param dict internal_type: An model type
1271
+ :rtype: tuple
1272
+ :returns: A tuple XML name + namespace dict
1273
+ """
1274
+ internal_type_xml_map = getattr(internal_type, "_xml_map", {})
1275
+ xml_name = internal_type_xml_map.get("name", internal_type.__name__)
1276
+ xml_ns = internal_type_xml_map.get("ns", None)
1277
+ if xml_ns:
1278
+ xml_name = "{{{}}}{}".format(xml_ns, xml_name)
1279
+ return xml_name
1280
+
1281
+
1282
+ def xml_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument,too-many-return-statements
1283
+ if isinstance(data, dict):
1284
+ return None
1285
+
1286
+ # Test if this model is XML ready first
1287
+ if not isinstance(data, ET.Element):
1288
+ return None
1289
+
1290
+ xml_desc = attr_desc.get("xml", {})
1291
+ xml_name = xml_desc.get("name", attr_desc["key"])
1292
+
1293
+ # Look for a children
1294
+ is_iter_type = attr_desc["type"].startswith("[")
1295
+ is_wrapped = xml_desc.get("wrapped", False)
1296
+ internal_type = attr_desc.get("internalType", None)
1297
+ internal_type_xml_map = getattr(internal_type, "_xml_map", {})
1298
+
1299
+ # Integrate namespace if necessary
1300
+ xml_ns = xml_desc.get("ns", internal_type_xml_map.get("ns", None))
1301
+ if xml_ns:
1302
+ xml_name = "{{{}}}{}".format(xml_ns, xml_name)
1303
+
1304
+ # If it's an attribute, that's simple
1305
+ if xml_desc.get("attr", False):
1306
+ return data.get(xml_name)
1307
+
1308
+ # If it's x-ms-text, that's simple too
1309
+ if xml_desc.get("text", False):
1310
+ return data.text
1311
+
1312
+ # Scenario where I take the local name:
1313
+ # - Wrapped node
1314
+ # - Internal type is an enum (considered basic types)
1315
+ # - Internal type has no XML/Name node
1316
+ if is_wrapped or (internal_type and (issubclass(internal_type, Enum) or "name" not in internal_type_xml_map)):
1317
+ children = data.findall(xml_name)
1318
+ # If internal type has a local name and it's not a list, I use that name
1319
+ elif not is_iter_type and internal_type and "name" in internal_type_xml_map:
1320
+ xml_name = _extract_name_from_internal_type(internal_type)
1321
+ children = data.findall(xml_name)
1322
+ # That's an array
1323
+ else:
1324
+ if internal_type: # Complex type, ignore itemsName and use the complex type name
1325
+ items_name = _extract_name_from_internal_type(internal_type)
1326
+ else:
1327
+ items_name = xml_desc.get("itemsName", xml_name)
1328
+ children = data.findall(items_name)
1329
+
1330
+ if len(children) == 0:
1331
+ if is_iter_type:
1332
+ if is_wrapped:
1333
+ return None # is_wrapped no node, we want None
1334
+ return [] # not wrapped, assume empty list
1335
+ return None # Assume it's not there, maybe an optional node.
1336
+
1337
+ # If is_iter_type and not wrapped, return all found children
1338
+ if is_iter_type:
1339
+ if not is_wrapped:
1340
+ return children
1341
+ # Iter and wrapped, should have found one node only (the wrap one)
1342
+ if len(children) != 1:
1343
+ raise DeserializationError(
1344
+ "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?".format(
1345
+ xml_name
1346
+ )
1347
+ )
1348
+ return list(children[0]) # Might be empty list and that's ok.
1349
+
1350
+ # Here it's not a itertype, we should have found one element only or empty
1351
+ if len(children) > 1:
1352
+ raise DeserializationError("Find several XML '{}' where it was not expected".format(xml_name))
1353
+ return children[0]
1354
+
1355
+
1356
+ class Deserializer:
1357
+ """Response object model deserializer.
1358
+
1359
+ :param dict classes: Class type dictionary for deserializing complex types.
1360
+ :ivar list key_extractors: Ordered list of extractors to be used by this deserializer.
1361
+ """
1362
+
1363
+ basic_types = {str: "str", int: "int", bool: "bool", float: "float"}
1364
+
1365
+ valid_date = re.compile(r"\d{4}[-]\d{2}[-]\d{2}T\d{2}:\d{2}:\d{2}\.?\d*Z?[-+]?[\d{2}]?:?[\d{2}]?")
1366
+
1367
+ def __init__(self, classes: Optional[Mapping[str, type]] = None) -> None:
1368
+ self.deserialize_type = {
1369
+ "iso-8601": Deserializer.deserialize_iso,
1370
+ "rfc-1123": Deserializer.deserialize_rfc,
1371
+ "unix-time": Deserializer.deserialize_unix,
1372
+ "duration": Deserializer.deserialize_duration,
1373
+ "date": Deserializer.deserialize_date,
1374
+ "time": Deserializer.deserialize_time,
1375
+ "decimal": Deserializer.deserialize_decimal,
1376
+ "long": Deserializer.deserialize_long,
1377
+ "bytearray": Deserializer.deserialize_bytearray,
1378
+ "base64": Deserializer.deserialize_base64,
1379
+ "object": self.deserialize_object,
1380
+ "[]": self.deserialize_iter,
1381
+ "{}": self.deserialize_dict,
1382
+ }
1383
+ self.deserialize_expected_types = {
1384
+ "duration": (isodate.Duration, datetime.timedelta),
1385
+ "iso-8601": (datetime.datetime),
1386
+ }
1387
+ self.dependencies: dict[str, type] = dict(classes) if classes else {}
1388
+ self.key_extractors = [rest_key_extractor, xml_key_extractor]
1389
+ # Additional properties only works if the "rest_key_extractor" is used to
1390
+ # extract the keys. Making it to work whatever the key extractor is too much
1391
+ # complicated, with no real scenario for now.
1392
+ # So adding a flag to disable additional properties detection. This flag should be
1393
+ # used if your expect the deserialization to NOT come from a JSON REST syntax.
1394
+ # Otherwise, result are unexpected
1395
+ self.additional_properties_detection = True
1396
+
1397
+ def __call__(self, target_obj, response_data, content_type=None):
1398
+ """Call the deserializer to process a REST response.
1399
+
1400
+ :param str target_obj: Target data type to deserialize to.
1401
+ :param requests.Response response_data: REST response object.
1402
+ :param str content_type: Swagger "produces" if available.
1403
+ :raises DeserializationError: if deserialization fails.
1404
+ :return: Deserialized object.
1405
+ :rtype: object
1406
+ """
1407
+ data = self._unpack_content(response_data, content_type)
1408
+ return self._deserialize(target_obj, data)
1409
+
1410
+ def _deserialize(self, target_obj, data): # pylint: disable=inconsistent-return-statements
1411
+ """Call the deserializer on a model.
1412
+
1413
+ Data needs to be already deserialized as JSON or XML ElementTree
1414
+
1415
+ :param str target_obj: Target data type to deserialize to.
1416
+ :param object data: Object to deserialize.
1417
+ :raises DeserializationError: if deserialization fails.
1418
+ :return: Deserialized object.
1419
+ :rtype: object
1420
+ """
1421
+ # This is already a model, go recursive just in case
1422
+ if hasattr(data, "_attribute_map"):
1423
+ constants = [name for name, config in getattr(data, "_validation", {}).items() if config.get("constant")]
1424
+ try:
1425
+ for attr, mapconfig in data._attribute_map.items(): # pylint: disable=protected-access
1426
+ if attr in constants:
1427
+ continue
1428
+ value = getattr(data, attr)
1429
+ if value is None:
1430
+ continue
1431
+ local_type = mapconfig["type"]
1432
+ internal_data_type = local_type.strip("[]{}")
1433
+ if internal_data_type not in self.dependencies or isinstance(internal_data_type, Enum):
1434
+ continue
1435
+ setattr(data, attr, self._deserialize(local_type, value))
1436
+ return data
1437
+ except AttributeError:
1438
+ return
1439
+
1440
+ response, class_name = self._classify_target(target_obj, data)
1441
+
1442
+ if isinstance(response, str):
1443
+ return self.deserialize_data(data, response)
1444
+ if isinstance(response, type) and issubclass(response, Enum):
1445
+ return self.deserialize_enum(data, response)
1446
+
1447
+ if data is None or data is CoreNull:
1448
+ return data
1449
+ try:
1450
+ attributes = response._attribute_map # type: ignore # pylint: disable=protected-access
1451
+ d_attrs = {}
1452
+ for attr, attr_desc in attributes.items():
1453
+ # Check empty string. If it's not empty, someone has a real "additionalProperties"...
1454
+ if attr == "additional_properties" and attr_desc["key"] == "":
1455
+ continue
1456
+ raw_value = None
1457
+ # Enhance attr_desc with some dynamic data
1458
+ attr_desc = attr_desc.copy() # Do a copy, do not change the real one
1459
+ internal_data_type = attr_desc["type"].strip("[]{}")
1460
+ if internal_data_type in self.dependencies:
1461
+ attr_desc["internalType"] = self.dependencies[internal_data_type]
1462
+
1463
+ for key_extractor in self.key_extractors:
1464
+ found_value = key_extractor(attr, attr_desc, data)
1465
+ if found_value is not None:
1466
+ if raw_value is not None and raw_value != found_value:
1467
+ msg = (
1468
+ "Ignoring extracted value '%s' from %s for key '%s'"
1469
+ " (duplicate extraction, follow extractors order)"
1470
+ )
1471
+ _LOGGER.warning(msg, found_value, key_extractor, attr)
1472
+ continue
1473
+ raw_value = found_value
1474
+
1475
+ value = self.deserialize_data(raw_value, attr_desc["type"])
1476
+ d_attrs[attr] = value
1477
+ except (AttributeError, TypeError, KeyError) as err:
1478
+ msg = "Unable to deserialize to object: " + class_name # type: ignore
1479
+ raise DeserializationError(msg) from err
1480
+ additional_properties = self._build_additional_properties(attributes, data)
1481
+ return self._instantiate_model(response, d_attrs, additional_properties)
1482
+
1483
+ def _build_additional_properties(self, attribute_map, data):
1484
+ if not self.additional_properties_detection:
1485
+ return None
1486
+ if "additional_properties" in attribute_map and attribute_map.get("additional_properties", {}).get("key") != "":
1487
+ # Check empty string. If it's not empty, someone has a real "additionalProperties"
1488
+ return None
1489
+ if isinstance(data, ET.Element):
1490
+ data = {el.tag: el.text for el in data}
1491
+
1492
+ known_keys = {
1493
+ _decode_attribute_map_key(_FLATTEN.split(desc["key"])[0])
1494
+ for desc in attribute_map.values()
1495
+ if desc["key"] != ""
1496
+ }
1497
+ present_keys = set(data.keys())
1498
+ missing_keys = present_keys - known_keys
1499
+ return {key: data[key] for key in missing_keys}
1500
+
1501
+ def _classify_target(self, target, data):
1502
+ """Check to see whether the deserialization target object can
1503
+ be classified into a subclass.
1504
+ Once classification has been determined, initialize object.
1505
+
1506
+ :param str target: The target object type to deserialize to.
1507
+ :param str/dict data: The response data to deserialize.
1508
+ :return: The classified target object and its class name.
1509
+ :rtype: tuple
1510
+ """
1511
+ if target is None:
1512
+ return None, None
1513
+
1514
+ if isinstance(target, str):
1515
+ try:
1516
+ target = self.dependencies[target]
1517
+ except KeyError:
1518
+ return target, target
1519
+
1520
+ try:
1521
+ target = target._classify(data, self.dependencies) # type: ignore # pylint: disable=protected-access
1522
+ except AttributeError:
1523
+ pass # Target is not a Model, no classify
1524
+ return target, target.__class__.__name__ # type: ignore
1525
+
1526
+ def failsafe_deserialize(self, target_obj, data, content_type=None):
1527
+ """Ignores any errors encountered in deserialization,
1528
+ and falls back to not deserializing the object. Recommended
1529
+ for use in error deserialization, as we want to return the
1530
+ HttpResponseError to users, and not have them deal with
1531
+ a deserialization error.
1532
+
1533
+ :param str target_obj: The target object type to deserialize to.
1534
+ :param str/dict data: The response data to deserialize.
1535
+ :param str content_type: Swagger "produces" if available.
1536
+ :return: Deserialized object.
1537
+ :rtype: object
1538
+ """
1539
+ try:
1540
+ return self(target_obj, data, content_type=content_type)
1541
+ except: # pylint: disable=bare-except
1542
+ _LOGGER.debug(
1543
+ "Ran into a deserialization error. Ignoring since this is failsafe deserialization", exc_info=True
1544
+ )
1545
+ return None
1546
+
1547
+ @staticmethod
1548
+ def _unpack_content(raw_data, content_type=None):
1549
+ """Extract the correct structure for deserialization.
1550
+
1551
+ If raw_data is a PipelineResponse, try to extract the result of RawDeserializer.
1552
+ if we can't, raise. Your Pipeline should have a RawDeserializer.
1553
+
1554
+ If not a pipeline response and raw_data is bytes or string, use content-type
1555
+ to decode it. If no content-type, try JSON.
1556
+
1557
+ If raw_data is something else, bypass all logic and return it directly.
1558
+
1559
+ :param obj raw_data: Data to be processed.
1560
+ :param str content_type: How to parse if raw_data is a string/bytes.
1561
+ :raises JSONDecodeError: If JSON is requested and parsing is impossible.
1562
+ :raises UnicodeDecodeError: If bytes is not UTF8
1563
+ :rtype: object
1564
+ :return: Unpacked content.
1565
+ """
1566
+ # Assume this is enough to detect a Pipeline Response without importing it
1567
+ context = getattr(raw_data, "context", {})
1568
+ if context:
1569
+ if RawDeserializer.CONTEXT_NAME in context:
1570
+ return context[RawDeserializer.CONTEXT_NAME]
1571
+ raise ValueError("This pipeline didn't have the RawDeserializer policy; can't deserialize")
1572
+
1573
+ # Assume this is enough to recognize universal_http.ClientResponse without importing it
1574
+ if hasattr(raw_data, "body"):
1575
+ return RawDeserializer.deserialize_from_http_generics(raw_data.text(), raw_data.headers)
1576
+
1577
+ # Assume this enough to recognize requests.Response without importing it.
1578
+ if hasattr(raw_data, "_content_consumed"):
1579
+ return RawDeserializer.deserialize_from_http_generics(raw_data.text, raw_data.headers)
1580
+
1581
+ if isinstance(raw_data, (str, bytes)) or hasattr(raw_data, "read"):
1582
+ return RawDeserializer.deserialize_from_text(raw_data, content_type) # type: ignore
1583
+ return raw_data
1584
+
1585
+ def _instantiate_model(self, response, attrs, additional_properties=None):
1586
+ """Instantiate a response model passing in deserialized args.
1587
+
1588
+ :param Response response: The response model class.
1589
+ :param dict attrs: The deserialized response attributes.
1590
+ :param dict additional_properties: Additional properties to be set.
1591
+ :rtype: Response
1592
+ :return: The instantiated response model.
1593
+ """
1594
+ if callable(response):
1595
+ subtype = getattr(response, "_subtype_map", {})
1596
+ try:
1597
+ readonly = [
1598
+ k
1599
+ for k, v in response._validation.items() # pylint: disable=protected-access # type: ignore
1600
+ if v.get("readonly")
1601
+ ]
1602
+ const = [
1603
+ k
1604
+ for k, v in response._validation.items() # pylint: disable=protected-access # type: ignore
1605
+ if v.get("constant")
1606
+ ]
1607
+ kwargs = {k: v for k, v in attrs.items() if k not in subtype and k not in readonly + const}
1608
+ response_obj = response(**kwargs)
1609
+ for attr in readonly:
1610
+ setattr(response_obj, attr, attrs.get(attr))
1611
+ if additional_properties:
1612
+ response_obj.additional_properties = additional_properties # type: ignore
1613
+ return response_obj
1614
+ except TypeError as err:
1615
+ msg = "Unable to deserialize {} into model {}. ".format(kwargs, response) # type: ignore
1616
+ raise DeserializationError(msg + str(err)) from err
1617
+ else:
1618
+ try:
1619
+ for attr, value in attrs.items():
1620
+ setattr(response, attr, value)
1621
+ return response
1622
+ except Exception as exp:
1623
+ msg = "Unable to populate response model. "
1624
+ msg += "Type: {}, Error: {}".format(type(response), exp)
1625
+ raise DeserializationError(msg) from exp
1626
+
1627
+ def deserialize_data(self, data, data_type): # pylint: disable=too-many-return-statements
1628
+ """Process data for deserialization according to data type.
1629
+
1630
+ :param str data: The response string to be deserialized.
1631
+ :param str data_type: The type to deserialize to.
1632
+ :raises DeserializationError: if deserialization fails.
1633
+ :return: Deserialized object.
1634
+ :rtype: object
1635
+ """
1636
+ if data is None:
1637
+ return data
1638
+
1639
+ try:
1640
+ if not data_type:
1641
+ return data
1642
+ if data_type in self.basic_types.values():
1643
+ return self.deserialize_basic(data, data_type)
1644
+ if data_type in self.deserialize_type:
1645
+ if isinstance(data, self.deserialize_expected_types.get(data_type, tuple())):
1646
+ return data
1647
+
1648
+ is_a_text_parsing_type = lambda x: x not in [ # pylint: disable=unnecessary-lambda-assignment
1649
+ "object",
1650
+ "[]",
1651
+ r"{}",
1652
+ ]
1653
+ if isinstance(data, ET.Element) and is_a_text_parsing_type(data_type) and not data.text:
1654
+ return None
1655
+ data_val = self.deserialize_type[data_type](data)
1656
+ return data_val
1657
+
1658
+ iter_type = data_type[0] + data_type[-1]
1659
+ if iter_type in self.deserialize_type:
1660
+ return self.deserialize_type[iter_type](data, data_type[1:-1])
1661
+
1662
+ obj_type = self.dependencies[data_type]
1663
+ if issubclass(obj_type, Enum):
1664
+ if isinstance(data, ET.Element):
1665
+ data = data.text
1666
+ return self.deserialize_enum(data, obj_type)
1667
+
1668
+ except (ValueError, TypeError, AttributeError) as err:
1669
+ msg = "Unable to deserialize response data."
1670
+ msg += " Data: {}, {}".format(data, data_type)
1671
+ raise DeserializationError(msg) from err
1672
+ return self._deserialize(obj_type, data)
1673
+
1674
+ def deserialize_iter(self, attr, iter_type):
1675
+ """Deserialize an iterable.
1676
+
1677
+ :param list attr: Iterable to be deserialized.
1678
+ :param str iter_type: The type of object in the iterable.
1679
+ :return: Deserialized iterable.
1680
+ :rtype: list
1681
+ """
1682
+ if attr is None:
1683
+ return None
1684
+ if isinstance(attr, ET.Element): # If I receive an element here, get the children
1685
+ attr = list(attr)
1686
+ if not isinstance(attr, (list, set)):
1687
+ raise DeserializationError("Cannot deserialize as [{}] an object of type {}".format(iter_type, type(attr)))
1688
+ return [self.deserialize_data(a, iter_type) for a in attr]
1689
+
1690
+ def deserialize_dict(self, attr, dict_type):
1691
+ """Deserialize a dictionary.
1692
+
1693
+ :param dict/list attr: Dictionary to be deserialized. Also accepts
1694
+ a list of key, value pairs.
1695
+ :param str dict_type: The object type of the items in the dictionary.
1696
+ :return: Deserialized dictionary.
1697
+ :rtype: dict
1698
+ """
1699
+ if isinstance(attr, list):
1700
+ return {x["key"]: self.deserialize_data(x["value"], dict_type) for x in attr}
1701
+
1702
+ if isinstance(attr, ET.Element):
1703
+ # Transform <Key>value</Key> into {"Key": "value"}
1704
+ attr = {el.tag: el.text for el in attr}
1705
+ return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
1706
+
1707
+ def deserialize_object(self, attr, **kwargs): # pylint: disable=too-many-return-statements
1708
+ """Deserialize a generic object.
1709
+ This will be handled as a dictionary.
1710
+
1711
+ :param dict attr: Dictionary to be deserialized.
1712
+ :return: Deserialized object.
1713
+ :rtype: dict
1714
+ :raises TypeError: if non-builtin datatype encountered.
1715
+ """
1716
+ if attr is None:
1717
+ return None
1718
+ if isinstance(attr, ET.Element):
1719
+ # Do no recurse on XML, just return the tree as-is
1720
+ return attr
1721
+ if isinstance(attr, str):
1722
+ return self.deserialize_basic(attr, "str")
1723
+ obj_type = type(attr)
1724
+ if obj_type in self.basic_types:
1725
+ return self.deserialize_basic(attr, self.basic_types[obj_type])
1726
+ if obj_type is _long_type:
1727
+ return self.deserialize_long(attr)
1728
+
1729
+ if obj_type == dict:
1730
+ deserialized = {}
1731
+ for key, value in attr.items():
1732
+ try:
1733
+ deserialized[key] = self.deserialize_object(value, **kwargs)
1734
+ except ValueError:
1735
+ deserialized[key] = None
1736
+ return deserialized
1737
+
1738
+ if obj_type == list:
1739
+ deserialized = []
1740
+ for obj in attr:
1741
+ try:
1742
+ deserialized.append(self.deserialize_object(obj, **kwargs))
1743
+ except ValueError:
1744
+ pass
1745
+ return deserialized
1746
+
1747
+ error = "Cannot deserialize generic object with type: "
1748
+ raise TypeError(error + str(obj_type))
1749
+
1750
+ def deserialize_basic(self, attr, data_type): # pylint: disable=too-many-return-statements
1751
+ """Deserialize basic builtin data type from string.
1752
+ Will attempt to convert to str, int, float and bool.
1753
+ This function will also accept '1', '0', 'true' and 'false' as
1754
+ valid bool values.
1755
+
1756
+ :param str attr: response string to be deserialized.
1757
+ :param str data_type: deserialization data type.
1758
+ :return: Deserialized basic type.
1759
+ :rtype: str, int, float or bool
1760
+ :raises TypeError: if string format is not valid.
1761
+ """
1762
+ # If we're here, data is supposed to be a basic type.
1763
+ # If it's still an XML node, take the text
1764
+ if isinstance(attr, ET.Element):
1765
+ attr = attr.text
1766
+ if not attr:
1767
+ if data_type == "str":
1768
+ # None or '', node <a/> is empty string.
1769
+ return ""
1770
+ # None or '', node <a/> with a strong type is None.
1771
+ # Don't try to model "empty bool" or "empty int"
1772
+ return None
1773
+
1774
+ if data_type == "bool":
1775
+ if attr in [True, False, 1, 0]:
1776
+ return bool(attr)
1777
+ if isinstance(attr, str):
1778
+ if attr.lower() in ["true", "1"]:
1779
+ return True
1780
+ if attr.lower() in ["false", "0"]:
1781
+ return False
1782
+ raise TypeError("Invalid boolean value: {}".format(attr))
1783
+
1784
+ if data_type == "str":
1785
+ return self.deserialize_unicode(attr)
1786
+ return eval(data_type)(attr) # nosec # pylint: disable=eval-used
1787
+
1788
+ @staticmethod
1789
+ def deserialize_unicode(data):
1790
+ """Preserve unicode objects in Python 2, otherwise return data
1791
+ as a string.
1792
+
1793
+ :param str data: response string to be deserialized.
1794
+ :return: Deserialized string.
1795
+ :rtype: str or unicode
1796
+ """
1797
+ # We might be here because we have an enum modeled as string,
1798
+ # and we try to deserialize a partial dict with enum inside
1799
+ if isinstance(data, Enum):
1800
+ return data
1801
+
1802
+ # Consider this is real string
1803
+ try:
1804
+ if isinstance(data, unicode): # type: ignore
1805
+ return data
1806
+ except NameError:
1807
+ return str(data)
1808
+ return str(data)
1809
+
1810
+ @staticmethod
1811
+ def deserialize_enum(data, enum_obj):
1812
+ """Deserialize string into enum object.
1813
+
1814
+ If the string is not a valid enum value it will be returned as-is
1815
+ and a warning will be logged.
1816
+
1817
+ :param str data: Response string to be deserialized. If this value is
1818
+ None or invalid it will be returned as-is.
1819
+ :param Enum enum_obj: Enum object to deserialize to.
1820
+ :return: Deserialized enum object.
1821
+ :rtype: Enum
1822
+ """
1823
+ if isinstance(data, enum_obj) or data is None:
1824
+ return data
1825
+ if isinstance(data, Enum):
1826
+ data = data.value
1827
+ if isinstance(data, int):
1828
+ # Workaround. We might consider remove it in the future.
1829
+ try:
1830
+ return list(enum_obj.__members__.values())[data]
1831
+ except IndexError as exc:
1832
+ error = "{!r} is not a valid index for enum {!r}"
1833
+ raise DeserializationError(error.format(data, enum_obj)) from exc
1834
+ try:
1835
+ return enum_obj(str(data))
1836
+ except ValueError:
1837
+ for enum_value in enum_obj:
1838
+ if enum_value.value.lower() == str(data).lower():
1839
+ return enum_value
1840
+ # We don't fail anymore for unknown value, we deserialize as a string
1841
+ _LOGGER.warning("Deserializer is not able to find %s as valid enum in %s", data, enum_obj)
1842
+ return Deserializer.deserialize_unicode(data)
1843
+
1844
+ @staticmethod
1845
+ def deserialize_bytearray(attr):
1846
+ """Deserialize string into bytearray.
1847
+
1848
+ :param str attr: response string to be deserialized.
1849
+ :return: Deserialized bytearray
1850
+ :rtype: bytearray
1851
+ :raises TypeError: if string format invalid.
1852
+ """
1853
+ if isinstance(attr, ET.Element):
1854
+ attr = attr.text
1855
+ return bytearray(b64decode(attr)) # type: ignore
1856
+
1857
+ @staticmethod
1858
+ def deserialize_base64(attr):
1859
+ """Deserialize base64 encoded string into string.
1860
+
1861
+ :param str attr: response string to be deserialized.
1862
+ :return: Deserialized base64 string
1863
+ :rtype: bytearray
1864
+ :raises TypeError: if string format invalid.
1865
+ """
1866
+ if isinstance(attr, ET.Element):
1867
+ attr = attr.text
1868
+ padding = "=" * (3 - (len(attr) + 3) % 4) # type: ignore
1869
+ attr = attr + padding # type: ignore
1870
+ encoded = attr.replace("-", "+").replace("_", "/")
1871
+ return b64decode(encoded)
1872
+
1873
+ @staticmethod
1874
+ def deserialize_decimal(attr):
1875
+ """Deserialize string into Decimal object.
1876
+
1877
+ :param str attr: response string to be deserialized.
1878
+ :return: Deserialized decimal
1879
+ :raises DeserializationError: if string format invalid.
1880
+ :rtype: decimal
1881
+ """
1882
+ if isinstance(attr, ET.Element):
1883
+ attr = attr.text
1884
+ try:
1885
+ return decimal.Decimal(str(attr)) # type: ignore
1886
+ except decimal.DecimalException as err:
1887
+ msg = "Invalid decimal {}".format(attr)
1888
+ raise DeserializationError(msg) from err
1889
+
1890
+ @staticmethod
1891
+ def deserialize_long(attr):
1892
+ """Deserialize string into long (Py2) or int (Py3).
1893
+
1894
+ :param str attr: response string to be deserialized.
1895
+ :return: Deserialized int
1896
+ :rtype: long or int
1897
+ :raises ValueError: if string format invalid.
1898
+ """
1899
+ if isinstance(attr, ET.Element):
1900
+ attr = attr.text
1901
+ return _long_type(attr) # type: ignore
1902
+
1903
+ @staticmethod
1904
+ def deserialize_duration(attr):
1905
+ """Deserialize ISO-8601 formatted string into TimeDelta object.
1906
+
1907
+ :param str attr: response string to be deserialized.
1908
+ :return: Deserialized duration
1909
+ :rtype: TimeDelta
1910
+ :raises DeserializationError: if string format invalid.
1911
+ """
1912
+ if isinstance(attr, ET.Element):
1913
+ attr = attr.text
1914
+ try:
1915
+ duration = isodate.parse_duration(attr)
1916
+ except (ValueError, OverflowError, AttributeError) as err:
1917
+ msg = "Cannot deserialize duration object."
1918
+ raise DeserializationError(msg) from err
1919
+ return duration
1920
+
1921
+ @staticmethod
1922
+ def deserialize_date(attr):
1923
+ """Deserialize ISO-8601 formatted string into Date object.
1924
+
1925
+ :param str attr: response string to be deserialized.
1926
+ :return: Deserialized date
1927
+ :rtype: Date
1928
+ :raises DeserializationError: if string format invalid.
1929
+ """
1930
+ if isinstance(attr, ET.Element):
1931
+ attr = attr.text
1932
+ if re.search(r"[^\W\d_]", attr, re.I + re.U): # type: ignore
1933
+ raise DeserializationError("Date must have only digits and -. Received: %s" % attr)
1934
+ # This must NOT use defaultmonth/defaultday. Using None ensure this raises an exception.
1935
+ return isodate.parse_date(attr, defaultmonth=0, defaultday=0)
1936
+
1937
+ @staticmethod
1938
+ def deserialize_time(attr):
1939
+ """Deserialize ISO-8601 formatted string into time object.
1940
+
1941
+ :param str attr: response string to be deserialized.
1942
+ :return: Deserialized time
1943
+ :rtype: datetime.time
1944
+ :raises DeserializationError: if string format invalid.
1945
+ """
1946
+ if isinstance(attr, ET.Element):
1947
+ attr = attr.text
1948
+ if re.search(r"[^\W\d_]", attr, re.I + re.U): # type: ignore
1949
+ raise DeserializationError("Date must have only digits and -. Received: %s" % attr)
1950
+ return isodate.parse_time(attr)
1951
+
1952
+ @staticmethod
1953
+ def deserialize_rfc(attr):
1954
+ """Deserialize RFC-1123 formatted string into Datetime object.
1955
+
1956
+ :param str attr: response string to be deserialized.
1957
+ :return: Deserialized RFC datetime
1958
+ :rtype: Datetime
1959
+ :raises DeserializationError: if string format invalid.
1960
+ """
1961
+ if isinstance(attr, ET.Element):
1962
+ attr = attr.text
1963
+ try:
1964
+ parsed_date = email.utils.parsedate_tz(attr) # type: ignore
1965
+ date_obj = datetime.datetime(
1966
+ *parsed_date[:6], tzinfo=datetime.timezone(datetime.timedelta(minutes=(parsed_date[9] or 0) / 60))
1967
+ )
1968
+ if not date_obj.tzinfo:
1969
+ date_obj = date_obj.astimezone(tz=TZ_UTC)
1970
+ except ValueError as err:
1971
+ msg = "Cannot deserialize to rfc datetime object."
1972
+ raise DeserializationError(msg) from err
1973
+ return date_obj
1974
+
1975
+ @staticmethod
1976
+ def deserialize_iso(attr):
1977
+ """Deserialize ISO-8601 formatted string into Datetime object.
1978
+
1979
+ :param str attr: response string to be deserialized.
1980
+ :return: Deserialized ISO datetime
1981
+ :rtype: Datetime
1982
+ :raises DeserializationError: if string format invalid.
1983
+ """
1984
+ if isinstance(attr, ET.Element):
1985
+ attr = attr.text
1986
+ try:
1987
+ attr = attr.upper() # type: ignore
1988
+ match = Deserializer.valid_date.match(attr)
1989
+ if not match:
1990
+ raise ValueError("Invalid datetime string: " + attr)
1991
+
1992
+ check_decimal = attr.split(".")
1993
+ if len(check_decimal) > 1:
1994
+ decimal_str = ""
1995
+ for digit in check_decimal[1]:
1996
+ if digit.isdigit():
1997
+ decimal_str += digit
1998
+ else:
1999
+ break
2000
+ if len(decimal_str) > 6:
2001
+ attr = attr.replace(decimal_str, decimal_str[0:6])
2002
+
2003
+ date_obj = isodate.parse_datetime(attr)
2004
+ test_utc = date_obj.utctimetuple()
2005
+ if test_utc.tm_year > 9999 or test_utc.tm_year < 1:
2006
+ raise OverflowError("Hit max or min date")
2007
+ except (ValueError, OverflowError, AttributeError) as err:
2008
+ msg = "Cannot deserialize datetime object."
2009
+ raise DeserializationError(msg) from err
2010
+ return date_obj
2011
+
2012
+ @staticmethod
2013
+ def deserialize_unix(attr):
2014
+ """Serialize Datetime object into IntTime format.
2015
+ This is represented as seconds.
2016
+
2017
+ :param int attr: Object to be serialized.
2018
+ :return: Deserialized datetime
2019
+ :rtype: Datetime
2020
+ :raises DeserializationError: if format invalid
2021
+ """
2022
+ if isinstance(attr, ET.Element):
2023
+ attr = int(attr.text) # type: ignore
2024
+ try:
2025
+ attr = int(attr)
2026
+ date_obj = datetime.datetime.fromtimestamp(attr, TZ_UTC)
2027
+ except ValueError as err:
2028
+ msg = "Cannot deserialize to unix datetime object."
2029
+ raise DeserializationError(msg) from err
2030
+ return date_obj