dapla-toolbelt-metadata 0.2.1__py3-none-any.whl → 0.9.11__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.
Potentially problematic release.
This version of dapla-toolbelt-metadata might be problematic. Click here for more details.
- dapla_metadata/__init__.py +11 -1
- dapla_metadata/_shared/__init__.py +1 -0
- dapla_metadata/_shared/config.py +109 -0
- dapla_metadata/_shared/enums.py +27 -0
- dapla_metadata/_shared/py.typed +0 -0
- dapla_metadata/dapla/__init__.py +4 -0
- dapla_metadata/dapla/user_info.py +138 -0
- dapla_metadata/datasets/__init__.py +1 -1
- dapla_metadata/datasets/_merge.py +333 -0
- dapla_metadata/datasets/code_list.py +5 -6
- dapla_metadata/datasets/compatibility/__init__.py +10 -0
- dapla_metadata/datasets/compatibility/_handlers.py +363 -0
- dapla_metadata/datasets/compatibility/_utils.py +259 -0
- dapla_metadata/datasets/compatibility/model_backwards_compatibility.py +135 -0
- dapla_metadata/datasets/core.py +136 -182
- dapla_metadata/datasets/dapla_dataset_path_info.py +145 -19
- dapla_metadata/datasets/dataset_parser.py +41 -28
- dapla_metadata/datasets/model_validation.py +29 -20
- dapla_metadata/datasets/statistic_subject_mapping.py +5 -1
- dapla_metadata/datasets/utility/constants.py +22 -15
- dapla_metadata/datasets/utility/enums.py +8 -20
- dapla_metadata/datasets/utility/urn.py +234 -0
- dapla_metadata/datasets/utility/utils.py +183 -111
- dapla_metadata/standards/__init__.py +4 -0
- dapla_metadata/standards/name_validator.py +250 -0
- dapla_metadata/standards/standard_validators.py +98 -0
- dapla_metadata/standards/utils/__init__.py +1 -0
- dapla_metadata/standards/utils/constants.py +49 -0
- dapla_metadata/variable_definitions/__init__.py +11 -0
- dapla_metadata/variable_definitions/_generated/.openapi-generator/FILES +20 -0
- dapla_metadata/variable_definitions/_generated/.openapi-generator/VERSION +1 -0
- dapla_metadata/variable_definitions/_generated/.openapi-generator-ignore +6 -0
- dapla_metadata/variable_definitions/_generated/README.md +148 -0
- dapla_metadata/variable_definitions/_generated/__init__.py +0 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/__init__.py +47 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/__init__.py +8 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/data_migration_api.py +766 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/draft_variable_definitions_api.py +888 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/patches_api.py +888 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/validity_periods_api.py +583 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api/variable_definitions_api.py +613 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api_client.py +779 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/api_response.py +27 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/configuration.py +474 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/CompleteResponse.md +51 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/Contact.md +30 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/DataMigrationApi.md +90 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/Draft.md +42 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/DraftVariableDefinitionsApi.md +259 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/LanguageStringType.md +31 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/Owner.md +31 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/Patch.md +43 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/PatchesApi.md +249 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/PublicApi.md +218 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/SupportedLanguages.md +15 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/UpdateDraft.md +44 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/ValidityPeriod.md +42 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/ValidityPeriodsApi.md +236 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/VariableDefinitionsApi.md +304 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/docs/VariableStatus.md +17 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/exceptions.py +193 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/__init__.py +31 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/complete_response.py +260 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/contact.py +94 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/draft.py +228 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/get_vardok_vardef_mapping_by_id200_response.py +158 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/language_string_type.py +101 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/owner.py +87 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/patch.py +244 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/problem.py +118 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/update_draft.py +274 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/validity_period.py +225 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/vardok_id_response.py +81 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/vardok_vardef_id_pair_response.py +84 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/models/variable_status.py +33 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/py.typed +0 -0
- dapla_metadata/variable_definitions/_generated/vardef_client/rest.py +249 -0
- dapla_metadata/variable_definitions/_utils/__init__.py +1 -0
- dapla_metadata/variable_definitions/_utils/_client.py +32 -0
- dapla_metadata/variable_definitions/_utils/config.py +54 -0
- dapla_metadata/variable_definitions/_utils/constants.py +80 -0
- dapla_metadata/variable_definitions/_utils/files.py +309 -0
- dapla_metadata/variable_definitions/_utils/template_files.py +99 -0
- dapla_metadata/variable_definitions/_utils/variable_definition_files.py +143 -0
- dapla_metadata/variable_definitions/exceptions.py +255 -0
- dapla_metadata/variable_definitions/vardef.py +372 -0
- dapla_metadata/variable_definitions/vardok_id.py +48 -0
- dapla_metadata/variable_definitions/vardok_vardef_id_pair.py +47 -0
- dapla_metadata/variable_definitions/variable_definition.py +422 -0
- {dapla_toolbelt_metadata-0.2.1.dist-info → dapla_toolbelt_metadata-0.9.11.dist-info}/METADATA +34 -36
- dapla_toolbelt_metadata-0.9.11.dist-info/RECORD +97 -0
- {dapla_toolbelt_metadata-0.2.1.dist-info → dapla_toolbelt_metadata-0.9.11.dist-info}/WHEEL +1 -1
- dapla_metadata/datasets/config.py +0 -80
- dapla_metadata/datasets/model_backwards_compatibility.py +0 -520
- dapla_metadata/datasets/user_info.py +0 -88
- dapla_toolbelt_metadata-0.2.1.dist-info/RECORD +0 -22
- {dapla_toolbelt_metadata-0.2.1.dist-info → dapla_toolbelt_metadata-0.9.11.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,779 @@
|
|
|
1
|
+
"""Variable Definitions
|
|
2
|
+
|
|
3
|
+
## Introduction Variable Definitions are centralized definitions of concrete variables which are typically present in multiple datasets. Variable Definitions support standardization of data and metadata and facilitate sharing and joining of data by clarifying when variables have an identical definition. ## Maintenance of Variable Definitions This API allows for creation, maintenance and access of Variable Definitions. ### Ownership Creation and maintenance of variables may only be performed by Statistics Norway employees representing a specific Dapla team, who are defined as the owners of a given Variable Definition. The team an owner represents must be specified when making a request through the `active_group` query parameter. All maintenance is to be performed by the owners, with no intervention from administrators. ### Status All Variable Definitions have an associated status. The possible values for status are `DRAFT`, `PUBLISHED_INTERNAL` and `PUBLISHED_EXTERNAL`. #### Draft When a Variable Definition is created it is assigned the status `DRAFT`. Under this status the Variable Definition is: - Only visible to Statistics Norway employees. - Mutable (it may be changed directly without need for versioning). - Not suitable to refer to from other systems. This status may be changed to `PUBLISHED_INTERNAL` or `PUBLISHED_EXTERNAL` with a direct update. #### Published Internal Under this status the Variable Definition is: - Only visible to Statistics Norway employees. - Immutable (all changes are versioned). - Suitable to refer to in internal systems for statistics production. - Not suitable to refer to for external use (for example in Statistikkbanken). This status may be changed to `PUBLISHED_EXTERNAL` by creating a Patch version. #### Published External Under this status the Variable Definition is: - Visible to the general public. - Immutable (all changes are versioned). - Suitable to refer to from any system. This status may not be changed as it would break immutability. If a Variable Definition is no longer relevant then its period of validity should be ended by specifying a `valid_until` date in a Patch version. ### Immutability Variable Definitions are immutable. This means that any changes must be performed in a strict versioning system. Consumers can avoid being exposed to breaking changes by specifying a `date_of_validity` when they request a Variable Definition. #### Patches Patches are for changes which do not affect the fundamental meaning of the Variable Definition. #### Validity Periods Validity Periods are versions with a period defined by a `valid_from` date and optionally a `valid_until` date. If the fundamental meaning of a Variable Definition is to be changed, it should be done by creating a new Validity Period.
|
|
4
|
+
|
|
5
|
+
The version of the OpenAPI document: 0.1
|
|
6
|
+
Contact: metadata@ssb.no
|
|
7
|
+
Generated by OpenAPI Generator (https://openapi-generator.tech)
|
|
8
|
+
|
|
9
|
+
Do not edit the class manually.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import datetime
|
|
13
|
+
import decimal
|
|
14
|
+
import json
|
|
15
|
+
import mimetypes
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
import tempfile
|
|
19
|
+
from enum import Enum
|
|
20
|
+
from urllib.parse import quote
|
|
21
|
+
|
|
22
|
+
from dateutil.parser import parse
|
|
23
|
+
from pydantic import SecretStr
|
|
24
|
+
|
|
25
|
+
from . import models
|
|
26
|
+
from . import rest
|
|
27
|
+
from .api_response import ApiResponse
|
|
28
|
+
from .api_response import T as ApiResponseT
|
|
29
|
+
from .configuration import Configuration
|
|
30
|
+
from .exceptions import ApiException
|
|
31
|
+
from .exceptions import ApiValueError
|
|
32
|
+
|
|
33
|
+
RequestSerialized = tuple[str, str, dict[str, str], str | None, list[str]]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ApiClient:
|
|
37
|
+
"""Generic API client for OpenAPI client library builds.
|
|
38
|
+
|
|
39
|
+
OpenAPI generic API client. This client handles the client-
|
|
40
|
+
server communication, and is invariant across implementations. Specifics of
|
|
41
|
+
the methods and models for each application are generated from the OpenAPI
|
|
42
|
+
templates.
|
|
43
|
+
|
|
44
|
+
:param configuration: .Configuration object for this client
|
|
45
|
+
:param header_name: a header to pass when making calls to the API.
|
|
46
|
+
:param header_value: a header value to pass when making calls to
|
|
47
|
+
the API.
|
|
48
|
+
:param cookie: a cookie to include in the header when making calls
|
|
49
|
+
to the API
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
PRIMITIVE_TYPES = (float, bool, bytes, str, int)
|
|
53
|
+
NATIVE_TYPES_MAPPING = {
|
|
54
|
+
"int": int,
|
|
55
|
+
"long": int, # TODO remove as only py3 is supported?
|
|
56
|
+
"float": float,
|
|
57
|
+
"str": str,
|
|
58
|
+
"bool": bool,
|
|
59
|
+
"date": datetime.date,
|
|
60
|
+
"datetime": datetime.datetime,
|
|
61
|
+
"decimal": decimal.Decimal,
|
|
62
|
+
"object": object,
|
|
63
|
+
}
|
|
64
|
+
_pool = None
|
|
65
|
+
|
|
66
|
+
def __init__(
|
|
67
|
+
self,
|
|
68
|
+
configuration=None,
|
|
69
|
+
header_name=None,
|
|
70
|
+
header_value=None,
|
|
71
|
+
cookie=None,
|
|
72
|
+
) -> None:
|
|
73
|
+
# use default configuration if none is provided
|
|
74
|
+
if configuration is None:
|
|
75
|
+
configuration = Configuration.get_default()
|
|
76
|
+
self.configuration = configuration
|
|
77
|
+
|
|
78
|
+
self.rest_client = rest.RESTClientObject(configuration)
|
|
79
|
+
self.default_headers = {}
|
|
80
|
+
if header_name is not None:
|
|
81
|
+
self.default_headers[header_name] = header_value
|
|
82
|
+
self.cookie = cookie
|
|
83
|
+
# Set default User-Agent.
|
|
84
|
+
self.user_agent = "OpenAPI-Generator/1.0.0/python"
|
|
85
|
+
self.client_side_validation = configuration.client_side_validation
|
|
86
|
+
|
|
87
|
+
def __enter__(self):
|
|
88
|
+
return self
|
|
89
|
+
|
|
90
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def user_agent(self):
|
|
95
|
+
"""User agent for this API client"""
|
|
96
|
+
return self.default_headers["User-Agent"]
|
|
97
|
+
|
|
98
|
+
@user_agent.setter
|
|
99
|
+
def user_agent(self, value):
|
|
100
|
+
self.default_headers["User-Agent"] = value
|
|
101
|
+
|
|
102
|
+
def set_default_header(self, header_name, header_value):
|
|
103
|
+
self.default_headers[header_name] = header_value
|
|
104
|
+
|
|
105
|
+
_default = None
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def get_default(cls):
|
|
109
|
+
"""Return new instance of ApiClient.
|
|
110
|
+
|
|
111
|
+
This method returns newly created, based on default constructor,
|
|
112
|
+
object of ApiClient class or returns a copy of default
|
|
113
|
+
ApiClient.
|
|
114
|
+
|
|
115
|
+
:return: The ApiClient object.
|
|
116
|
+
"""
|
|
117
|
+
if cls._default is None:
|
|
118
|
+
cls._default = ApiClient()
|
|
119
|
+
return cls._default
|
|
120
|
+
|
|
121
|
+
@classmethod
|
|
122
|
+
def set_default(cls, default):
|
|
123
|
+
"""Set default instance of ApiClient.
|
|
124
|
+
|
|
125
|
+
It stores default ApiClient.
|
|
126
|
+
|
|
127
|
+
:param default: object of ApiClient.
|
|
128
|
+
"""
|
|
129
|
+
cls._default = default
|
|
130
|
+
|
|
131
|
+
def param_serialize(
|
|
132
|
+
self,
|
|
133
|
+
method,
|
|
134
|
+
resource_path,
|
|
135
|
+
path_params=None,
|
|
136
|
+
query_params=None,
|
|
137
|
+
header_params=None,
|
|
138
|
+
body=None,
|
|
139
|
+
post_params=None,
|
|
140
|
+
files=None,
|
|
141
|
+
auth_settings=None,
|
|
142
|
+
collection_formats=None,
|
|
143
|
+
_host=None,
|
|
144
|
+
_request_auth=None,
|
|
145
|
+
) -> RequestSerialized:
|
|
146
|
+
"""Builds the HTTP request params needed by the request.
|
|
147
|
+
:param method: Method to call.
|
|
148
|
+
:param resource_path: Path to method endpoint.
|
|
149
|
+
:param path_params: Path parameters in the url.
|
|
150
|
+
:param query_params: Query parameters in the url.
|
|
151
|
+
:param header_params: Header parameters to be
|
|
152
|
+
placed in the request header.
|
|
153
|
+
:param body: Request body.
|
|
154
|
+
:param post_params dict: Request post form parameters,
|
|
155
|
+
for `application/x-www-form-urlencoded`, `multipart/form-data`.
|
|
156
|
+
:param auth_settings list: Auth Settings names for the request.
|
|
157
|
+
:param files dict: key -> filename, value -> filepath,
|
|
158
|
+
for `multipart/form-data`.
|
|
159
|
+
:param collection_formats: dict of collection formats for path, query,
|
|
160
|
+
header, and post parameters.
|
|
161
|
+
:param _request_auth: set to override the auth_settings for an a single
|
|
162
|
+
request; this effectively ignores the authentication
|
|
163
|
+
in the spec for a single request.
|
|
164
|
+
:return: tuple of form (path, http_method, query_params, header_params,
|
|
165
|
+
body, post_params, files)
|
|
166
|
+
"""
|
|
167
|
+
config = self.configuration
|
|
168
|
+
|
|
169
|
+
# header parameters
|
|
170
|
+
header_params = header_params or {}
|
|
171
|
+
header_params.update(self.default_headers)
|
|
172
|
+
if self.cookie:
|
|
173
|
+
header_params["Cookie"] = self.cookie
|
|
174
|
+
if header_params:
|
|
175
|
+
header_params = self.sanitize_for_serialization(header_params)
|
|
176
|
+
header_params = dict(
|
|
177
|
+
self.parameters_to_tuples(header_params, collection_formats),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
# path parameters
|
|
181
|
+
if path_params:
|
|
182
|
+
path_params = self.sanitize_for_serialization(path_params)
|
|
183
|
+
path_params = self.parameters_to_tuples(
|
|
184
|
+
path_params,
|
|
185
|
+
collection_formats,
|
|
186
|
+
)
|
|
187
|
+
for k, v in path_params:
|
|
188
|
+
# specified safe chars, encode everything
|
|
189
|
+
resource_path = resource_path.replace(
|
|
190
|
+
"{%s}" % k,
|
|
191
|
+
quote(str(v), safe=config.safe_chars_for_path_param),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
# post parameters
|
|
195
|
+
if post_params or files:
|
|
196
|
+
post_params = post_params if post_params else []
|
|
197
|
+
post_params = self.sanitize_for_serialization(post_params)
|
|
198
|
+
post_params = self.parameters_to_tuples(
|
|
199
|
+
post_params,
|
|
200
|
+
collection_formats,
|
|
201
|
+
)
|
|
202
|
+
if files:
|
|
203
|
+
post_params.extend(self.files_parameters(files))
|
|
204
|
+
|
|
205
|
+
# auth setting
|
|
206
|
+
self.update_params_for_auth(
|
|
207
|
+
header_params,
|
|
208
|
+
query_params,
|
|
209
|
+
auth_settings,
|
|
210
|
+
resource_path,
|
|
211
|
+
method,
|
|
212
|
+
body,
|
|
213
|
+
request_auth=_request_auth,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# body
|
|
217
|
+
if body:
|
|
218
|
+
body = self.sanitize_for_serialization(body)
|
|
219
|
+
|
|
220
|
+
# request url
|
|
221
|
+
if _host is None or self.configuration.ignore_operation_servers:
|
|
222
|
+
url = self.configuration.host + resource_path
|
|
223
|
+
else:
|
|
224
|
+
# use server/host defined in path or operation instead
|
|
225
|
+
url = _host + resource_path
|
|
226
|
+
|
|
227
|
+
# query parameters
|
|
228
|
+
if query_params:
|
|
229
|
+
query_params = self.sanitize_for_serialization(query_params)
|
|
230
|
+
url_query = self.parameters_to_url_query(
|
|
231
|
+
query_params,
|
|
232
|
+
collection_formats,
|
|
233
|
+
)
|
|
234
|
+
url += "?" + url_query
|
|
235
|
+
|
|
236
|
+
return method, url, header_params, body, post_params
|
|
237
|
+
|
|
238
|
+
def call_api(
|
|
239
|
+
self,
|
|
240
|
+
method,
|
|
241
|
+
url,
|
|
242
|
+
header_params=None,
|
|
243
|
+
body=None,
|
|
244
|
+
post_params=None,
|
|
245
|
+
_request_timeout=None,
|
|
246
|
+
) -> rest.RESTResponse:
|
|
247
|
+
"""Makes the HTTP request (synchronous)
|
|
248
|
+
:param method: Method to call.
|
|
249
|
+
:param url: Path to method endpoint.
|
|
250
|
+
:param header_params: Header parameters to be
|
|
251
|
+
placed in the request header.
|
|
252
|
+
:param body: Request body.
|
|
253
|
+
:param post_params dict: Request post form parameters,
|
|
254
|
+
for `application/x-www-form-urlencoded`, `multipart/form-data`.
|
|
255
|
+
:param _request_timeout: timeout setting for this request.
|
|
256
|
+
:return: RESTResponse
|
|
257
|
+
"""
|
|
258
|
+
try:
|
|
259
|
+
# perform request and return response
|
|
260
|
+
response_data = self.rest_client.request(
|
|
261
|
+
method,
|
|
262
|
+
url,
|
|
263
|
+
headers=header_params,
|
|
264
|
+
body=body,
|
|
265
|
+
post_params=post_params,
|
|
266
|
+
_request_timeout=_request_timeout,
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
except ApiException as e:
|
|
270
|
+
raise e
|
|
271
|
+
|
|
272
|
+
return response_data
|
|
273
|
+
|
|
274
|
+
def response_deserialize(
|
|
275
|
+
self,
|
|
276
|
+
response_data: rest.RESTResponse,
|
|
277
|
+
response_types_map: dict[str, ApiResponseT] | None = None,
|
|
278
|
+
) -> ApiResponse[ApiResponseT]:
|
|
279
|
+
"""Deserializes response into an object.
|
|
280
|
+
:param response_data: RESTResponse object to be deserialized.
|
|
281
|
+
:param response_types_map: dict of response types.
|
|
282
|
+
:return: ApiResponse
|
|
283
|
+
"""
|
|
284
|
+
msg = "RESTResponse.read() must be called before passing it to response_deserialize()"
|
|
285
|
+
assert response_data.data is not None, msg
|
|
286
|
+
|
|
287
|
+
response_type = response_types_map.get(str(response_data.status), None)
|
|
288
|
+
if (
|
|
289
|
+
not response_type
|
|
290
|
+
and isinstance(response_data.status, int)
|
|
291
|
+
and 100 <= response_data.status <= 599
|
|
292
|
+
):
|
|
293
|
+
# if not found, look for '1XX', '2XX', etc.
|
|
294
|
+
response_type = response_types_map.get(
|
|
295
|
+
str(response_data.status)[0] + "XX",
|
|
296
|
+
None,
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
# deserialize response data
|
|
300
|
+
response_text = None
|
|
301
|
+
return_data = None
|
|
302
|
+
try:
|
|
303
|
+
if response_type == "bytearray":
|
|
304
|
+
return_data = response_data.data
|
|
305
|
+
elif response_type == "file":
|
|
306
|
+
return_data = self.__deserialize_file(response_data)
|
|
307
|
+
elif response_type is not None:
|
|
308
|
+
match = None
|
|
309
|
+
content_type = response_data.getheader("content-type")
|
|
310
|
+
if content_type is not None:
|
|
311
|
+
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
|
|
312
|
+
encoding = match.group(1) if match else "utf-8"
|
|
313
|
+
response_text = response_data.data.decode(encoding)
|
|
314
|
+
return_data = self.deserialize(
|
|
315
|
+
response_text,
|
|
316
|
+
response_type,
|
|
317
|
+
content_type,
|
|
318
|
+
)
|
|
319
|
+
finally:
|
|
320
|
+
if not 200 <= response_data.status <= 299:
|
|
321
|
+
raise ApiException.from_response(
|
|
322
|
+
http_resp=response_data,
|
|
323
|
+
body=response_text,
|
|
324
|
+
data=return_data,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
return ApiResponse(
|
|
328
|
+
status_code=response_data.status,
|
|
329
|
+
data=return_data,
|
|
330
|
+
headers=response_data.getheaders(),
|
|
331
|
+
raw_data=response_data.data,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
def sanitize_for_serialization(self, obj):
|
|
335
|
+
"""Builds a JSON POST object.
|
|
336
|
+
|
|
337
|
+
If obj is None, return None.
|
|
338
|
+
If obj is SecretStr, return obj.get_secret_value()
|
|
339
|
+
If obj is str, int, long, float, bool, return directly.
|
|
340
|
+
If obj is datetime.datetime, datetime.date
|
|
341
|
+
convert to string in iso8601 format.
|
|
342
|
+
If obj is decimal.Decimal return string representation.
|
|
343
|
+
If obj is list, sanitize each element in the list.
|
|
344
|
+
If obj is dict, return the dict.
|
|
345
|
+
If obj is OpenAPI model, return the properties dict.
|
|
346
|
+
|
|
347
|
+
:param obj: The data to serialize.
|
|
348
|
+
:return: The serialized form of data.
|
|
349
|
+
"""
|
|
350
|
+
if obj is None:
|
|
351
|
+
return None
|
|
352
|
+
if isinstance(obj, Enum):
|
|
353
|
+
return obj.value
|
|
354
|
+
if isinstance(obj, SecretStr):
|
|
355
|
+
return obj.get_secret_value()
|
|
356
|
+
if isinstance(obj, self.PRIMITIVE_TYPES):
|
|
357
|
+
return obj
|
|
358
|
+
if isinstance(obj, list):
|
|
359
|
+
return [self.sanitize_for_serialization(sub_obj) for sub_obj in obj]
|
|
360
|
+
if isinstance(obj, tuple):
|
|
361
|
+
return tuple(self.sanitize_for_serialization(sub_obj) for sub_obj in obj)
|
|
362
|
+
if isinstance(obj, (datetime.datetime, datetime.date)):
|
|
363
|
+
return obj.isoformat()
|
|
364
|
+
if isinstance(obj, decimal.Decimal):
|
|
365
|
+
return str(obj)
|
|
366
|
+
|
|
367
|
+
if isinstance(obj, dict):
|
|
368
|
+
obj_dict = obj
|
|
369
|
+
elif hasattr(obj, "to_dict") and callable(obj.to_dict):
|
|
370
|
+
obj_dict = obj.to_dict()
|
|
371
|
+
else:
|
|
372
|
+
obj_dict = obj.__dict__
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
key: self.sanitize_for_serialization(val) for key, val in obj_dict.items()
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
def deserialize(
|
|
379
|
+
self,
|
|
380
|
+
response_text: str,
|
|
381
|
+
response_type: str,
|
|
382
|
+
content_type: str | None,
|
|
383
|
+
):
|
|
384
|
+
"""Deserializes response into an object.
|
|
385
|
+
|
|
386
|
+
:param response: RESTResponse object to be deserialized.
|
|
387
|
+
:param response_type: class literal for
|
|
388
|
+
deserialized object, or string of class name.
|
|
389
|
+
:param content_type: content type of response.
|
|
390
|
+
|
|
391
|
+
:return: deserialized object.
|
|
392
|
+
"""
|
|
393
|
+
# fetch data from response object
|
|
394
|
+
if content_type is None:
|
|
395
|
+
try:
|
|
396
|
+
data = json.loads(response_text)
|
|
397
|
+
except ValueError:
|
|
398
|
+
data = response_text
|
|
399
|
+
elif re.match(
|
|
400
|
+
r"^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)",
|
|
401
|
+
content_type,
|
|
402
|
+
re.IGNORECASE,
|
|
403
|
+
):
|
|
404
|
+
if response_text == "":
|
|
405
|
+
data = ""
|
|
406
|
+
else:
|
|
407
|
+
data = json.loads(response_text)
|
|
408
|
+
elif re.match(r"^text\/[a-z.+-]+\s*(;|$)", content_type, re.IGNORECASE):
|
|
409
|
+
data = response_text
|
|
410
|
+
else:
|
|
411
|
+
raise ApiException(
|
|
412
|
+
status=0,
|
|
413
|
+
reason=f"Unsupported content type: {content_type}",
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
return self.__deserialize(data, response_type)
|
|
417
|
+
|
|
418
|
+
def __deserialize(self, data, klass):
|
|
419
|
+
"""Deserializes dict, list, str into an object.
|
|
420
|
+
|
|
421
|
+
:param data: dict, list or str.
|
|
422
|
+
:param klass: class literal, or string of class name.
|
|
423
|
+
|
|
424
|
+
:return: object.
|
|
425
|
+
"""
|
|
426
|
+
if data is None:
|
|
427
|
+
return None
|
|
428
|
+
|
|
429
|
+
if isinstance(klass, str):
|
|
430
|
+
if klass.startswith("List["):
|
|
431
|
+
m = re.match(r"List\[(.*)]", klass)
|
|
432
|
+
assert m is not None, "Malformed List type definition"
|
|
433
|
+
sub_kls = m.group(1)
|
|
434
|
+
return [self.__deserialize(sub_data, sub_kls) for sub_data in data]
|
|
435
|
+
|
|
436
|
+
if klass.startswith("Dict["):
|
|
437
|
+
m = re.match(r"Dict\[([^,]*), (.*)]", klass)
|
|
438
|
+
assert m is not None, "Malformed Dict type definition"
|
|
439
|
+
sub_kls = m.group(2)
|
|
440
|
+
return {k: self.__deserialize(v, sub_kls) for k, v in data.items()}
|
|
441
|
+
|
|
442
|
+
# convert str to class
|
|
443
|
+
if klass in self.NATIVE_TYPES_MAPPING:
|
|
444
|
+
klass = self.NATIVE_TYPES_MAPPING[klass]
|
|
445
|
+
else:
|
|
446
|
+
klass = getattr(models, klass)
|
|
447
|
+
|
|
448
|
+
if klass in self.PRIMITIVE_TYPES:
|
|
449
|
+
return self.__deserialize_primitive(data, klass)
|
|
450
|
+
if klass == object:
|
|
451
|
+
return self.__deserialize_object(data)
|
|
452
|
+
if klass == datetime.date:
|
|
453
|
+
return self.__deserialize_date(data)
|
|
454
|
+
if klass == datetime.datetime:
|
|
455
|
+
return self.__deserialize_datetime(data)
|
|
456
|
+
if klass == decimal.Decimal:
|
|
457
|
+
return decimal.Decimal(data)
|
|
458
|
+
if issubclass(klass, Enum):
|
|
459
|
+
return self.__deserialize_enum(data, klass)
|
|
460
|
+
return self.__deserialize_model(data, klass)
|
|
461
|
+
|
|
462
|
+
def parameters_to_tuples(self, params, collection_formats):
|
|
463
|
+
"""Get parameters as list of tuples, formatting collections.
|
|
464
|
+
|
|
465
|
+
:param params: Parameters as dict or list of two-tuples
|
|
466
|
+
:param dict collection_formats: Parameter collection formats
|
|
467
|
+
:return: Parameters as list of tuples, collections formatted
|
|
468
|
+
"""
|
|
469
|
+
new_params: list[tuple[str, str]] = []
|
|
470
|
+
if collection_formats is None:
|
|
471
|
+
collection_formats = {}
|
|
472
|
+
for k, v in params.items() if isinstance(params, dict) else params:
|
|
473
|
+
if k in collection_formats:
|
|
474
|
+
collection_format = collection_formats[k]
|
|
475
|
+
if collection_format == "multi":
|
|
476
|
+
new_params.extend((k, value) for value in v)
|
|
477
|
+
else:
|
|
478
|
+
if collection_format == "ssv":
|
|
479
|
+
delimiter = " "
|
|
480
|
+
elif collection_format == "tsv":
|
|
481
|
+
delimiter = "\t"
|
|
482
|
+
elif collection_format == "pipes":
|
|
483
|
+
delimiter = "|"
|
|
484
|
+
else: # csv is the default
|
|
485
|
+
delimiter = ","
|
|
486
|
+
new_params.append((k, delimiter.join(str(value) for value in v)))
|
|
487
|
+
else:
|
|
488
|
+
new_params.append((k, v))
|
|
489
|
+
return new_params
|
|
490
|
+
|
|
491
|
+
def parameters_to_url_query(self, params, collection_formats):
|
|
492
|
+
"""Get parameters as list of tuples, formatting collections.
|
|
493
|
+
|
|
494
|
+
:param params: Parameters as dict or list of two-tuples
|
|
495
|
+
:param dict collection_formats: Parameter collection formats
|
|
496
|
+
:return: URL query string (e.g. a=Hello%20World&b=123)
|
|
497
|
+
"""
|
|
498
|
+
new_params: list[tuple[str, str]] = []
|
|
499
|
+
if collection_formats is None:
|
|
500
|
+
collection_formats = {}
|
|
501
|
+
for k, v in params.items() if isinstance(params, dict) else params:
|
|
502
|
+
if isinstance(v, bool):
|
|
503
|
+
v = str(v).lower()
|
|
504
|
+
if isinstance(v, (int, float)):
|
|
505
|
+
v = str(v)
|
|
506
|
+
if isinstance(v, dict):
|
|
507
|
+
v = json.dumps(v)
|
|
508
|
+
|
|
509
|
+
if k in collection_formats:
|
|
510
|
+
collection_format = collection_formats[k]
|
|
511
|
+
if collection_format == "multi":
|
|
512
|
+
new_params.extend((k, str(value)) for value in v)
|
|
513
|
+
else:
|
|
514
|
+
if collection_format == "ssv":
|
|
515
|
+
delimiter = " "
|
|
516
|
+
elif collection_format == "tsv":
|
|
517
|
+
delimiter = "\t"
|
|
518
|
+
elif collection_format == "pipes":
|
|
519
|
+
delimiter = "|"
|
|
520
|
+
else: # csv is the default
|
|
521
|
+
delimiter = ","
|
|
522
|
+
new_params.append(
|
|
523
|
+
(k, delimiter.join(quote(str(value)) for value in v)),
|
|
524
|
+
)
|
|
525
|
+
else:
|
|
526
|
+
new_params.append((k, quote(str(v))))
|
|
527
|
+
|
|
528
|
+
return "&".join(["=".join(map(str, item)) for item in new_params])
|
|
529
|
+
|
|
530
|
+
def files_parameters(
|
|
531
|
+
self,
|
|
532
|
+
files: dict[str, str | bytes | list[str] | list[bytes] | tuple[str, bytes]],
|
|
533
|
+
):
|
|
534
|
+
"""Builds form parameters.
|
|
535
|
+
|
|
536
|
+
:param files: File parameters.
|
|
537
|
+
:return: Form parameters with files.
|
|
538
|
+
"""
|
|
539
|
+
params = []
|
|
540
|
+
for k, v in files.items():
|
|
541
|
+
if isinstance(v, str):
|
|
542
|
+
with open(v, "rb") as f:
|
|
543
|
+
filename = os.path.basename(f.name)
|
|
544
|
+
filedata = f.read()
|
|
545
|
+
elif isinstance(v, bytes):
|
|
546
|
+
filename = k
|
|
547
|
+
filedata = v
|
|
548
|
+
elif isinstance(v, tuple):
|
|
549
|
+
filename, filedata = v
|
|
550
|
+
elif isinstance(v, list):
|
|
551
|
+
for file_param in v:
|
|
552
|
+
params.extend(self.files_parameters({k: file_param}))
|
|
553
|
+
continue
|
|
554
|
+
else:
|
|
555
|
+
raise ValueError("Unsupported file value")
|
|
556
|
+
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
|
|
557
|
+
params.append(
|
|
558
|
+
tuple([k, tuple([filename, filedata, mimetype])]),
|
|
559
|
+
)
|
|
560
|
+
return params
|
|
561
|
+
|
|
562
|
+
def select_header_accept(self, accepts: list[str]) -> str | None:
|
|
563
|
+
"""Returns `Accept` based on an array of accepts provided.
|
|
564
|
+
|
|
565
|
+
:param accepts: List of headers.
|
|
566
|
+
:return: Accept (e.g. application/json).
|
|
567
|
+
"""
|
|
568
|
+
if not accepts:
|
|
569
|
+
return None
|
|
570
|
+
|
|
571
|
+
for accept in accepts:
|
|
572
|
+
if re.search("json", accept, re.IGNORECASE):
|
|
573
|
+
return accept
|
|
574
|
+
|
|
575
|
+
return accepts[0]
|
|
576
|
+
|
|
577
|
+
def select_header_content_type(self, content_types):
|
|
578
|
+
"""Returns `Content-Type` based on an array of content_types provided.
|
|
579
|
+
|
|
580
|
+
:param content_types: List of content-types.
|
|
581
|
+
:return: Content-Type (e.g. application/json).
|
|
582
|
+
"""
|
|
583
|
+
if not content_types:
|
|
584
|
+
return None
|
|
585
|
+
|
|
586
|
+
for content_type in content_types:
|
|
587
|
+
if re.search("json", content_type, re.IGNORECASE):
|
|
588
|
+
return content_type
|
|
589
|
+
|
|
590
|
+
return content_types[0]
|
|
591
|
+
|
|
592
|
+
def update_params_for_auth(
|
|
593
|
+
self,
|
|
594
|
+
headers,
|
|
595
|
+
queries,
|
|
596
|
+
auth_settings,
|
|
597
|
+
resource_path,
|
|
598
|
+
method,
|
|
599
|
+
body,
|
|
600
|
+
request_auth=None,
|
|
601
|
+
) -> None:
|
|
602
|
+
"""Updates header and query params based on authentication setting.
|
|
603
|
+
|
|
604
|
+
:param headers: Header parameters dict to be updated.
|
|
605
|
+
:param queries: Query parameters tuple list to be updated.
|
|
606
|
+
:param auth_settings: Authentication setting identifiers list.
|
|
607
|
+
:resource_path: A string representation of the HTTP request resource path.
|
|
608
|
+
:method: A string representation of the HTTP request method.
|
|
609
|
+
:body: A object representing the body of the HTTP request.
|
|
610
|
+
The object type is the return value of sanitize_for_serialization().
|
|
611
|
+
:param request_auth: if set, the provided settings will
|
|
612
|
+
override the token in the configuration.
|
|
613
|
+
"""
|
|
614
|
+
if not auth_settings:
|
|
615
|
+
return
|
|
616
|
+
|
|
617
|
+
if request_auth:
|
|
618
|
+
self._apply_auth_params(
|
|
619
|
+
headers,
|
|
620
|
+
queries,
|
|
621
|
+
resource_path,
|
|
622
|
+
method,
|
|
623
|
+
body,
|
|
624
|
+
request_auth,
|
|
625
|
+
)
|
|
626
|
+
else:
|
|
627
|
+
for auth in auth_settings:
|
|
628
|
+
auth_setting = self.configuration.auth_settings().get(auth)
|
|
629
|
+
if auth_setting:
|
|
630
|
+
self._apply_auth_params(
|
|
631
|
+
headers,
|
|
632
|
+
queries,
|
|
633
|
+
resource_path,
|
|
634
|
+
method,
|
|
635
|
+
body,
|
|
636
|
+
auth_setting,
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
def _apply_auth_params(
|
|
640
|
+
self,
|
|
641
|
+
headers,
|
|
642
|
+
queries,
|
|
643
|
+
resource_path,
|
|
644
|
+
method,
|
|
645
|
+
body,
|
|
646
|
+
auth_setting,
|
|
647
|
+
) -> None:
|
|
648
|
+
"""Updates the request parameters based on a single auth_setting
|
|
649
|
+
|
|
650
|
+
:param headers: Header parameters dict to be updated.
|
|
651
|
+
:param queries: Query parameters tuple list to be updated.
|
|
652
|
+
:resource_path: A string representation of the HTTP request resource path.
|
|
653
|
+
:method: A string representation of the HTTP request method.
|
|
654
|
+
:body: A object representing the body of the HTTP request.
|
|
655
|
+
The object type is the return value of sanitize_for_serialization().
|
|
656
|
+
:param auth_setting: auth settings for the endpoint
|
|
657
|
+
"""
|
|
658
|
+
if auth_setting["in"] == "cookie":
|
|
659
|
+
headers["Cookie"] = auth_setting["value"]
|
|
660
|
+
elif auth_setting["in"] == "header":
|
|
661
|
+
if auth_setting["type"] != "http-signature":
|
|
662
|
+
headers[auth_setting["key"]] = auth_setting["value"]
|
|
663
|
+
elif auth_setting["in"] == "query":
|
|
664
|
+
queries.append((auth_setting["key"], auth_setting["value"]))
|
|
665
|
+
else:
|
|
666
|
+
raise ApiValueError(
|
|
667
|
+
"Authentication token must be in `query` or `header`",
|
|
668
|
+
)
|
|
669
|
+
|
|
670
|
+
def __deserialize_file(self, response):
|
|
671
|
+
"""Deserializes body to file
|
|
672
|
+
|
|
673
|
+
Saves response body into a file in a temporary folder,
|
|
674
|
+
using the filename from the `Content-Disposition` header if provided.
|
|
675
|
+
|
|
676
|
+
handle file downloading
|
|
677
|
+
save response body into a tmp file and return the instance
|
|
678
|
+
|
|
679
|
+
:param response: RESTResponse.
|
|
680
|
+
:return: file path.
|
|
681
|
+
"""
|
|
682
|
+
fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
|
|
683
|
+
os.close(fd)
|
|
684
|
+
os.remove(path)
|
|
685
|
+
|
|
686
|
+
content_disposition = response.getheader("Content-Disposition")
|
|
687
|
+
if content_disposition:
|
|
688
|
+
m = re.search(
|
|
689
|
+
r'filename=[\'"]?([^\'"\s]+)[\'"]?',
|
|
690
|
+
content_disposition,
|
|
691
|
+
)
|
|
692
|
+
assert m is not None, "Unexpected 'content-disposition' header value"
|
|
693
|
+
filename = m.group(1)
|
|
694
|
+
path = os.path.join(os.path.dirname(path), filename)
|
|
695
|
+
|
|
696
|
+
with open(path, "wb") as f:
|
|
697
|
+
f.write(response.data)
|
|
698
|
+
|
|
699
|
+
return path
|
|
700
|
+
|
|
701
|
+
def __deserialize_primitive(self, data, klass):
|
|
702
|
+
"""Deserializes string to primitive type.
|
|
703
|
+
|
|
704
|
+
:param data: str.
|
|
705
|
+
:param klass: class literal.
|
|
706
|
+
|
|
707
|
+
:return: int, long, float, str, bool.
|
|
708
|
+
"""
|
|
709
|
+
try:
|
|
710
|
+
return klass(data)
|
|
711
|
+
except UnicodeEncodeError:
|
|
712
|
+
return str(data)
|
|
713
|
+
except TypeError:
|
|
714
|
+
return data
|
|
715
|
+
|
|
716
|
+
def __deserialize_object(self, value):
|
|
717
|
+
"""Return an original value.
|
|
718
|
+
|
|
719
|
+
:return: object.
|
|
720
|
+
"""
|
|
721
|
+
return value
|
|
722
|
+
|
|
723
|
+
def __deserialize_date(self, string):
|
|
724
|
+
"""Deserializes string to date.
|
|
725
|
+
|
|
726
|
+
:param string: str.
|
|
727
|
+
:return: date.
|
|
728
|
+
"""
|
|
729
|
+
try:
|
|
730
|
+
return parse(string).date()
|
|
731
|
+
except ImportError:
|
|
732
|
+
return string
|
|
733
|
+
except ValueError:
|
|
734
|
+
raise rest.ApiException(
|
|
735
|
+
status=0,
|
|
736
|
+
reason=f"Failed to parse `{string}` as date object",
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
def __deserialize_datetime(self, string):
|
|
740
|
+
"""Deserializes string to datetime.
|
|
741
|
+
|
|
742
|
+
The string should be in iso8601 datetime format.
|
|
743
|
+
|
|
744
|
+
:param string: str.
|
|
745
|
+
:return: datetime.
|
|
746
|
+
"""
|
|
747
|
+
try:
|
|
748
|
+
return parse(string)
|
|
749
|
+
except ImportError:
|
|
750
|
+
return string
|
|
751
|
+
except ValueError:
|
|
752
|
+
raise rest.ApiException(
|
|
753
|
+
status=0,
|
|
754
|
+
reason=(f"Failed to parse `{string}` as datetime object"),
|
|
755
|
+
)
|
|
756
|
+
|
|
757
|
+
def __deserialize_enum(self, data, klass):
|
|
758
|
+
"""Deserializes primitive type to enum.
|
|
759
|
+
|
|
760
|
+
:param data: primitive type.
|
|
761
|
+
:param klass: class literal.
|
|
762
|
+
:return: enum value.
|
|
763
|
+
"""
|
|
764
|
+
try:
|
|
765
|
+
return klass(data)
|
|
766
|
+
except ValueError:
|
|
767
|
+
raise rest.ApiException(
|
|
768
|
+
status=0,
|
|
769
|
+
reason=(f"Failed to parse `{data}` as `{klass}`"),
|
|
770
|
+
)
|
|
771
|
+
|
|
772
|
+
def __deserialize_model(self, data, klass):
|
|
773
|
+
"""Deserializes list or dict to model.
|
|
774
|
+
|
|
775
|
+
:param data: dict, list.
|
|
776
|
+
:param klass: class literal.
|
|
777
|
+
:return: model object.
|
|
778
|
+
"""
|
|
779
|
+
return klass.from_dict(data)
|