Deserializes body to file Saves response body into a file in a temporary folder, using the filename from the `Content-Disposition` header if provided. Args: param response_data (str): the file data to write configuration (Configuration): the instance to use to convert
(response_data, configuration, content_disposition=None)
| 1380 | |
| 1381 | |
| 1382 | def deserialize_file(response_data, configuration, content_disposition=None): |
| 1383 | """Deserializes body to file |
| 1384 | |
| 1385 | Saves response body into a file in a temporary folder, |
| 1386 | using the filename from the `Content-Disposition` header if provided. |
| 1387 | |
| 1388 | Args: |
| 1389 | param response_data (str): the file data to write |
| 1390 | configuration (Configuration): the instance to use to convert files |
| 1391 | |
| 1392 | Keyword Args: |
| 1393 | content_disposition (str): the value of the Content-Disposition |
| 1394 | header |
| 1395 | |
| 1396 | Returns: |
| 1397 | (file_type): the deserialized file which is open |
| 1398 | The user is responsible for closing and reading the file |
| 1399 | """ |
| 1400 | fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path) |
| 1401 | os.close(fd) |
| 1402 | os.remove(path) |
| 1403 | |
| 1404 | if content_disposition: |
| 1405 | filename = re.search( |
| 1406 | r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition, flags=re.I |
| 1407 | ) |
| 1408 | if filename is not None: |
| 1409 | filename = filename.group(1) |
| 1410 | else: |
| 1411 | filename = "default_" + str(uuid.uuid4()) |
| 1412 | |
| 1413 | path = os.path.join(os.path.dirname(path), filename) |
| 1414 | |
| 1415 | with open(path, "wb") as f: |
| 1416 | if isinstance(response_data, str): |
| 1417 | # change str to bytes so we can write it |
| 1418 | response_data = response_data.encode("utf-8") |
| 1419 | f.write(response_data) |
| 1420 | |
| 1421 | f = open(path, "rb") |
| 1422 | return f |
| 1423 | |
| 1424 | |
| 1425 | def attempt_convert_item( |
no test coverage detected