uapi-sdk-python 0.1.16__py3-none-any.whl → 0.1.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
uapi/client.py CHANGED
@@ -3,6 +3,7 @@ from dataclasses import dataclass
3
3
  from typing import Any, Dict, Optional
4
4
  import httpx
5
5
  import time
6
+ from pathlib import Path
6
7
 
7
8
  from .errors import *
8
9
  # internal models live under ./internal (generated by openapi-generator-cli)
@@ -37,6 +38,31 @@ def _coerce_optional_bool(value: Any) -> Optional[bool]:
37
38
  return bool(value)
38
39
  return bool(value)
39
40
 
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
+
40
66
  class _HTTP:
41
67
  def __init__(self, cfg: _Config):
42
68
  self._cfg = cfg
@@ -62,6 +88,8 @@ class _HTTP:
62
88
  *,
63
89
  params: Dict[str, Any] | None = None,
64
90
  json: Any | None = None,
91
+ data: Dict[str, Any] | None = None,
92
+ files: Dict[str, Any] | None = None,
65
93
  headers: Dict[str, str] | None = None,
66
94
  disable_cache: bool | None = None,
67
95
  ):
@@ -70,7 +98,7 @@ class _HTTP:
70
98
  if self._cfg.token:
71
99
  headers["Authorization"] = f"Bearer {self._cfg.token}"
72
100
  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)
74
102
  if r.status_code >= 400:
75
103
  err = map_error(r)
76
104
  self.last_response_meta = err.meta
@@ -842,7 +870,8 @@ class _ImageApi:
842
870
  - **500 Internal Server Error**: 如果在压缩过程中服务器发生内部错误,会返回此状态码。
843
871
  """
844
872
  params = {}
845
- body = {}
873
+ data = {}
874
+ files = {}
846
875
 
847
876
  if "query" == "query" and "level" in kwargs:
848
877
  params["level"] = kwargs["level"]
@@ -854,7 +883,7 @@ class _ImageApi:
854
883
  params["_t"] = kwargs["_t"]
855
884
 
856
885
  if "file" in kwargs:
857
- body["file"] = kwargs["file"]
886
+ files["file"] = _prepare_file_value(kwargs["file"])
858
887
 
859
888
  disable_cache = kwargs.get("disable_cache")
860
889
  if disable_cache is None and "disableCache" in kwargs:
@@ -865,7 +894,8 @@ class _ImageApi:
865
894
  "POST",
866
895
  path,
867
896
  params=params,
868
- json=body if body else None,
897
+ data=data or None,
898
+ files=files or None,
869
899
  disable_cache=_coerce_optional_bool(disable_cache),
870
900
  )
871
901
 
@@ -884,7 +914,8 @@ class _ImageApi:
884
914
  - **网络资源获取**:当您选择传递图片链接时,服务端会自动尝试获取该资源。请确保您提供的图片链接是公网直接可访问的,且不需要任何形式的登录鉴权。
885
915
  """
886
916
  params = {}
887
- body = {}
917
+ data = {}
918
+ files = {}
888
919
 
889
920
  if "query" == "query" and "width" in kwargs:
890
921
  params["width"] = kwargs["width"]
@@ -914,10 +945,10 @@ class _ImageApi:
914
945
  params["_t"] = kwargs["_t"]
915
946
 
916
947
  if "file" in kwargs:
917
- body["file"] = kwargs["file"]
948
+ files["file"] = _prepare_file_value(kwargs["file"])
918
949
 
919
950
  if "url" in kwargs:
920
- body["url"] = kwargs["url"]
951
+ data["url"] = _stringify_form_value(kwargs["url"])
921
952
 
922
953
  disable_cache = kwargs.get("disable_cache")
923
954
  if disable_cache is None and "disableCache" in kwargs:
@@ -928,7 +959,8 @@ class _ImageApi:
928
959
  "POST",
929
960
  path,
930
961
  params=params,
931
- json=body if body else None,
962
+ data=data or None,
963
+ files=files or None,
932
964
  disable_cache=_coerce_optional_bool(disable_cache),
933
965
  )
934
966
 
@@ -982,19 +1014,20 @@ class _ImageApi:
982
1014
  - **背景颜色**:同样支持 `bg_color` 表单字段来控制GIF背景。
