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