33from typing import Any , Dict , Optional
44import httpx
55import time
6+ from pathlib import Path
67
78from .errors import *
89# internal models live under ./internal (generated by openapi-generator-cli)
@@ -37,6 +38,31 @@ def _coerce_optional_bool(value: Any) -> Optional[bool]:
3738 return bool (value )
3839 return bool (value )
3940
41+
42+ def _stringify_form_value (value : Any ) -> str :
43+ if isinstance (value , bool ):
44+ return "true" if value else "false"
45+ return str (value )
46+
47+
48+ def _prepare_file_value (value : Any ):
49+ if isinstance (value , (str , Path )):
50+ path = Path (value )
51+ if not path .is_file ():
52+ raise FileNotFoundError (f"File not found: { path } " )
53+ return (path .name , path .read_bytes ())
54+ if isinstance (value , (bytes , bytearray )):
55+ return ("upload.bin" , bytes (value ))
56+ if isinstance (value , tuple ):
57+ return value
58+ if hasattr (value , "read" ):
59+ content = value .read ()
60+ if isinstance (content , str ):
61+ content = content .encode ("utf-8" )
62+ file_name = Path (getattr (value , "name" , "upload.bin" )).name
63+ return (file_name or "upload.bin" , content )
64+ raise TypeError (f"Unsupported multipart file value: { type (value )!r} " )
65+
4066class _HTTP :
4167 def __init__ (self , cfg : _Config ):
4268 self ._cfg = cfg
@@ -62,6 +88,8 @@ def request(
6288 * ,
6389 params : Dict [str , Any ] | None = None ,
6490 json : Any | None = None ,
91+ data : Dict [str , Any ] | None = None ,
92+ files : Dict [str , Any ] | None = None ,
6593 headers : Dict [str , str ] | None = None ,
6694 disable_cache : bool | None = None ,
6795 ):
@@ -70,7 +98,7 @@ def request(
7098 if self ._cfg .token :
7199 headers ["Authorization" ] = f"Bearer { self ._cfg .token } "
72100 query = self ._apply_cache_control (method , params , disable_cache )
73- r = self ._client .request (method , url , params = query , json = json , headers = headers )
101+ r = self ._client .request (method , url , params = query , json = json , data = data , files = files , headers = headers )
74102 if r .status_code >= 400 :
75103 err = map_error (r )
76104 self .last_response_meta = err .meta
@@ -842,7 +870,8 @@ def post_image_compress(self, **kwargs):
842870- **500 Internal Server Error**: 如果在压缩过程中服务器发生内部错误,会返回此状态码。
843871 """
844872 params = {}
845- body = {}
873+ data = {}
874+ files = {}
846875
847876 if "query" == "query" and "level" in kwargs :
848877 params ["level" ] = kwargs ["level" ]
@@ -854,7 +883,7 @@ def post_image_compress(self, **kwargs):
854883 params ["_t" ] = kwargs ["_t" ]
855884
856885 if "file" in kwargs :
857- body ["file" ] = kwargs ["file" ]
886+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
858887
859888 disable_cache = kwargs .get ("disable_cache" )
860889 if disable_cache is None and "disableCache" in kwargs :
@@ -865,7 +894,8 @@ def post_image_compress(self, **kwargs):
865894 "POST" ,
866895 path ,
867896 params = params ,
868- json = body if body else None ,
897+ data = data or None ,
898+ files = files or None ,
869899 disable_cache = _coerce_optional_bool (disable_cache ),
870900 )
871901
@@ -884,7 +914,8 @@ def post_image_decode(self, **kwargs):
884914- **网络资源获取**:当您选择传递图片链接时,服务端会自动尝试获取该资源。请确保您提供的图片链接是公网直接可访问的,且不需要任何形式的登录鉴权。
885915 """
886916 params = {}
887- body = {}
917+ data = {}
918+ files = {}
888919
889920 if "query" == "query" and "width" in kwargs :
890921 params ["width" ] = kwargs ["width" ]
@@ -914,10 +945,10 @@ def post_image_decode(self, **kwargs):
914945 params ["_t" ] = kwargs ["_t" ]
915946
916947 if "file" in kwargs :
917- body ["file" ] = kwargs ["file" ]
948+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
918949
919950 if "url" in kwargs :
920- body ["url" ] = kwargs ["url" ]
951+ data ["url" ] = _stringify_form_value ( kwargs ["url" ])
921952
922953 disable_cache = kwargs .get ("disable_cache" )
923954 if disable_cache is None and "disableCache" in kwargs :
@@ -928,7 +959,8 @@ def post_image_decode(self, **kwargs):
928959 "POST" ,
929960 path ,
930961 params = params ,
931- json = body if body else None ,
962+ data = data or None ,
963+ files = files or None ,
932964 disable_cache = _coerce_optional_bool (disable_cache ),
933965 )
934966
@@ -982,19 +1014,20 @@ def post_image_motou(self, **kwargs):
9821014- **背景颜色**:同样支持 `bg_color` 表单字段来控制GIF背景。
9831015 """
9841016 params = {}
985- body = {}
1017+ data = {}
1018+ files = {}
9861019
9871020 if "_t" in kwargs :
9881021 params ["_t" ] = kwargs ["_t" ]
9891022
9901023 if "bg_color" in kwargs :
991- body ["bg_color" ] = kwargs ["bg_color" ]
1024+ data ["bg_color" ] = _stringify_form_value ( kwargs ["bg_color" ])
9921025
9931026 if "file" in kwargs :
994- body ["file" ] = kwargs ["file" ]
1027+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
9951028
9961029 if "image_url" in kwargs :
997- body ["image_url" ] = kwargs ["image_url" ]
1030+ data ["image_url" ] = _stringify_form_value ( kwargs ["image_url" ])
9981031
9991032 disable_cache = kwargs .get ("disable_cache" )
10001033 if disable_cache is None and "disableCache" in kwargs :
@@ -1005,7 +1038,8 @@ def post_image_motou(self, **kwargs):
10051038 "POST" ,
10061039 path ,
10071040 params = params ,
1008- json = body if body else None ,
1041+ data = data or None ,
1042+ files = files or None ,
10091043 disable_cache = _coerce_optional_bool (disable_cache ),
10101044 )
10111045
@@ -1033,16 +1067,17 @@ def post_image_nsfw(self, **kwargs):
10331067- **inference_time_ms**: 模型推理耗时,单位毫秒
10341068 """
10351069 params = {}
1036- body = {}
1070+ data = {}
1071+ files = {}
10371072
10381073 if "_t" in kwargs :
10391074 params ["_t" ] = kwargs ["_t" ]
10401075
10411076 if "file" in kwargs :
1042- body ["file" ] = kwargs ["file" ]
1077+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
10431078
10441079 if "url" in kwargs :
1045- body ["url" ] = kwargs ["url" ]
1080+ data ["url" ] = _stringify_form_value ( kwargs ["url" ])
10461081
10471082 disable_cache = kwargs .get ("disable_cache" )
10481083 if disable_cache is None and "disableCache" in kwargs :
@@ -1053,7 +1088,8 @@ def post_image_nsfw(self, **kwargs):
10531088 "POST" ,
10541089 path ,
10551090 params = params ,
1056- json = body if body else None ,
1091+ data = data or None ,
1092+ files = files or None ,
10571093 disable_cache = _coerce_optional_bool (disable_cache ),
10581094 )
10591095
@@ -1071,31 +1107,32 @@ def post_image_ocr(self, **kwargs):
10711107- **灵活的输入与请求要求**:接口支持 `file`、`url` 或 `image_base64` 三种方式输入。请确保请求格式为 `multipart/form-data`,且图片链接在公网可直接访问。
10721108 """
10731109 params = {}
1074- body = {}
1110+ data = {}
1111+ files = {}
10751112
10761113 if "_t" in kwargs :
10771114 params ["_t" ] = kwargs ["_t" ]
10781115
10791116 if "enable_cls" in kwargs :
1080- body ["enable_cls" ] = kwargs ["enable_cls" ]
1117+ data ["enable_cls" ] = _stringify_form_value ( kwargs ["enable_cls" ])
10811118
10821119 if "file" in kwargs :
1083- body ["file" ] = kwargs ["file" ]
1120+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
10841121
10851122 if "image_base64" in kwargs :
1086- body ["image_base64" ] = kwargs ["image_base64" ]
1123+ data ["image_base64" ] = _stringify_form_value ( kwargs ["image_base64" ])
10871124
10881125 if "image_name" in kwargs :
1089- body ["image_name" ] = kwargs ["image_name" ]
1126+ data ["image_name" ] = _stringify_form_value ( kwargs ["image_name" ])
10901127
10911128 if "need_location" in kwargs :
1092- body ["need_location" ] = kwargs ["need_location" ]
1129+ data ["need_location" ] = _stringify_form_value ( kwargs ["need_location" ])
10931130
10941131 if "return_markdown" in kwargs :
1095- body ["return_markdown" ] = kwargs ["return_markdown" ]
1132+ data ["return_markdown" ] = _stringify_form_value ( kwargs ["return_markdown" ])
10961133
10971134 if "url" in kwargs :
1098- body ["url" ] = kwargs ["url" ]
1135+ data ["url" ] = _stringify_form_value ( kwargs ["url" ])
10991136
11001137 disable_cache = kwargs .get ("disable_cache" )
11011138 if disable_cache is None and "disableCache" in kwargs :
@@ -1106,7 +1143,8 @@ def post_image_ocr(self, **kwargs):
11061143 "POST" ,
11071144 path ,
11081145 params = params ,
1109- json = body if body else None ,
1146+ data = data or None ,
1147+ files = files or None ,
11101148 disable_cache = _coerce_optional_bool (disable_cache ),
11111149 )
11121150
@@ -1152,7 +1190,8 @@ def post_image_svg(self, **kwargs):
11521190上传一个 SVG 文件,并指定目标格式(如 PNG、JPEG 等),接口将返回转换后的图像。你还可以调整输出图像的尺寸和(对于JPEG)压缩质量,以满足不同场景的需求。
11531191 """
11541192 params = {}
1155- body = {}
1193+ data = {}
1194+ files = {}
11561195
11571196 if "query" == "query" and "format" in kwargs :
11581197 params ["format" ] = kwargs ["format" ]
@@ -1170,7 +1209,7 @@ def post_image_svg(self, **kwargs):
11701209 params ["_t" ] = kwargs ["_t" ]
11711210
11721211 if "file" in kwargs :
1173- body ["file" ] = kwargs ["file" ]
1212+ files ["file" ] = _prepare_file_value ( kwargs ["file" ])
11741213
11751214 disable_cache = kwargs .get ("disable_cache" )
11761215 if disable_cache is None and "disableCache" in kwargs :
@@ -1181,7 +1220,8 @@ def post_image_svg(self, **kwargs):
11811220 "POST" ,
11821221 path ,
11831222 params = params ,
1184- json = body if body else None ,
1223+ data = data or None ,
1224+ files = files or None ,
11851225 disable_cache = _coerce_optional_bool (disable_cache ),
11861226 )
11871227
0 commit comments