1+ from fluxpoint .errors import paramEnumValidCheck
12from fluxpoint .vars import RequestTypes
23from fluxpoint .http import BaseHTTP
4+ from fluxpoint .vars import ImageType
5+ import io
6+ import aiohttp
37
48class Convert (BaseHTTP ):
59 """Convert Api endpoints documented in https://docs.fluxpoint.dev/api/endpoints/convert
@@ -9,4 +13,64 @@ def __init__(self, *args, **kwargs) -> None:
913 self .__baseurl = '/convert'
1014 super ().__init__ (* args , ** kwargs )
1115
16+ @staticmethod
17+ def imgdata2form (image : io .IOBase ) -> aiohttp .FormData :
18+ """
19+ Converts image data to formdata
1220
21+ :param image: image data
22+ :type image: :class:`io.IOBase`
23+ :return: aiohttp form data class
24+ :rtype: :class:`aiohttp.FormData`
25+ """
26+ form = aiohttp .FormData ()
27+ form .add_field ("file" , image , filename = "upload" , content_type = "application/octet-stream" )
28+ return form
29+
30+ async def html2markdown (self , html :str ) -> str :
31+ """
32+ Convert html to markdown
33+ :param html: The html to convert
34+ :type html: str
35+ :return: The converted html to markdown
36+ :rtype: str
37+ """
38+ return (await self .request (RequestTypes .POST , f'{ self .__baseurl } /html-markdown' ,data = html )).get ('markdown' )
39+
40+ async def markdown2html (self , markdown :str ) -> str :
41+ """
42+ Convert markdown to html
43+ :param markdown: The markdown to convert
44+ :type markdown: str
45+ :return: The converted markdown to html
46+ :rtype: str
47+ """
48+ return (await self .request (RequestTypes .POST , f'{ self .__baseurl } /markdown-html' ,data = markdown )).get ('html' )
49+
50+ async def image2type (self , image : io .IOBase , imgtype : ImageType , quality : int = 100 ) -> io .IOBase :
51+ """
52+ Convert an image to png/jpg/webp
53+
54+ :raises ValueError: When the ``quality`` parameter is not between ``1`` and ``100``
55+
56+ :param image: The image to convert
57+ :type image: :class:`io.IOBase`
58+ :param quality: The quality to convert to
59+ :type quality: int
60+ :param imgtype: The type to convert to
61+ :type imgtype: :class:`ImageType`
62+
63+ :return: The converted image
64+ :rtype: :class:`io.IOBase`
65+ """
66+ if quality > 100 or quality < 1 :
67+ raise ValueError ("The return quality value must be between 1 and 100" )
68+ paramEnumValidCheck (image , ImageType )
69+ return await self .request (
70+ RequestTypes .POST ,
71+ f'{ self .__baseurl } /image-{ imgtype .value } ' ,
72+ data = self .__class__ .imgdata2form (image ),
73+ return_bytes = True ,
74+ return_json = False ,
75+ params = {"quality" : quality }
76+ )
0 commit comments