983
1015
  """
984
1016
  params = {}
985
- body = {}
1017
+ data = {}
1018
+ files = {}
986
1019
 
987
1020
  if "_t" in kwargs:
988
1021
  params["_t"] = kwargs["_t"]
989
1022
 
990
1023
  if "bg_color" in kwargs:
991
- body["bg_color"] = kwargs["bg_color"]
1024
+ data["bg_color"] = _stringify_form_value(kwargs["bg_color"])
992
1025
 
993
1026
  if "file" in kwargs:
994
- body["file"] = kwargs["file"]
1027
+ files["file"] = _prepare_file_value(kwargs["file"])
995
1028
 
996
1029
  if "image_url" in kwargs:
997
- body["image_url"] = kwargs["image_url"]
1030
+ data["image_url"] = _stringify_form_value(kwargs["image_url"])
998
1031
 
999
1032
  disable_cache = kwargs.get("disable_cache")
1000
1033
  if disable_cache is None and "disableCache" in kwargs:
@@ -1005,7 +1038,8 @@ class _ImageApi:
1005
1038
  "POST",
1006
1039
  path,
1007
1040
  params=params,
1008
- json=body if body else None,
1041
+ data=data or None,
1042
+ files=files or None,
1009
1043
  disable_cache=_coerce_optional_bool(disable_cache),
1010
1044
  )
1011
1045
 
@@ -1033,16 +1067,17 @@ class _ImageApi:
1033
1067
  - **inference_time_ms**: 模型推理耗时,单位毫秒
1034
1068
  """
1035
1069
  params = {}
1036
- body = {}
1070
+ data = {}
1071
+ files = {}
1037
1072
 
1038
1073
  if "_t" in kwargs:
1039
1074
  params["_t"] = kwargs["_t"]
1040
1075
 
1041
1076
  if "file" in kwargs:
1042
- body["file"] = kwargs["file"]
1077
+ files["file"] = _prepare_file_value(kwargs["file"])
1043
1078
 
1044
1079
  if "url" in kwargs:
1045
- body["url"] = kwargs["url"]
1080
+ data["url"] = _stringify_form_value(kwargs["url"])
1046
1081
 
1047
1082
  disable_cache = kwargs.get("disable_cache")
1048
1083
  if disable_cache is None and "disableCache" in kwargs:
@@ -1053,7 +1088,8 @@ class _ImageApi:
1053
1088
  "POST",
1054
1089
  path,
1055
1090
  params=params,
1056
- json=body if body else None,
1091
+ data=data or None,
1092
+ files=files or None,
1057
1093
  disable_cache=_coerce_optional_bool(disable_cache),
1058
1094
  )
1059
1095
 
@@ -1071,31 +1107,32 @@ class _ImageApi:
1071
1107
  - **灵活的输入与请求要求**:接口支持 `file`、`url` 或 `image_base64` 三种方式输入。请确保请求格式为 `multipart/form-data`,且图片链接在公网可直接访问。
1072
1108
  """
1073
1109
  params = {}
1074
- body = {}
1110
+ data = {}
1111
+ files = {}
1075
1112
 
1076
1113
  if "_t" in kwargs:
1077
1114
  params["_t"] = kwargs["_t"]
1078
1115
 
1079
1116
  if "enable_cls" in kwargs:
1080
- body["enable_cls"] = kwargs["enable_cls"]
1117
+ data["enable_cls"] = _stringify_form_value(kwargs["enable_cls"])
1081
1118
 
1082
1119
  if "file" in kwargs:
1083
- body["file"] = kwargs["file"]
1120
+ files["file"] = _prepare_file_value(kwargs["file"])
1084
1121
 
1085
1122
  if "image_base64" in kwargs:
1086
- body["image_base64"] = kwargs["image_base64"]
1123
+ data["image_base64"] = _stringify_form_value(kwargs["image_base64"])
1087
1124
 
1088
1125
  if "image_name" in kwargs:
1089
- body["image_name"] = kwargs["image_name"]
1126
+ data["image_name"] = _stringify_form_value(kwargs["image_name"])
1090
1127
 
1091
1128
  if "need_location" in kwargs:
1092
- body["need_location"] = kwargs["need_location"]
1129
+ data["need_location"] = _stringify_form_value(kwargs["need_location"])
1093
1130
 
1094
1131
  if "return_markdown" in kwargs:
1095
- body["return_markdown"] = kwargs["return_markdown"]
1132
+ data["return_markdown"] = _stringify_form_value(kwargs["return_markdown"])
1096
1133
 
1097
1134
  if "url" in kwargs:
1098
- body["url"] = kwargs["url"]
1135
+ data["url"] = _stringify_form_value(kwargs["url"])
1099
1136
 
1100
1137
  disable_cache = kwargs.get("disable_cache")
1101
1138
  if disable_cache is None and "disableCache" in kwargs:
@@ -1106,7 +1143,8 @@ class _ImageApi:
1106
1143
  "POST",
1107
1144
  path,
1108
1145
  params=params,
1109
- json=body if body else None,
1146
+ data=data or None,
1147
+ files=files or None,
1110
1148
  disable_cache=_coerce_optional_bool(disable_cache),
1111
1149
  )
1112
1150
 
@@ -1152,7 +1190,8 @@ class _ImageApi:
1152
1190
  上传一个 SVG 文件,并指定目标格式(如 PNG、JPEG 等),接口将返回转换后的图像。你还可以调整输出图像的尺寸和(对于JPEG)压缩质量,以满足不同场景的需求。
1153
1191
  """
