@@ -199,8 +199,6 @@ class ApiClient(object):
199
199
e.body = e.body.decode('utf-8')
200
200
raise e
201
201
202
- content_type = response_data.getheader('content-type')
203
-
204
202
self.last_response = response_data
205
203
206
204
return_data = response_data
@@ -214,15 +212,17 @@ class ApiClient(object):
214
212
{ {/tornado} }
215
213
return return_data
216
214
217
- if response_type not in ["file", "bytes"]:
218
- match = None
219
- if content_type is not None:
220
- match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type)
221
- encoding = match.group(1) if match else "utf-8"
222
- response_data.data = response_data.data.decode(encoding)
223
-
224
215
# deserialize response data
225
216
if response_type:
217
+ if response_type != (file_type,):
218
+ encoding = "utf-8"
219
+ content_type = response_data.getheader('content-type')
220
+ if content_type is not None:
221
+ match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type)
222
+ if match:
223
+ encoding = match.group(1)
224
+ response_data.data = response_data.data.decode(encoding)
225
+
226
226
return_data = self.deserialize(
227
227
response_data,
228
228
response_type,
@@ -268,21 +268,24 @@ class ApiClient(object):
268
268
269
269
@classmethod
270
270
def sanitize_for_serialization(cls, obj):
271
- """Builds a JSON POST object.
271
+ """Prepares data for transmission before it is sent with the rest client
272
272
If obj is None, return None.
273
273
If obj is str, int, long, float, bool, return directly.
274
274
If obj is datetime.datetime, datetime.date
275
275
convert to string in iso8601 format.
276
276
If obj is list, sanitize each element in the list.
277
277
If obj is dict, return the dict.
278
278
If obj is OpenAPI model, return the properties dict.
279
+ If obj is io.IOBase, return the bytes
279
280
:param obj: The data to serialize.
280
281
:return: The serialized form of data.
281
282
"""
282
283
if isinstance(obj, (ModelNormal, ModelComposed)):
283
284
return {
284
285
key: cls.sanitize_for_serialization(val) for key, val in model_to_dict(obj, serialize= True).items()
285
286
}
287
+ elif isinstance(obj, io.IOBase):
288
+ return cls.get_file_data_and_close_file(obj)
286
289
elif isinstance(obj, (str, int, float, none_type, bool)):
287
290
return obj
288
291
elif isinstance(obj, (datetime, date)):
@@ -526,6 +529,12 @@ class ApiClient(object):
526
529
new_params.append((k, v))
527
530
return new_params
528
531
532
+ @staticmethod
533
+ def get_file_data_and_close_file(file_instance: io.IOBase) -> bytes:
534
+ file_data = file_instance.read()
535
+ file_instance.close()
536
+ return file_data
537
+
529
538
def files_parameters(self, files: typing.Optional[typing.Dict[str, typing.List[io.IOBase]]] = None):
530
539
"""Builds form parameters.
531
540
@@ -551,12 +560,11 @@ class ApiClient(object):
551
560
"for %s must be open." % param_name
552
561
)
553
562
filename = os.path.basename(file_instance.name)
554
- filedata = file_instance.read( )
563
+ filedata = self.get_file_data_and_close_file(file_instance )
555
564
mimetype = (mimetypes.guess_type(filename)[0] or
556
565
'application/octet-stream')
557
566
params.append(
558
567
tuple([param_name, tuple([filename, filedata, mimetype])]))
559
- file_instance.close()
560
568
561
569
return params
562
570
0 commit comments