2022-06-11 17:59:55 -07:00
import base64
2023-08-17 12:48:13 -07:00
from typing import Any , Optional , Tuple , Union
2022-06-11 17:59:55 -07:00
2023-05-04 00:58:06 -07:00
from . . . api . file . create_file_conversion import asyncio as fc_asyncio , sync as fc_sync
2022-06-11 17:59:55 -07:00
from . . . client import Client
2023-05-04 00:58:06 -07:00
from . . . models import Error , FileConversion , FileExportFormat , FileImportFormat
2022-06-11 17:59:55 -07:00
def sync (
2022-10-03 12:05:22 -07:00
src_format : FileImportFormat ,
output_format : FileExportFormat ,
2022-06-11 17:59:55 -07:00
body : bytes ,
* ,
client : Client ,
2023-08-17 12:48:13 -07:00
) - > Optional [ Union [ Any , Tuple [ FileConversion , bytes ] , Error ] ] :
2022-06-11 17:59:55 -07:00
""" Convert a CAD file from one format to another. If the file being converted is larger than a certain size it will be performed asynchronously. This function automatically base64 encodes the request body and base64 decodes the request output. """
encoded = base64 . b64encode ( body )
fc = fc_sync (
src_format = src_format ,
output_format = output_format ,
body = encoded ,
client = client ,
)
if isinstance ( fc , FileConversion ) and fc . output != " " :
2023-05-04 00:58:06 -07:00
if isinstance ( fc . output , str ) :
2023-08-17 14:02:51 -07:00
b = base64 . b64decode ( fc . output + " === " )
2023-08-17 12:48:13 -07:00
return ( fc , b )
2022-06-11 17:59:55 -07:00
return fc
async def asyncio (
2022-10-03 12:05:22 -07:00
src_format : FileImportFormat ,
output_format : FileExportFormat ,
2022-06-11 17:59:55 -07:00
body : bytes ,
* ,
client : Client ,
2023-08-17 12:48:13 -07:00
) - > Optional [ Union [ Any , Tuple [ FileConversion , bytes ] , Error ] ] :
2022-06-11 17:59:55 -07:00
""" Convert a CAD file from one format to another. If the file being converted is larger than a certain size it will be performed asynchronously. This function automatically base64 encodes the request body and base64 decodes the request output. """
encoded = base64 . b64encode ( body )
fc = await fc_asyncio (
2023-05-04 00:58:06 -07:00
src_format = src_format ,
output_format = output_format ,
body = encoded ,
client = client ,
)
2022-06-11 17:59:55 -07:00
if isinstance ( fc , FileConversion ) and fc . output != " " :
2023-05-04 00:58:06 -07:00
if isinstance ( fc . output , str ) :
2023-08-17 14:02:51 -07:00
b = base64 . b64decode ( fc . output + " === " )
2023-08-17 12:48:13 -07:00
return ( fc , b )
2022-06-11 17:59:55 -07:00
return fc