@@ -6,8 +6,6 @@ import mimetypes
6
6
import warnings
7
7
import multiprocessing
8
8
from multiprocessing.pool import ThreadPool
9
- from datetime import date, datetime
10
- from uuid import UUID
11
9
import io
12
10
import os
13
11
import re
@@ -21,14 +19,12 @@ from {{ package }} import rest
21
19
from {{ package }}.configuration import Configuration
22
20
from {{ package }}.exceptions import ApiTypeError, ApiValueError
23
21
from {{ package }}.model_utils import (
24
- ModelNormal,
25
- ModelSimple,
26
- ModelComposed,
27
22
check_allowed_values,
28
23
check_validations,
29
24
deserialize_file,
30
25
file_type,
31
- model_to_dict,
26
+ data_to_dict,
27
+ get_file_data_and_close_file,
32
28
validate_and_convert_types,
33
29
get_attribute_from_path,
34
30
set_attribute_from_path,
@@ -154,40 +150,6 @@ class ApiClient:
154
150
new_params.append((k, v))
155
151
return new_params
156
152
157
- @classmethod
158
- def sanitize_for_serialization(cls, obj):
159
- """Prepares data for transmission before it is sent with the rest client.
160
- If obj is None, return None.
161
- If obj is str, int, long, float, bool, return directly.
162
- If obj is datetime.datetime, datetime.date convert to string in iso8601 format.
163
- If obj is list, sanitize each element in the list.
164
- If obj is dict, return the dict.
165
- If obj is OpenAPI model, return the properties dict.
166
- If obj is io.IOBase, return the bytes.
167
-
168
- :param obj: The data to serialize.
169
- :return: The serialized form of data.
170
- """
171
- if isinstance(obj, (ModelNormal, ModelComposed)):
172
- return {key: cls.sanitize_for_serialization(val) for key, val in model_to_dict(obj).items()}
173
- elif isinstance(obj, io.IOBase):
174
- return cls.get_file_data_and_close_file(obj)
175
- elif isinstance(obj, (str, int, float, bool)) or obj is None:
176
- return obj
177
- elif isinstance(obj, (datetime, date)):
178
- if getattr(obj, "tzinfo", None) is not None:
179
- return obj.isoformat()
180
- return "{}Z".format(obj.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3])
181
- elif isinstance(obj, UUID ):
182
- return str(obj)
183
- elif isinstance(obj, ModelSimple):
184
- return cls.sanitize_for_serialization(obj.value)
185
- elif isinstance(obj, (list, tuple)):
186
- return [cls.sanitize_for_serialization(item) for item in obj]
187
- if isinstance(obj, dict):
188
- return {key: cls.sanitize_for_serialization(val) for key, val in obj.items()}
189
- raise ApiValueError("Unable to prepare type {} for serialization".format(obj.__class__.__name__))
190
-
191
153
def deserialize(self, response_data: str, response_type: Any, check_type: Optional[bool]):
192
154
"""Deserializes response into an object.
193
155
@@ -286,12 +248,12 @@ class ApiClient:
286
248
header_params = header_params or {}
287
249
header_params.update(self.default_headers)
288
250
if header_params:
289
- header_params = self.sanitize_for_serialization (header_params)
251
+ header_params = data_to_dict (header_params)
290
252
header_params = dict(self.parameters_to_tuples(header_params, collection_formats))
291
253
292
254
# path parameters
293
255
if path_params:
294
- path_params = self.sanitize_for_serialization (path_params)
256
+ path_params = data_to_dict (path_params)
295
257
for k, v in self.parameters_to_tuples(path_params, collection_formats):
296
258
# specified safe chars, encode everything
297
259
resource_path = resource_path.replace(
@@ -302,21 +264,21 @@ class ApiClient:
302
264
303
265
# query parameters
304
266
if query_params:
305
- query_params = self.sanitize_for_serialization (query_params)
267
+ query_params = data_to_dict (query_params)
306
268
query_params = self.parameters_to_tuples(query_params, collection_formats)
307
269
308
270
# post parameters
309
271
if post_params or files:
310
272
post_params = post_params or []
311
- post_params = self.sanitize_for_serialization (post_params)
273
+ post_params = data_to_dict (post_params)
312
274
post_params = self.parameters_to_tuples(post_params, collection_formats)
313
275
post_params.extend(self.files_parameters(files))
314
276
if header_params["Content-Type"].startswith("multipart"):
315
277
post_params = self.parameters_to_multipart(post_params)
316
278
317
279
# body
318
280
if body:
319
- body = self.sanitize_for_serialization (body)
281
+ body = data_to_dict (body)
320
282
321
283
# request url
322
284
if host is None:
@@ -439,12 +401,6 @@ class ApiClient:
439
401
new_params.append((k, v))
440
402
return new_params
441
403
442
- @staticmethod
443
- def get_file_data_and_close_file(file_instance: io.IOBase) -> bytes:
444
- file_data = file_instance.read()
445
- file_instance.close()
446
- return file_data
447
-
448
404
def files_parameters(self, files: Optional[Dict[str, List[io.FileIO]]] = None):
449
405
"""Builds form parameters.
450
406
@@ -469,7 +425,7 @@ class ApiClient:
469
425
"Cannot read a closed file. The passed in file_type " "for %s must be open." % param_name
470
426
)
471
427
filename = os.path.basename(str(file_instance.name))
472
- filedata = self. get_file_data_and_close_file(file_instance)
428
+ filedata = get_file_data_and_close_file(file_instance)
473
429
mimetype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
474
430
params.append(tuple([param_name, tuple([filename, filedata, mimetype])]))
475
431
0 commit comments