1# The MIT License (MIT)
2#
3# Copyright (c) 2019 Looker Data Sciences, Inc.
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23"""Deserialize API response into models
24"""
25import datetime
26import enum
27import functools
28import json
29import keyword
30import sys
31from typing import (
32 Callable,
33 MutableMapping,
34 Sequence,
35 Type,
36 Union,
37)
38
39import cattr
40from cattrs.cols import is_sequence, list_structure_factory
41
42from looker_sdk.rtl import model, hooks
43
44
45class DeserializeError(Exception):
46 """Improperly formatted data to deserialize."""
47
48
49TModelOrSequence = Union[
50 MutableMapping[str, str],
51 Sequence[int],
52 Sequence[str],
53 model.Model,
54 Sequence[model.Model],
55]
56TDeserializeReturn = TModelOrSequence
57TStructure = Union[Type[Sequence[int]], Type[Sequence[str]], Type[TDeserializeReturn]]
58TDeserialize = Callable[[str, TStructure], TDeserializeReturn]
59TSerialize = Callable[[TModelOrSequence], bytes]
60
61
62def deserialize(
63 *, data: str, structure: TStructure, converter: cattr.Converter
64) -> TDeserializeReturn:
65 """Translate API data into models."""
66 try:
67 data = json.loads(data)
68 except json.JSONDecodeError as ex:
69 raise DeserializeError(f"Bad json {ex}")
70 try:
71 converter.register_structure_hook_factory(is_sequence, list_structure_factory)
72 response: TDeserializeReturn = converter.structure( # type: ignore
73 data, structure
74 )
75 except (TypeError, AttributeError) as ex:
76 raise DeserializeError(f"Bad data {ex}")
77 return response
78
79
80def serialize(*, api_model: TModelOrSequence, converter: cattr.Converter) -> bytes:
81 """Translate api_model into formdata encoded json bytes"""
82 data = converter.unstructure(api_model) # type: ignore
83 return json.dumps(data,default=lambda o: o.__dict__).encode("utf-8") # type: ignore
84
85
86def forward_ref_structure_hook(context, converter, data, forward_ref):
87 """Applied to ForwardRef model and enum annotations
88
89 - Map reserved words in json keys to approriate (safe) names in model.
90 - handle ForwardRef types until github.com/Tinche/cattrs/pull/42/ is fixed
91 Note: this is the reason we need a "context" param and have to use a
92 partial func to register the hook. Once the issue is resolved we can
93 remove "context" and the partial.
94 """
95 data = hooks.tr_data_keys(data)
96 actual_type = eval(forward_ref.__forward_arg__, context, locals())
97 if issubclass(actual_type, enum.Enum):
98 instance = converter.structure(data, actual_type)
99 elif issubclass(actual_type, model.Model):
100 # cannot use converter.structure - recursion error
101 instance = converter.structure_attrs_fromdict(data, actual_type)
102 else:
103 raise DeserializeError(f"Unknown type to deserialize: {actual_type}")
104 return instance
105
106
107def translate_keys_structure_hook(converter, data, model_type):
108 """Applied only to models.Model"""
109 new_data = hooks.tr_data_keys(data)
110 ret = converter.structure_attrs_fromdict(new_data, model_type)
111 return ret
112
113converter40 = cattr.Converter()
114deserialize40 = functools.partial(deserialize, converter=converter40)
115serialize40 = functools.partial(serialize, converter=converter40)
116
117converter40.register_structure_hook(datetime.datetime, hooks.datetime_structure_hook)
118unstructure_hook40 = functools.partial(hooks.unstructure_hook, converter40) # type: ignore
119converter40.register_unstructure_hook(model.Model, unstructure_hook40) # type: ignore
120converter40.register_unstructure_hook(
121 datetime.datetime, hooks.datetime_unstructure_hook # type: ignore
122)