1154
1192
  params = {}
1155
- body = {}
1193
+ data = {}
1194
+ files = {}
1156
1195
 
1157
1196
  if "query" == "query" and "format" in kwargs:
1158
1197
  params["format"] = kwargs["format"]
@@ -1170,7 +1209,7 @@ class _ImageApi:
1170
1209
  params["_t"] = kwargs["_t"]
1171
1210
 
1172
1211
  if "file" in kwargs:
1173
- body["file"] = kwargs["file"]
1212
+ files["file"] = _prepare_file_value(kwargs["file"])
1174
1213
 
1175
1214
  disable_cache = kwargs.get("disable_cache")
1176
1215
  if disable_cache is None and "disableCache" in kwargs:
@@ -1181,7 +1220,8 @@ class _ImageApi:
1181
1220
  "POST",
1182
1221
  path,
1183
1222
  params=params,
1184
- json=body if body else None,
1223
+ data=data or None,
1224
+ files=files or None,
1185
1225
  disable_cache=_coerce_optional_bool(disable_cache),
1186
1226
  )
1187
1227
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: uapi-sdk-python
3
- Version: 0.1.16
3
+ Version: 0.1.17
4
4
  Summary: Idiomatic UAPI SDK for Python
5
5
  Author-email: UAPI <dev@uapis.cn>
6
6
  Requires-Python: >=3.9
@@ -0,0 +1,7 @@
1
+ uapi/__init__.py,sha256=3dluUZfyWYYGTWzDBFip0d5YZej6waZiq2ayvJ0RI9o,86
2
+ uapi/client.py,sha256=2MijKxsm656POod6_GZTS1fyD_7ZJdGWyjBXDanx6JY,152431
3
+ uapi/errors.py,sha256=ZmB8xwqED689r4R5x4zSF7rMqZ9vUU8bwV5j_rxyHN4,11067
4
+ uapi_sdk_python-0.1.17.dist-info/METADATA,sha256=pYu7jYBrvFPt6A5yVXiFyHUrP1JlLUh33ehb1Fray1Y,5848
5
+ uapi_sdk_python-0.1.17.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
6
+ uapi_sdk_python-0.1.17.dist-info/top_level.txt,sha256=0TrPOLLMqin88fxqxR9T5piS4dSoyju9HFiPu1oPEUU,5
7
+ uapi_sdk_python-0.1.17.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- uapi/__init__.py,sha256=3dluUZfyWYYGTWzDBFip0d5YZej6waZiq2ayvJ0RI9o,86
2
- uapi/client.py,sha256=KR_cJ0yDBxBksIJSX61HYYMW5w50RzG6MCWZMB7znY8,150805
3
- uapi/errors.py,sha256=ZmB8xwqED689r4R5x4zSF7rMqZ9vUU8bwV5j_rxyHN4,11067
4
- uapi_sdk_python-0.1.16.dist-info/METADATA,sha256=cgnYPUmus-QhqTxYH_v-F5nkztMRs3geDikB5kO77uU,5848
5
- uapi_sdk_python-0.1.16.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
6
- uapi_sdk_python-0.1.16.dist-info/top_level.txt,sha256=0TrPOLLMqin88fxqxR9T5piS4dSoyju9HFiPu1oPEUU,5
7
- uapi_sdk_python-0.1.16.dist-info/RECORD,,