looker-sdk 24.18.0__py3-none-any.whl → 24.20.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.
@@ -0,0 +1,230 @@
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
+ """Base model for all generated models
24
+ """
25
+
26
+ import collections
27
+ import datetime
28
+ import enum
29
+ import functools
30
+ import keyword
31
+ from typing import Any, Iterable, Optional, Sequence, TypeVar, cast
32
+
33
+ import cattr
34
+
35
+ from looker_sdk.rtl import hooks
36
+
37
+ try:
38
+ from typing import ForwardRef # type: ignore
39
+ except ImportError:
40
+ from typing import _ForwardRef as ForwardRef # type: ignore
41
+
42
+
43
+ EXPLICIT_NULL = cast(Any, "EXPLICIT_NULL") # type:ignore
44
+
45
+
46
+ class Model:
47
+ """Base model for all generated models."""
48
+
49
+ def _get_converter(self):
50
+ if not hasattr(self, "_converter"):
51
+ converter = cattr.Converter()
52
+ converter.register_unstructure_hook(
53
+ datetime.datetime, hooks.datetime_unstructure_hook
54
+ )
55
+ uh = functools.partial(hooks.unstructure_hook, converter)
56
+ converter.register_unstructure_hook(Model, uh) # type: ignore
57
+ self._converter = converter
58
+ return self._converter
59
+
60
+ def _key_to_attr(self, key):
61
+ """Appends the trailing _ to python reserved words."""
62
+ if key[-1] == "_":
63
+ raise KeyError(key)
64
+ if key in keyword.kwlist:
65
+ key = f"{key}_"
66
+ return key
67
+
68
+ def __getitem__(self, key):
69
+ key = self._key_to_attr(key)
70
+ try:
71
+ ret = getattr(self, key)
72
+ except AttributeError:
73
+ raise KeyError(key)
74
+
75
+ if isinstance(ret, enum.Enum):
76
+ ret = ret.value
77
+ return ret
78
+
79
+ def __setitem__(self, key, value):
80
+ key = self._key_to_attr(key)
81
+ if not hasattr(self, key):
82
+ raise AttributeError(
83
+ f"'{self.__class__.__name__}' object has no attribute '{key}'"
84
+ )
85
+ annotation = self.__annotations__[key]
86
+ if isinstance(annotation, ForwardRef):
87
+ actual_type = eval(
88
+ annotation.__forward_arg__, self.__global_context, locals()
89
+ )
90
+ if isinstance(actual_type, enum.EnumMeta):
91
+
92
+ # untyped because mypy really doesn't like this enum internals stuff
93
+ def err(val):
94
+ valid = []
95
+ for v in actual_type.__members__.values():
96
+ if v.value != "invalid_api_enum_value":
97
+ valid.append(v.value)
98
+ return (
99
+ f"Invalid value '{val}' for " # type: ignore
100
+ f"'{self.__class__.__name__}.{key}'. Valid values are "
101
+ f"{valid}" # type: ignore
102
+ )
103
+
104
+ if isinstance(value, actual_type):
105
+ raise ValueError(err(value))
106
+ enum_member = actual_type(value)
107
+ if enum_member.value == "invalid_api_enum_value":
108
+ raise ValueError(err(value))
109
+ value = enum_member
110
+ elif issubclass(actual_type, Model):
111
+ value = self._get_converter().structure(value, actual_type)
112
+
113
+ return setattr(self, key, value)
114
+
115
+ def __delitem__(self, key):
116
+ self[key] # validates key
117
+ setattr(self, self._key_to_attr(key), None)
118
+
119
+ def __iter__(self):
120
+ return iter(self._get_converter().unstructure(self))
121
+
122
+ def __len__(self):
123
+ return len(self._get_converter().unstructure(self))
124
+
125
+ def __contains__(self, key):
126
+ return key in self._get_converter().unstructure(self)
127
+
128
+ def keys(self):
129
+ return self._get_converter().unstructure(self).keys()
130
+
131
+ def items(self):
132
+ return self._get_converter().unstructure(self).items()
133
+
134
+ def values(self):
135
+ return self._get_converter().unstructure(self).values()
136
+
137
+ def get(self, key, default=None):
138
+ try:
139
+ return self[key]
140
+ except KeyError:
141
+ return default
142
+
143
+ def pop(self, key, default=None):
144
+ ret = self.get(key, default)
145
+ if key in self:
146
+ del self[key]
147
+ return ret
148
+
149
+ def popitem(self):
150
+ raise NotImplementedError()
151
+
152
+ def clear(self):
153
+ raise NotImplementedError()
154
+
155
+ def update(self, iterable=None, **kwargs):
156
+ if iterable:
157
+ has_keys = getattr(iterable, "keys", None)
158
+ if callable(has_keys):
159
+ for k in iterable:
160
+ self[k] = iterable[k]
161
+ else:
162
+ for k, v in iterable:
163
+ self[k] = v
164
+ for k in kwargs:
165
+ self[k] = kwargs[k]
166
+
167
+ def setdefault(self, key, default=None):
168
+ if key not in self:
169
+ self[key] = default
170
+ return self[key]
171
+
172
+ def copy(self):
173
+ raise NotImplementedError()
174
+
175
+
176
+ def safe_enum__new__(cls, value):
177
+ """Handle out-of-spec enum values returned by API.
178
+
179
+ This is achieved by overriding the __new__ method to return
180
+ `invalid_api_enum_value` (defined on each subclass) when an
181
+ unexpected value for the enum is returned by the API.
182
+ """
183
+ if not isinstance(value, (str, int, bool)):
184
+ return super().__new__(cls, value)
185
+ else:
186
+ vals = {v.value: v for v in cls.__members__.values()}
187
+ return vals.get(value, cls.invalid_api_enum_value)
188
+
189
+
190
+ T = TypeVar("T")
191
+
192
+
193
+ class DelimSequence(collections.UserList, Sequence[T]):
194
+ def __init__(
195
+ self,
196
+ data: Optional[Sequence[T]] = None,
197
+ prefix: str = "",
198
+ suffix: str = "",
199
+ separator: str = ",",
200
+ ):
201
+ self.prefix = prefix
202
+ self.suffix = suffix
203
+ self.separator = separator
204
+
205
+ super().__init__(data)
206
+
207
+ def append(self, elem: T):
208
+ super().append(elem)
209
+
210
+ def extend(self, iterable: Iterable[T]):
211
+ super().extend(iterable)
212
+
213
+ def insert(self, i: int, elem: T):
214
+ super().insert(i, elem)
215
+
216
+ def remove(self, elem: T):
217
+ super().remove(elem)
218
+
219
+ def index(self, x: T, *args):
220
+ super().index(x, *args) # type: ignore
221
+
222
+ def count(self, elem: T):
223
+ super().count(elem)
224
+
225
+ def __str__(self):
226
+ return (
227
+ f"{self.prefix}"
228
+ f"{self.separator.join(str(d) for d in self.data)}"
229
+ f"{self.suffix}"
230
+ )
@@ -0,0 +1,110 @@
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
+ """Transport implementation using requests package.
24
+ """
25
+
26
+ import logging
27
+ from typing import cast, Callable, Dict, MutableMapping, Optional
28
+
29
+ import requests
30
+
31
+ from looker_sdk.rtl import transport
32
+
33
+
34
+ class RequestsTransport(transport.Transport):
35
+ """RequestsTransport implementation of Transport."""
36
+
37
+ def __init__(
38
+ self, settings: transport.PTransportSettings, session: requests.Session
39
+ ):
40
+ self.settings = settings
41
+ headers: Dict[str, str] = {transport.LOOKER_API_ID: settings.agent_tag}
42
+ if settings.headers:
43
+ headers.update(settings.headers)
44
+ session.headers.update(headers)
45
+ session.verify = settings.verify_ssl
46
+ self.session = session
47
+ self.logger = logging.getLogger(__name__)
48
+
49
+ @classmethod
50
+ def configure(cls, settings: transport.PTransportSettings) -> transport.Transport:
51
+ return cls(settings, requests.Session())
52
+
53
+ def request(
54
+ self,
55
+ method: transport.HttpMethod,
56
+ path: str,
57
+ query_params: Optional[MutableMapping[str, str]] = None,
58
+ body: Optional[bytes] = None,
59
+ authenticator: transport.TAuthenticator = None,
60
+ transport_options: Optional[transport.TransportOptions] = None,
61
+ ) -> transport.Response:
62
+
63
+ headers = {}
64
+ timeout = self.settings.timeout
65
+ if authenticator:
66
+ headers.update(authenticator(transport_options or {}))
67
+ if transport_options:
68
+ if transport_options.get("headers"):
69
+ headers.update(transport_options["headers"])
70
+ if transport_options.get("timeout"):
71
+ timeout = transport_options["timeout"]
72
+ self.logger.info("%s(%s)", method.name, path)
73
+ try:
74
+ resp = self.session.request(
75
+ method.name,
76
+ path,
77
+ auth=NullAuth(),
78
+ params=query_params,
79
+ data=body,
80
+ headers=headers,
81
+ timeout=timeout,
82
+ )
83
+ except IOError as exc:
84
+ ret = transport.Response(
85
+ False,
86
+ bytes(str(exc), encoding="utf-8"),
87
+ transport.ResponseMode.STRING,
88
+ )
89
+ else:
90
+ ret = transport.Response(
91
+ resp.ok,
92
+ resp.content,
93
+ transport.response_mode(resp.headers.get("content-type")),
94
+ )
95
+ encoding = cast(
96
+ Optional[str], requests.utils.get_encoding_from_headers(resp.headers)
97
+ )
98
+ if encoding:
99
+ ret.encoding = encoding
100
+
101
+ return ret
102
+
103
+
104
+ class NullAuth(requests.auth.AuthBase):
105
+ """A custom auth class which ensures requests does not override authorization
106
+ headers with netrc file credentials if present.
107
+ """
108
+
109
+ def __call__(self, r):
110
+ return r
@@ -0,0 +1,120 @@
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
+ """
25
+ import datetime
26
+ import enum
27
+ import functools
28
+ import json
29
+ import keyword
30
+ import sys
31
+ from typing import (
32
+ Callable,
33
+ MutableMapping,
34
+ Sequence,
35
+ Type,
36
+ Union,
37
+ )
38
+
39
+ import cattr
40
+
41
+ from looker_sdk.rtl import model, hooks
42
+
43
+
44
+ class DeserializeError(Exception):
45
+ """Improperly formatted data to deserialize."""
46
+
47
+
48
+ TModelOrSequence = Union[
49
+ MutableMapping[str, str],
50
+ Sequence[int],
51
+ Sequence[str],
52
+ model.Model,
53
+ Sequence[model.Model],
54
+ ]
55
+ TDeserializeReturn = TModelOrSequence
56
+ TStructure = Union[Type[Sequence[int]], Type[Sequence[str]], Type[TDeserializeReturn]]
57
+ TDeserialize = Callable[[str, TStructure], TDeserializeReturn]
58
+ TSerialize = Callable[[TModelOrSequence], bytes]
59
+
60
+
61
+ def deserialize(
62
+ *, data: str, structure: TStructure, converter: cattr.Converter
63
+ ) -> TDeserializeReturn:
64
+ """Translate API data into models."""
65
+ try:
66
+ data = json.loads(data)
67
+ except json.JSONDecodeError as ex:
68
+ raise DeserializeError(f"Bad json {ex}")
69
+ try:
70
+ response: TDeserializeReturn = converter.structure( # type: ignore
71
+ data, structure
72
+ )
73
+ except (TypeError, AttributeError) as ex:
74
+ raise DeserializeError(f"Bad data {ex}")
75
+ return response
76
+
77
+
78
+ def serialize(*, api_model: TModelOrSequence, converter: cattr.Converter) -> bytes:
79
+ """Translate api_model into formdata encoded json bytes"""
80
+ data = converter.unstructure(api_model) # type: ignore
81
+ return json.dumps(data).encode("utf-8") # type: ignore
82
+
83
+
84
+ def forward_ref_structure_hook(context, converter, data, forward_ref):
85
+ """Applied to ForwardRef model and enum annotations
86
+
87
+ - Map reserved words in json keys to approriate (safe) names in model.
88
+ - handle ForwardRef types until github.com/Tinche/cattrs/pull/42/ is fixed
89
+ Note: this is the reason we need a "context" param and have to use a
90
+ partial func to register the hook. Once the issue is resolved we can
91
+ remove "context" and the partial.
92
+ """
93
+ data = hooks.tr_data_keys(data)
94
+ actual_type = eval(forward_ref.__forward_arg__, context, locals())
95
+ if issubclass(actual_type, enum.Enum):
96
+ instance = converter.structure(data, actual_type)
97
+ elif issubclass(actual_type, model.Model):
98
+ # cannot use converter.structure - recursion error
99
+ instance = converter.structure_attrs_fromdict(data, actual_type)
100
+ else:
101
+ raise DeserializeError(f"Unknown type to deserialize: {actual_type}")
102
+ return instance
103
+
104
+
105
+ def translate_keys_structure_hook(converter, data, model_type):
106
+ """Applied only to models.Model"""
107
+ new_data = hooks.tr_data_keys(data)
108
+ ret = converter.structure_attrs_fromdict(new_data, model_type)
109
+ return ret
110
+
111
+ converter40 = cattr.Converter()
112
+ deserialize40 = functools.partial(deserialize, converter=converter40)
113
+ serialize40 = functools.partial(serialize, converter=converter40)
114
+
115
+ converter40.register_structure_hook(datetime.datetime, hooks.datetime_structure_hook)
116
+ unstructure_hook40 = functools.partial(hooks.unstructure_hook, converter40) # type: ignore
117
+ converter40.register_unstructure_hook(model.Model, unstructure_hook40) # type: ignore
118
+ converter40.register_unstructure_hook(
119
+ datetime.datetime, hooks.datetime_unstructure_hook # type: ignore
120
+ )
@@ -0,0 +1,137 @@
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
+ """Types and abstract base class for transport implementations.
24
+ """
25
+ import abc
26
+ import enum
27
+ import re
28
+ import sys
29
+ from typing import Callable, Dict, MutableMapping, Optional
30
+
31
+ import attr
32
+
33
+ from looker_sdk import error
34
+ from looker_sdk.rtl import constants
35
+
36
+ if sys.version_info >= (3, 8):
37
+ from typing import Protocol, TypedDict
38
+ else:
39
+ from typing_extensions import Protocol, TypedDict
40
+
41
+
42
+ AGENT_PREFIX = "PY SDK"
43
+ LOOKER_API_ID = "x-looker-appid"
44
+
45
+
46
+ class HttpMethod(enum.Enum):
47
+ """Supported HTTP verbs."""
48
+
49
+ GET = 1
50
+ POST = 2
51
+ PUT = 3
52
+ DELETE = 4
53
+ PATCH = 5
54
+ TRACE = 6
55
+ HEAD = 7
56
+
57
+
58
+ class PTransportSettings(Protocol):
59
+ base_url: str
60
+ verify_ssl: bool
61
+ timeout: int
62
+ agent_tag: str
63
+ headers: Optional[MutableMapping[str, str]]
64
+
65
+ def is_configured(self):
66
+ if not self.base_url:
67
+ raise error.SDKError("Missing required configuration value: base_url")
68
+
69
+
70
+ class TransportOptions(TypedDict, total=False):
71
+ """Dictionary of available per-request transport options
72
+
73
+ # make me() call with 5 minute timeout and send a special header
74
+ e.g. sdk.me(transport_options={"timeout": 60 * 5, headers: {"foo": "bar"}})
75
+ """
76
+
77
+ timeout: int
78
+ headers: MutableMapping[str, str]
79
+
80
+
81
+ TAuthenticator = Optional[Callable[[TransportOptions], Dict[str, str]]]
82
+
83
+
84
+ class ResponseMode(enum.Enum):
85
+ """ResponseMode for an HTTP request - either binary or "string" """
86
+
87
+ BINARY = 1
88
+ STRING = 2
89
+ UNKNOWN = 3
90
+
91
+
92
+ @attr.s(auto_attribs=True)
93
+ class Response:
94
+ """Success Response object."""
95
+
96
+ ok: bool
97
+ value: bytes
98
+ response_mode: ResponseMode
99
+ encoding: str = "utf-8"
100
+
101
+
102
+ _STRING_MODE = re.compile(constants.RESPONSE_STRING_MODE, re.IGNORECASE)
103
+ _BINARY_MODE = re.compile(constants.RESPONSE_BINARY_MODE, re.IGNORECASE)
104
+
105
+
106
+ def response_mode(content_type: Optional[str] = None) -> ResponseMode:
107
+ """Determine ResponseMode from http Content-Type header"""
108
+ response = ResponseMode.UNKNOWN
109
+ if not content_type:
110
+ return response
111
+
112
+ if _STRING_MODE.search(content_type):
113
+ response = ResponseMode.STRING
114
+ elif _BINARY_MODE.search(content_type):
115
+ response = ResponseMode.BINARY
116
+ return response
117
+
118
+
119
+ class Transport(abc.ABC):
120
+ """Transport base class."""
121
+
122
+ @classmethod
123
+ @abc.abstractmethod
124
+ def configure(cls, settings: PTransportSettings) -> "Transport":
125
+ """Configure and return an instance of Transport"""
126
+
127
+ @abc.abstractmethod
128
+ def request(
129
+ self,
130
+ method: HttpMethod,
131
+ path: str,
132
+ query_params: Optional[MutableMapping[str, str]] = None,
133
+ body: Optional[bytes] = None,
134
+ authenticator: TAuthenticator = None,
135
+ transport_options: Optional[TransportOptions] = None,
136
+ ) -> Response:
137
+ """Send API request."""
File without changes
@@ -0,0 +1 @@
1
+ # Generated file.