dxpos-openapi 0.0.1__tar.gz
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.
- dxpos_openapi-0.0.1/LICENSE +21 -0
- dxpos_openapi-0.0.1/PKG-INFO +199 -0
- dxpos_openapi-0.0.1/README.md +142 -0
- dxpos_openapi-0.0.1/pyproject.toml +54 -0
- dxpos_openapi-0.0.1/setup.cfg +4 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi/__init__.py +6 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi/auth.py +42 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi/client.py +174 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi/constants.py +28 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi.egg-info/PKG-INFO +199 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi.egg-info/SOURCES.txt +14 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi.egg-info/dependency_links.txt +1 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi.egg-info/requires.txt +12 -0
- dxpos_openapi-0.0.1/src/dxpos_openapi.egg-info/top_level.txt +1 -0
- dxpos_openapi-0.0.1/tests/test_auth.py +51 -0
- dxpos_openapi-0.0.1/tests/test_client.py +297 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DXPOS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: dxpos-openapi
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: DXPOS OpenAPI Python SDK
|
|
5
|
+
Author-email: DXPOS <support@dxpos.co>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 DXPOS
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://openapi.dxpos.co
|
|
29
|
+
Project-URL: Documentation, https://openapi.dxpos.co
|
|
30
|
+
Keywords: dxpos,openapi,sdk
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
42
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
43
|
+
Requires-Python: >=3.8.16
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
License-File: LICENSE
|
|
46
|
+
Requires-Dist: requests==2.31.0
|
|
47
|
+
Requires-Dist: urllib3==1.26.20
|
|
48
|
+
Requires-Dist: certifi==2024.8.30
|
|
49
|
+
Requires-Dist: charset-normalizer==3.3.2
|
|
50
|
+
Requires-Dist: idna==3.7
|
|
51
|
+
Provides-Extra: dev
|
|
52
|
+
Requires-Dist: pytest==8.3.5; extra == "dev"
|
|
53
|
+
Requires-Dist: build==1.2.2; extra == "dev"
|
|
54
|
+
Requires-Dist: twine==5.1.1; extra == "dev"
|
|
55
|
+
Requires-Dist: setuptools==75.3.0; extra == "dev"
|
|
56
|
+
Requires-Dist: wheel==0.45.1; extra == "dev"
|
|
57
|
+
|
|
58
|
+
# dxpos-openapi
|
|
59
|
+
|
|
60
|
+
DXPOS OpenAPI 的 Python SDK。安装后:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from dxpos_openapi import DxposClient
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
需要 Python 3.8.16 及以上。
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install dxpos-openapi
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
升级:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install -U dxpos-openapi
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 初始化
|
|
81
|
+
|
|
82
|
+
密钥和域名在构造 `DxposClient` 时传入,SDK 不读环境变量。`base_url` 默认生产域名。
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from dxpos_openapi import DxposClient
|
|
86
|
+
|
|
87
|
+
client = DxposClient(
|
|
88
|
+
access_key="你的 Access Key",
|
|
89
|
+
secret_key="你的 Secret Key",
|
|
90
|
+
# base_url="https://openapi-test.dxpos.co", # 测试环境
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| 环境 | base_url |
|
|
95
|
+
|------|----------|
|
|
96
|
+
| 生产(默认) | `https://openapi.dxpos.co` |
|
|
97
|
+
| 测试 | `https://openapi-test.dxpos.co` |
|
|
98
|
+
|
|
99
|
+
五个方法会自动做 HMAC 签名,不用自己算。
|
|
100
|
+
|
|
101
|
+
## 返回值
|
|
102
|
+
|
|
103
|
+
每个方法都返回接口原样字典,业务失败也不抛异常:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
{
|
|
107
|
+
"code": 0,
|
|
108
|
+
"msg": "",
|
|
109
|
+
"data": {},
|
|
110
|
+
"request_id": "req_..."
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`code == 0` 为成功,读 `data`。本地文件不存在、格式不支持、COS 上传失败、网络错误等会合成 `code == -1`。
|
|
115
|
+
|
|
116
|
+
## 方法
|
|
117
|
+
|
|
118
|
+
### 主题列表
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
resp = client.list_themes()
|
|
122
|
+
theme_id = resp["data"]["items"][0]["theme_id"]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 上传本地图片
|
|
126
|
+
|
|
127
|
+
内部会预创建文件并 PUT 到 COS。成功时返回预创建接口的原样 JSON,`data` 里有 `file_id`。
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
resp = client.upload_file("/path/to/product.jpg")
|
|
131
|
+
file_id = resp["data"]["file_id"]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
仅支持 `.jpg` / `.jpeg` / `.png` / `.webp`。
|
|
135
|
+
|
|
136
|
+
### 创建场景增强任务
|
|
137
|
+
|
|
138
|
+
`file_id`、`image_path`、`image_url` 必须三选一。
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
# 已有公网 URL
|
|
142
|
+
resp = client.create_scene_enhancement(
|
|
143
|
+
theme_id=theme_id,
|
|
144
|
+
image_url="https://example.com/product.jpg",
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# 已经 upload_file 拿到 file_id
|
|
148
|
+
resp = client.create_scene_enhancement(theme_id=theme_id, file_id=file_id)
|
|
149
|
+
|
|
150
|
+
# 本地路径:自动上传再建任务
|
|
151
|
+
resp = client.create_scene_enhancement(
|
|
152
|
+
theme_id=theme_id,
|
|
153
|
+
image_path="/path/to/product.jpg",
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
task_id = resp["data"]["task_id"]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 查询任务
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
resp = client.get_task(task_id)
|
|
163
|
+
# data.status: waiting / processing / done / failed / canceled
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 取消任务
|
|
167
|
+
|
|
168
|
+
仅排队中(`waiting`)可取消。
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
resp = client.cancel_task(task_id)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 维护者:发布到 PyPI
|
|
175
|
+
|
|
176
|
+
不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
|
|
177
|
+
|
|
178
|
+
开发依赖:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
pip install -e ".[dev]"
|
|
182
|
+
pytest
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
首次 `0.0.1`:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
rm -rf dist build src/*.egg-info
|
|
189
|
+
python -m build
|
|
190
|
+
twine check dist/*
|
|
191
|
+
twine upload -r testpypi dist/*
|
|
192
|
+
# 干净环境验证:
|
|
193
|
+
# pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.1
|
|
194
|
+
twine upload dist/*
|
|
195
|
+
git tag v0.0.1
|
|
196
|
+
git push origin main --tags
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
以后更新:同步改 `pyproject.toml` 的 `version` 和 `src/dxpos_openapi/__init__.py` 的 `__version__`,再走一遍 build / twine / tag。同一版本号不能覆盖重传。小改用 `0.0.x`,加方法或改入参用 `0.1.0`。
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# dxpos-openapi
|
|
2
|
+
|
|
3
|
+
DXPOS OpenAPI 的 Python SDK。安装后:
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from dxpos_openapi import DxposClient
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
需要 Python 3.8.16 及以上。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install dxpos-openapi
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
升级:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install -U dxpos-openapi
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 初始化
|
|
24
|
+
|
|
25
|
+
密钥和域名在构造 `DxposClient` 时传入,SDK 不读环境变量。`base_url` 默认生产域名。
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from dxpos_openapi import DxposClient
|
|
29
|
+
|
|
30
|
+
client = DxposClient(
|
|
31
|
+
access_key="你的 Access Key",
|
|
32
|
+
secret_key="你的 Secret Key",
|
|
33
|
+
# base_url="https://openapi-test.dxpos.co", # 测试环境
|
|
34
|
+
)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| 环境 | base_url |
|
|
38
|
+
|------|----------|
|
|
39
|
+
| 生产(默认) | `https://openapi.dxpos.co` |
|
|
40
|
+
| 测试 | `https://openapi-test.dxpos.co` |
|
|
41
|
+
|
|
42
|
+
五个方法会自动做 HMAC 签名,不用自己算。
|
|
43
|
+
|
|
44
|
+
## 返回值
|
|
45
|
+
|
|
46
|
+
每个方法都返回接口原样字典,业务失败也不抛异常:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
{
|
|
50
|
+
"code": 0,
|
|
51
|
+
"msg": "",
|
|
52
|
+
"data": {},
|
|
53
|
+
"request_id": "req_..."
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`code == 0` 为成功,读 `data`。本地文件不存在、格式不支持、COS 上传失败、网络错误等会合成 `code == -1`。
|
|
58
|
+
|
|
59
|
+
## 方法
|
|
60
|
+
|
|
61
|
+
### 主题列表
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
resp = client.list_themes()
|
|
65
|
+
theme_id = resp["data"]["items"][0]["theme_id"]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 上传本地图片
|
|
69
|
+
|
|
70
|
+
内部会预创建文件并 PUT 到 COS。成功时返回预创建接口的原样 JSON,`data` 里有 `file_id`。
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
resp = client.upload_file("/path/to/product.jpg")
|
|
74
|
+
file_id = resp["data"]["file_id"]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
仅支持 `.jpg` / `.jpeg` / `.png` / `.webp`。
|
|
78
|
+
|
|
79
|
+
### 创建场景增强任务
|
|
80
|
+
|
|
81
|
+
`file_id`、`image_path`、`image_url` 必须三选一。
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
# 已有公网 URL
|
|
85
|
+
resp = client.create_scene_enhancement(
|
|
86
|
+
theme_id=theme_id,
|
|
87
|
+
image_url="https://example.com/product.jpg",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# 已经 upload_file 拿到 file_id
|
|
91
|
+
resp = client.create_scene_enhancement(theme_id=theme_id, file_id=file_id)
|
|
92
|
+
|
|
93
|
+
# 本地路径:自动上传再建任务
|
|
94
|
+
resp = client.create_scene_enhancement(
|
|
95
|
+
theme_id=theme_id,
|
|
96
|
+
image_path="/path/to/product.jpg",
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
task_id = resp["data"]["task_id"]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 查询任务
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
resp = client.get_task(task_id)
|
|
106
|
+
# data.status: waiting / processing / done / failed / canceled
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 取消任务
|
|
110
|
+
|
|
111
|
+
仅排队中(`waiting`)可取消。
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
resp = client.cancel_task(task_id)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 维护者:发布到 PyPI
|
|
118
|
+
|
|
119
|
+
不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
|
|
120
|
+
|
|
121
|
+
开发依赖:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
pip install -e ".[dev]"
|
|
125
|
+
pytest
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
首次 `0.0.1`:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
rm -rf dist build src/*.egg-info
|
|
132
|
+
python -m build
|
|
133
|
+
twine check dist/*
|
|
134
|
+
twine upload -r testpypi dist/*
|
|
135
|
+
# 干净环境验证:
|
|
136
|
+
# pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.1
|
|
137
|
+
twine upload dist/*
|
|
138
|
+
git tag v0.0.1
|
|
139
|
+
git push origin main --tags
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
以后更新:同步改 `pyproject.toml` 的 `version` 和 `src/dxpos_openapi/__init__.py` 的 `__version__`,再走一遍 build / twine / tag。同一版本号不能覆盖重传。小改用 `0.0.x`,加方法或改入参用 `0.1.0`。
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools==75.3.0", "wheel==0.45.1"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dxpos-openapi"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "DXPOS OpenAPI Python SDK"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {file = "LICENSE"}
|
|
11
|
+
requires-python = ">=3.8.16"
|
|
12
|
+
authors = [{name = "DXPOS", email = "support@dxpos.co"}]
|
|
13
|
+
keywords = ["dxpos", "openapi", "sdk"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.8",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"requests==2.31.0",
|
|
30
|
+
"urllib3==1.26.20",
|
|
31
|
+
"certifi==2024.8.30",
|
|
32
|
+
"charset-normalizer==3.3.2",
|
|
33
|
+
"idna==3.7",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
dev = [
|
|
38
|
+
"pytest==8.3.5",
|
|
39
|
+
"build==1.2.2",
|
|
40
|
+
"twine==5.1.1",
|
|
41
|
+
"setuptools==75.3.0",
|
|
42
|
+
"wheel==0.45.1",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://openapi.dxpos.co"
|
|
47
|
+
Documentation = "https://openapi.dxpos.co"
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
where = ["src"]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
testpaths = ["tests"]
|
|
54
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
import time
|
|
4
|
+
import uuid
|
|
5
|
+
|
|
6
|
+
from dxpos_openapi.constants import (
|
|
7
|
+
HEADER_ACCESS_KEY,
|
|
8
|
+
HEADER_NONCE,
|
|
9
|
+
HEADER_SIGNATURE,
|
|
10
|
+
HEADER_TIMESTAMP,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def body_sha256_hex(body):
|
|
15
|
+
return hashlib.sha256(body or b"").hexdigest()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_sign_str(method, path, timestamp, nonce, body):
|
|
19
|
+
return "{}\n{}\n{}\n{}\n{}".format(
|
|
20
|
+
method,
|
|
21
|
+
path,
|
|
22
|
+
timestamp,
|
|
23
|
+
nonce,
|
|
24
|
+
body_sha256_hex(body),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def sign_headers(access_key, secret_key, method, path, body=b""):
|
|
29
|
+
timestamp = str(int(time.time()))
|
|
30
|
+
nonce = uuid.uuid4().hex
|
|
31
|
+
raw = build_sign_str(method, path, timestamp, nonce, body)
|
|
32
|
+
signature = hmac.new(
|
|
33
|
+
secret_key.encode("utf-8"),
|
|
34
|
+
raw.encode("utf-8"),
|
|
35
|
+
hashlib.sha256,
|
|
36
|
+
).hexdigest()
|
|
37
|
+
return {
|
|
38
|
+
HEADER_ACCESS_KEY: access_key,
|
|
39
|
+
HEADER_TIMESTAMP: timestamp,
|
|
40
|
+
HEADER_NONCE: nonce,
|
|
41
|
+
HEADER_SIGNATURE: signature,
|
|
42
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import requests
|
|
5
|
+
|
|
6
|
+
from dxpos_openapi.auth import sign_headers
|
|
7
|
+
from dxpos_openapi.constants import (
|
|
8
|
+
DEFAULT_BASE_URL,
|
|
9
|
+
IMAGE_TYPE_FILE_ID,
|
|
10
|
+
IMAGE_TYPE_URL,
|
|
11
|
+
MIME_BY_SUFFIX,
|
|
12
|
+
PATH_FILE,
|
|
13
|
+
PATH_SCENE_ENHANCEMENT,
|
|
14
|
+
PATH_THEME,
|
|
15
|
+
path_cancel,
|
|
16
|
+
path_task,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _fail(msg, request_id=""):
|
|
21
|
+
return {"code": -1, "msg": msg, "data": {}, "request_id": request_id}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _encode_json(payload):
|
|
25
|
+
return json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DxposClient(object):
|
|
29
|
+
def __init__(self, access_key, secret_key, base_url=DEFAULT_BASE_URL, timeout=30):
|
|
30
|
+
if not access_key or not secret_key:
|
|
31
|
+
raise ValueError("access_key 和 secret_key 不能为空")
|
|
32
|
+
self.access_key = access_key
|
|
33
|
+
self.secret_key = secret_key
|
|
34
|
+
self.base_url = base_url.rstrip("/")
|
|
35
|
+
self.timeout = timeout
|
|
36
|
+
self._session = requests.Session()
|
|
37
|
+
|
|
38
|
+
def list_themes(self):
|
|
39
|
+
return self._request("GET", PATH_THEME)
|
|
40
|
+
|
|
41
|
+
def upload_file(self, image_path):
|
|
42
|
+
loaded = self._read_local_image(image_path)
|
|
43
|
+
if loaded["error"] is not None:
|
|
44
|
+
return loaded["error"]
|
|
45
|
+
created = self._request(
|
|
46
|
+
"POST",
|
|
47
|
+
PATH_FILE,
|
|
48
|
+
{
|
|
49
|
+
"size": loaded["size"],
|
|
50
|
+
"content_type": loaded["content_type"],
|
|
51
|
+
"filename": loaded["filename"],
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
if created.get("code") != 0:
|
|
55
|
+
return created
|
|
56
|
+
put_url = (created.get("data") or {}).get("put_url")
|
|
57
|
+
if not put_url:
|
|
58
|
+
return _fail("预创建成功但缺少 put_url")
|
|
59
|
+
put_error = self._put_cos(
|
|
60
|
+
put_url,
|
|
61
|
+
loaded["raw"],
|
|
62
|
+
loaded["content_type"],
|
|
63
|
+
loaded["size"],
|
|
64
|
+
)
|
|
65
|
+
if put_error is not None:
|
|
66
|
+
return put_error
|
|
67
|
+
return created
|
|
68
|
+
|
|
69
|
+
def create_scene_enhancement(self, theme_id, file_id=None, image_path=None, image_url=None):
|
|
70
|
+
picked = [item for item in (file_id, image_path, image_url) if item]
|
|
71
|
+
if len(picked) != 1:
|
|
72
|
+
return _fail("file_id、image_path、image_url 必须三选一")
|
|
73
|
+
if image_url:
|
|
74
|
+
return self._request(
|
|
75
|
+
"POST",
|
|
76
|
+
PATH_SCENE_ENHANCEMENT,
|
|
77
|
+
{
|
|
78
|
+
"theme_id": theme_id,
|
|
79
|
+
"image": {"type": IMAGE_TYPE_URL, "url": image_url},
|
|
80
|
+
},
|
|
81
|
+
)
|
|
82
|
+
if file_id:
|
|
83
|
+
return self._request(
|
|
84
|
+
"POST",
|
|
85
|
+
PATH_SCENE_ENHANCEMENT,
|
|
86
|
+
{
|
|
87
|
+
"theme_id": theme_id,
|
|
88
|
+
"image": {"type": IMAGE_TYPE_FILE_ID, "file_id": file_id},
|
|
89
|
+
},
|
|
90
|
+
)
|
|
91
|
+
uploaded = self.upload_file(image_path)
|
|
92
|
+
if uploaded.get("code") != 0:
|
|
93
|
+
return uploaded
|
|
94
|
+
new_file_id = (uploaded.get("data") or {}).get("file_id")
|
|
95
|
+
if not new_file_id:
|
|
96
|
+
return _fail("上传成功但缺少 file_id")
|
|
97
|
+
return self._request(
|
|
98
|
+
"POST",
|
|
99
|
+
PATH_SCENE_ENHANCEMENT,
|
|
100
|
+
{
|
|
101
|
+
"theme_id": theme_id,
|
|
102
|
+
"image": {"type": IMAGE_TYPE_FILE_ID, "file_id": new_file_id},
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def get_task(self, task_id):
|
|
107
|
+
return self._request("GET", path_task(task_id))
|
|
108
|
+
|
|
109
|
+
def cancel_task(self, task_id):
|
|
110
|
+
return self._request("POST", path_cancel(task_id))
|
|
111
|
+
|
|
112
|
+
def _request(self, method, path, payload=None):
|
|
113
|
+
body = b"" if payload is None else _encode_json(payload)
|
|
114
|
+
headers = sign_headers(self.access_key, self.secret_key, method, path, body)
|
|
115
|
+
if payload is not None:
|
|
116
|
+
headers["Content-Type"] = "application/json"
|
|
117
|
+
try:
|
|
118
|
+
resp = self._session.request(
|
|
119
|
+
method,
|
|
120
|
+
self.base_url + path,
|
|
121
|
+
data=body,
|
|
122
|
+
headers=headers,
|
|
123
|
+
timeout=self.timeout,
|
|
124
|
+
)
|
|
125
|
+
except requests.RequestException as exc:
|
|
126
|
+
return _fail("请求失败: {}".format(exc))
|
|
127
|
+
try:
|
|
128
|
+
parsed = resp.json()
|
|
129
|
+
except ValueError:
|
|
130
|
+
return _fail("响应不是 JSON: HTTP {}".format(resp.status_code))
|
|
131
|
+
if not isinstance(parsed, dict):
|
|
132
|
+
return _fail("响应格式不正确")
|
|
133
|
+
return parsed
|
|
134
|
+
|
|
135
|
+
def _put_cos(self, put_url, raw, content_type, size):
|
|
136
|
+
try:
|
|
137
|
+
resp = self._session.put(
|
|
138
|
+
put_url,
|
|
139
|
+
data=raw,
|
|
140
|
+
headers={
|
|
141
|
+
"Content-Type": content_type,
|
|
142
|
+
"Content-Length": str(size),
|
|
143
|
+
},
|
|
144
|
+
timeout=self.timeout,
|
|
145
|
+
)
|
|
146
|
+
except requests.RequestException as exc:
|
|
147
|
+
return _fail("上传失败: {}".format(exc))
|
|
148
|
+
if resp.status_code not in (200, 204):
|
|
149
|
+
return _fail("上传失败: HTTP {}".format(resp.status_code))
|
|
150
|
+
return None
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def _read_local_image(image_path):
|
|
154
|
+
if not image_path or not os.path.isfile(image_path):
|
|
155
|
+
return {"error": _fail("本地文件不存在")}
|
|
156
|
+
content_type = MIME_BY_SUFFIX.get(os.path.splitext(image_path)[1].lower())
|
|
157
|
+
if content_type is None:
|
|
158
|
+
return {
|
|
159
|
+
"error": _fail("图片格式不支持,仅支持 image/jpeg、image/png、image/webp")
|
|
160
|
+
}
|
|
161
|
+
try:
|
|
162
|
+
with open(image_path, "rb") as handle:
|
|
163
|
+
raw = handle.read()
|
|
164
|
+
except OSError as exc:
|
|
165
|
+
return {"error": _fail("读取本地文件失败: {}".format(exc))}
|
|
166
|
+
if not raw:
|
|
167
|
+
return {"error": _fail("本地文件为空")}
|
|
168
|
+
return {
|
|
169
|
+
"error": None,
|
|
170
|
+
"raw": raw,
|
|
171
|
+
"content_type": content_type,
|
|
172
|
+
"filename": os.path.basename(image_path),
|
|
173
|
+
"size": len(raw),
|
|
174
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
DEFAULT_BASE_URL = "https://openapi.dxpos.co"
|
|
2
|
+
|
|
3
|
+
PATH_THEME = "/api/v1/theme"
|
|
4
|
+
PATH_FILE = "/api/v1/file"
|
|
5
|
+
PATH_SCENE_ENHANCEMENT = "/api/v1/task/scene_enhancement"
|
|
6
|
+
|
|
7
|
+
HEADER_ACCESS_KEY = "X-Access-Key"
|
|
8
|
+
HEADER_TIMESTAMP = "X-Timestamp"
|
|
9
|
+
HEADER_NONCE = "X-Nonce"
|
|
10
|
+
HEADER_SIGNATURE = "X-Signature"
|
|
11
|
+
|
|
12
|
+
IMAGE_TYPE_FILE_ID = "file_id"
|
|
13
|
+
IMAGE_TYPE_URL = "url"
|
|
14
|
+
|
|
15
|
+
MIME_BY_SUFFIX = {
|
|
16
|
+
".jpg": "image/jpeg",
|
|
17
|
+
".jpeg": "image/jpeg",
|
|
18
|
+
".png": "image/png",
|
|
19
|
+
".webp": "image/webp",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def path_task(task_id):
|
|
24
|
+
return "/api/v1/task/{}".format(task_id)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def path_cancel(task_id):
|
|
28
|
+
return "/api/v1/task/{}/cancel".format(task_id)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: dxpos-openapi
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: DXPOS OpenAPI Python SDK
|
|
5
|
+
Author-email: DXPOS <support@dxpos.co>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 DXPOS
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://openapi.dxpos.co
|
|
29
|
+
Project-URL: Documentation, https://openapi.dxpos.co
|
|
30
|
+
Keywords: dxpos,openapi,sdk
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
42
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
43
|
+
Requires-Python: >=3.8.16
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
License-File: LICENSE
|
|
46
|
+
Requires-Dist: requests==2.31.0
|
|
47
|
+
Requires-Dist: urllib3==1.26.20
|
|
48
|
+
Requires-Dist: certifi==2024.8.30
|
|
49
|
+
Requires-Dist: charset-normalizer==3.3.2
|
|
50
|
+
Requires-Dist: idna==3.7
|
|
51
|
+
Provides-Extra: dev
|
|
52
|
+
Requires-Dist: pytest==8.3.5; extra == "dev"
|
|
53
|
+
Requires-Dist: build==1.2.2; extra == "dev"
|
|
54
|
+
Requires-Dist: twine==5.1.1; extra == "dev"
|
|
55
|
+
Requires-Dist: setuptools==75.3.0; extra == "dev"
|
|
56
|
+
Requires-Dist: wheel==0.45.1; extra == "dev"
|
|
57
|
+
|
|
58
|
+
# dxpos-openapi
|
|
59
|
+
|
|
60
|
+
DXPOS OpenAPI 的 Python SDK。安装后:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from dxpos_openapi import DxposClient
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
需要 Python 3.8.16 及以上。
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install dxpos-openapi
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
升级:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install -U dxpos-openapi
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 初始化
|
|
81
|
+
|
|
82
|
+
密钥和域名在构造 `DxposClient` 时传入,SDK 不读环境变量。`base_url` 默认生产域名。
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from dxpos_openapi import DxposClient
|
|
86
|
+
|
|
87
|
+
client = DxposClient(
|
|
88
|
+
access_key="你的 Access Key",
|
|
89
|
+
secret_key="你的 Secret Key",
|
|
90
|
+
# base_url="https://openapi-test.dxpos.co", # 测试环境
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| 环境 | base_url |
|
|
95
|
+
|------|----------|
|
|
96
|
+
| 生产(默认) | `https://openapi.dxpos.co` |
|
|
97
|
+
| 测试 | `https://openapi-test.dxpos.co` |
|
|
98
|
+
|
|
99
|
+
五个方法会自动做 HMAC 签名,不用自己算。
|
|
100
|
+
|
|
101
|
+
## 返回值
|
|
102
|
+
|
|
103
|
+
每个方法都返回接口原样字典,业务失败也不抛异常:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
{
|
|
107
|
+
"code": 0,
|
|
108
|
+
"msg": "",
|
|
109
|
+
"data": {},
|
|
110
|
+
"request_id": "req_..."
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`code == 0` 为成功,读 `data`。本地文件不存在、格式不支持、COS 上传失败、网络错误等会合成 `code == -1`。
|
|
115
|
+
|
|
116
|
+
## 方法
|
|
117
|
+
|
|
118
|
+
### 主题列表
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
resp = client.list_themes()
|
|
122
|
+
theme_id = resp["data"]["items"][0]["theme_id"]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 上传本地图片
|
|
126
|
+
|
|
127
|
+
内部会预创建文件并 PUT 到 COS。成功时返回预创建接口的原样 JSON,`data` 里有 `file_id`。
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
resp = client.upload_file("/path/to/product.jpg")
|
|
131
|
+
file_id = resp["data"]["file_id"]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
仅支持 `.jpg` / `.jpeg` / `.png` / `.webp`。
|
|
135
|
+
|
|
136
|
+
### 创建场景增强任务
|
|
137
|
+
|
|
138
|
+
`file_id`、`image_path`、`image_url` 必须三选一。
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
# 已有公网 URL
|
|
142
|
+
resp = client.create_scene_enhancement(
|
|
143
|
+
theme_id=theme_id,
|
|
144
|
+
image_url="https://example.com/product.jpg",
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# 已经 upload_file 拿到 file_id
|
|
148
|
+
resp = client.create_scene_enhancement(theme_id=theme_id, file_id=file_id)
|
|
149
|
+
|
|
150
|
+
# 本地路径:自动上传再建任务
|
|
151
|
+
resp = client.create_scene_enhancement(
|
|
152
|
+
theme_id=theme_id,
|
|
153
|
+
image_path="/path/to/product.jpg",
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
task_id = resp["data"]["task_id"]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 查询任务
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
resp = client.get_task(task_id)
|
|
163
|
+
# data.status: waiting / processing / done / failed / canceled
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 取消任务
|
|
167
|
+
|
|
168
|
+
仅排队中(`waiting`)可取消。
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
resp = client.cancel_task(task_id)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 维护者:发布到 PyPI
|
|
175
|
+
|
|
176
|
+
不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
|
|
177
|
+
|
|
178
|
+
开发依赖:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
pip install -e ".[dev]"
|
|
182
|
+
pytest
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
首次 `0.0.1`:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
rm -rf dist build src/*.egg-info
|
|
189
|
+
python -m build
|
|
190
|
+
twine check dist/*
|
|
191
|
+
twine upload -r testpypi dist/*
|
|
192
|
+
# 干净环境验证:
|
|
193
|
+
# pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.1
|
|
194
|
+
twine upload dist/*
|
|
195
|
+
git tag v0.0.1
|
|
196
|
+
git push origin main --tags
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
以后更新:同步改 `pyproject.toml` 的 `version` 和 `src/dxpos_openapi/__init__.py` 的 `__version__`,再走一遍 build / twine / tag。同一版本号不能覆盖重传。小改用 `0.0.x`,加方法或改入参用 `0.1.0`。
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/dxpos_openapi/__init__.py
|
|
5
|
+
src/dxpos_openapi/auth.py
|
|
6
|
+
src/dxpos_openapi/client.py
|
|
7
|
+
src/dxpos_openapi/constants.py
|
|
8
|
+
src/dxpos_openapi.egg-info/PKG-INFO
|
|
9
|
+
src/dxpos_openapi.egg-info/SOURCES.txt
|
|
10
|
+
src/dxpos_openapi.egg-info/dependency_links.txt
|
|
11
|
+
src/dxpos_openapi.egg-info/requires.txt
|
|
12
|
+
src/dxpos_openapi.egg-info/top_level.txt
|
|
13
|
+
tests/test_auth.py
|
|
14
|
+
tests/test_client.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dxpos_openapi
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
|
|
4
|
+
from dxpos_openapi.auth import body_sha256_hex, build_sign_str, sign_headers
|
|
5
|
+
from dxpos_openapi.constants import (
|
|
6
|
+
HEADER_ACCESS_KEY,
|
|
7
|
+
HEADER_NONCE,
|
|
8
|
+
HEADER_SIGNATURE,
|
|
9
|
+
HEADER_TIMESTAMP,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_empty_body_sha256():
|
|
14
|
+
assert body_sha256_hex(b"") == hashlib.sha256(b"").hexdigest()
|
|
15
|
+
assert body_sha256_hex(None) == hashlib.sha256(b"").hexdigest()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_sign_str_five_lines():
|
|
19
|
+
body = b'{"size":1}'
|
|
20
|
+
raw = build_sign_str("POST", "/api/v1/file", "1700000000", "abc", body)
|
|
21
|
+
lines = raw.split("\n")
|
|
22
|
+
assert lines == [
|
|
23
|
+
"POST",
|
|
24
|
+
"/api/v1/file",
|
|
25
|
+
"1700000000",
|
|
26
|
+
"abc",
|
|
27
|
+
hashlib.sha256(body).hexdigest(),
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_sign_headers_hex_and_fields():
|
|
32
|
+
headers = sign_headers("ak", "sk", "GET", "/api/v1/theme", b"")
|
|
33
|
+
assert set(headers) == {
|
|
34
|
+
HEADER_ACCESS_KEY,
|
|
35
|
+
HEADER_TIMESTAMP,
|
|
36
|
+
HEADER_NONCE,
|
|
37
|
+
HEADER_SIGNATURE,
|
|
38
|
+
}
|
|
39
|
+
assert headers[HEADER_ACCESS_KEY] == "ak"
|
|
40
|
+
assert headers[HEADER_TIMESTAMP].isdigit()
|
|
41
|
+
assert headers[HEADER_NONCE]
|
|
42
|
+
assert headers[HEADER_SIGNATURE] == headers[HEADER_SIGNATURE].lower()
|
|
43
|
+
raw = build_sign_str(
|
|
44
|
+
"GET",
|
|
45
|
+
"/api/v1/theme",
|
|
46
|
+
headers[HEADER_TIMESTAMP],
|
|
47
|
+
headers[HEADER_NONCE],
|
|
48
|
+
b"",
|
|
49
|
+
)
|
|
50
|
+
expected = hmac.new(b"sk", raw.encode("utf-8"), hashlib.sha256).hexdigest()
|
|
51
|
+
assert headers[HEADER_SIGNATURE] == expected
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
import requests
|
|
7
|
+
|
|
8
|
+
from dxpos_openapi.auth import build_sign_str
|
|
9
|
+
from dxpos_openapi.client import DxposClient
|
|
10
|
+
from dxpos_openapi.constants import (
|
|
11
|
+
DEFAULT_BASE_URL,
|
|
12
|
+
HEADER_ACCESS_KEY,
|
|
13
|
+
HEADER_NONCE,
|
|
14
|
+
HEADER_SIGNATURE,
|
|
15
|
+
HEADER_TIMESTAMP,
|
|
16
|
+
PATH_FILE,
|
|
17
|
+
PATH_SCENE_ENHANCEMENT,
|
|
18
|
+
PATH_THEME,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class FakeResponse(object):
|
|
23
|
+
def __init__(self, payload=None, status_code=200, text=""):
|
|
24
|
+
self.status_code = status_code
|
|
25
|
+
self._payload = payload
|
|
26
|
+
self.text = text
|
|
27
|
+
|
|
28
|
+
def json(self):
|
|
29
|
+
if self._payload is None:
|
|
30
|
+
raise ValueError("not json")
|
|
31
|
+
return self._payload
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _ok(data):
|
|
35
|
+
return {"code": 0, "msg": "", "data": data, "request_id": "req_test"}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _client():
|
|
39
|
+
return DxposClient("ak", "sk", base_url="https://openapi.example")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _assert_signed(method, path, body, headers):
|
|
43
|
+
raw = build_sign_str(
|
|
44
|
+
method,
|
|
45
|
+
path,
|
|
46
|
+
headers[HEADER_TIMESTAMP],
|
|
47
|
+
headers[HEADER_NONCE],
|
|
48
|
+
body,
|
|
49
|
+
)
|
|
50
|
+
expected = hmac.new(b"sk", raw.encode("utf-8"), hashlib.sha256).hexdigest()
|
|
51
|
+
assert headers[HEADER_ACCESS_KEY] == "ak"
|
|
52
|
+
assert headers[HEADER_SIGNATURE] == expected
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_constructor_requires_keys():
|
|
56
|
+
with pytest.raises(ValueError):
|
|
57
|
+
DxposClient("", "sk")
|
|
58
|
+
with pytest.raises(ValueError):
|
|
59
|
+
DxposClient("ak", "")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_default_base_url():
|
|
63
|
+
client = DxposClient("ak", "sk")
|
|
64
|
+
assert client.base_url == DEFAULT_BASE_URL
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_list_themes(monkeypatch):
|
|
68
|
+
client = _client()
|
|
69
|
+
captured = {}
|
|
70
|
+
|
|
71
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
72
|
+
captured.update(
|
|
73
|
+
{"method": method, "url": url, "data": data, "headers": headers}
|
|
74
|
+
)
|
|
75
|
+
return FakeResponse(_ok({"items": [{"theme_id": "t1"}]}))
|
|
76
|
+
|
|
77
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
78
|
+
resp = client.list_themes()
|
|
79
|
+
assert resp["code"] == 0
|
|
80
|
+
assert resp["data"]["items"][0]["theme_id"] == "t1"
|
|
81
|
+
assert captured["method"] == "GET"
|
|
82
|
+
assert captured["url"] == "https://openapi.example" + PATH_THEME
|
|
83
|
+
assert captured["data"] == b""
|
|
84
|
+
_assert_signed("GET", PATH_THEME, b"", captured["headers"])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_get_and_cancel_task(monkeypatch):
|
|
88
|
+
client = _client()
|
|
89
|
+
calls = []
|
|
90
|
+
|
|
91
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
92
|
+
calls.append({"method": method, "url": url, "data": data, "headers": headers})
|
|
93
|
+
return FakeResponse(_ok({"task_id": "tid", "status": "waiting"}))
|
|
94
|
+
|
|
95
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
96
|
+
get_resp = client.get_task("tid")
|
|
97
|
+
cancel_resp = client.cancel_task("tid")
|
|
98
|
+
assert get_resp["code"] == 0
|
|
99
|
+
assert cancel_resp["code"] == 0
|
|
100
|
+
assert calls[0]["method"] == "GET"
|
|
101
|
+
assert calls[0]["url"].endswith("/api/v1/task/tid")
|
|
102
|
+
assert calls[0]["data"] == b""
|
|
103
|
+
_assert_signed("GET", "/api/v1/task/tid", b"", calls[0]["headers"])
|
|
104
|
+
assert calls[1]["method"] == "POST"
|
|
105
|
+
assert calls[1]["url"].endswith("/api/v1/task/tid/cancel")
|
|
106
|
+
assert calls[1]["data"] == b""
|
|
107
|
+
_assert_signed("POST", "/api/v1/task/tid/cancel", b"", calls[1]["headers"])
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_create_with_url_does_not_hit_file(monkeypatch):
|
|
111
|
+
client = _client()
|
|
112
|
+
calls = []
|
|
113
|
+
|
|
114
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
115
|
+
calls.append({"method": method, "url": url, "data": data, "headers": headers})
|
|
116
|
+
return FakeResponse(_ok({"task_id": "tid"}))
|
|
117
|
+
|
|
118
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
119
|
+
resp = client.create_scene_enhancement(
|
|
120
|
+
theme_id="theme1",
|
|
121
|
+
image_url="https://example.com/a.jpg",
|
|
122
|
+
)
|
|
123
|
+
assert resp["code"] == 0
|
|
124
|
+
assert len(calls) == 1
|
|
125
|
+
assert calls[0]["url"].endswith(PATH_SCENE_ENHANCEMENT)
|
|
126
|
+
payload = json.loads(calls[0]["data"].decode("utf-8"))
|
|
127
|
+
assert payload == {
|
|
128
|
+
"theme_id": "theme1",
|
|
129
|
+
"image": {"type": "url", "url": "https://example.com/a.jpg"},
|
|
130
|
+
}
|
|
131
|
+
_assert_signed("POST", PATH_SCENE_ENHANCEMENT, calls[0]["data"], calls[0]["headers"])
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_create_with_file_id_does_not_hit_file(monkeypatch):
|
|
135
|
+
client = _client()
|
|
136
|
+
calls = []
|
|
137
|
+
|
|
138
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
139
|
+
calls.append({"url": url, "data": data})
|
|
140
|
+
return FakeResponse(_ok({"task_id": "tid"}))
|
|
141
|
+
|
|
142
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
143
|
+
resp = client.create_scene_enhancement(theme_id="theme1", file_id="fid")
|
|
144
|
+
assert resp["code"] == 0
|
|
145
|
+
assert len(calls) == 1
|
|
146
|
+
payload = json.loads(calls[0]["data"].decode("utf-8"))
|
|
147
|
+
assert payload["image"] == {"type": "file_id", "file_id": "fid"}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_upload_file_success(tmp_path, monkeypatch):
|
|
151
|
+
image = tmp_path / "product.jpg"
|
|
152
|
+
raw = b"jpeg-bytes"
|
|
153
|
+
image.write_bytes(raw)
|
|
154
|
+
client = _client()
|
|
155
|
+
calls = []
|
|
156
|
+
|
|
157
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
158
|
+
calls.append({"method": method, "url": url, "data": data, "headers": headers})
|
|
159
|
+
return FakeResponse(
|
|
160
|
+
_ok(
|
|
161
|
+
{
|
|
162
|
+
"file_id": "fid1",
|
|
163
|
+
"put_url": "https://cos.example/put",
|
|
164
|
+
"expires_at": "2026-09-22 18:00:00",
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
def fake_put(url, data=None, headers=None, timeout=None):
|
|
170
|
+
calls.append({"put_url": url, "put_data": data, "put_headers": headers})
|
|
171
|
+
return FakeResponse(status_code=200)
|
|
172
|
+
|
|
173
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
174
|
+
monkeypatch.setattr(client._session, "put", fake_put)
|
|
175
|
+
resp = client.upload_file(str(image))
|
|
176
|
+
assert resp["code"] == 0
|
|
177
|
+
assert resp["data"]["file_id"] == "fid1"
|
|
178
|
+
assert calls[0]["url"].endswith(PATH_FILE)
|
|
179
|
+
payload = json.loads(calls[0]["data"].decode("utf-8"))
|
|
180
|
+
assert payload == {
|
|
181
|
+
"size": len(raw),
|
|
182
|
+
"content_type": "image/jpeg",
|
|
183
|
+
"filename": "product.jpg",
|
|
184
|
+
}
|
|
185
|
+
_assert_signed("POST", PATH_FILE, calls[0]["data"], calls[0]["headers"])
|
|
186
|
+
assert calls[1]["put_url"] == "https://cos.example/put"
|
|
187
|
+
assert calls[1]["put_data"] == raw
|
|
188
|
+
assert calls[1]["put_headers"]["Content-Type"] == "image/jpeg"
|
|
189
|
+
assert calls[1]["put_headers"]["Content-Length"] == str(len(raw))
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_create_with_image_path_chains_upload(tmp_path, monkeypatch):
|
|
193
|
+
image = tmp_path / "a.png"
|
|
194
|
+
image.write_bytes(b"png-bytes")
|
|
195
|
+
client = _client()
|
|
196
|
+
calls = []
|
|
197
|
+
|
|
198
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
199
|
+
calls.append({"url": url, "data": data})
|
|
200
|
+
if url.endswith(PATH_FILE):
|
|
201
|
+
return FakeResponse(_ok({"file_id": "fid2", "put_url": "https://cos.example/put"}))
|
|
202
|
+
return FakeResponse(_ok({"task_id": "tid2"}))
|
|
203
|
+
|
|
204
|
+
def fake_put(url, data=None, headers=None, timeout=None):
|
|
205
|
+
calls.append({"put": True})
|
|
206
|
+
return FakeResponse(status_code=204)
|
|
207
|
+
|
|
208
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
209
|
+
monkeypatch.setattr(client._session, "put", fake_put)
|
|
210
|
+
resp = client.create_scene_enhancement(theme_id="theme1", image_path=str(image))
|
|
211
|
+
assert resp["code"] == 0
|
|
212
|
+
assert resp["data"]["task_id"] == "tid2"
|
|
213
|
+
assert [item.get("url", "PUT") for item in calls] == [
|
|
214
|
+
"https://openapi.example" + PATH_FILE,
|
|
215
|
+
"PUT",
|
|
216
|
+
"https://openapi.example" + PATH_SCENE_ENHANCEMENT,
|
|
217
|
+
]
|
|
218
|
+
create_payload = json.loads(calls[2]["data"].decode("utf-8"))
|
|
219
|
+
assert create_payload == {
|
|
220
|
+
"theme_id": "theme1",
|
|
221
|
+
"image": {"type": "file_id", "file_id": "fid2"},
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_precreate_failure_skips_put(tmp_path, monkeypatch):
|
|
226
|
+
image = tmp_path / "a.webp"
|
|
227
|
+
image.write_bytes(b"w")
|
|
228
|
+
client = _client()
|
|
229
|
+
put_called = []
|
|
230
|
+
|
|
231
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
232
|
+
return FakeResponse({"code": 10015, "msg": "图片过大,不能超过 5MB", "data": {}, "request_id": "r"})
|
|
233
|
+
|
|
234
|
+
def fake_put(*args, **kwargs):
|
|
235
|
+
put_called.append(True)
|
|
236
|
+
return FakeResponse(status_code=200)
|
|
237
|
+
|
|
238
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
239
|
+
monkeypatch.setattr(client._session, "put", fake_put)
|
|
240
|
+
resp = client.upload_file(str(image))
|
|
241
|
+
assert resp["code"] == 10015
|
|
242
|
+
assert resp["msg"] == "图片过大,不能超过 5MB"
|
|
243
|
+
assert put_called == []
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def test_put_failure_skips_create(tmp_path, monkeypatch):
|
|
247
|
+
image = tmp_path / "a.jpg"
|
|
248
|
+
image.write_bytes(b"x")
|
|
249
|
+
client = _client()
|
|
250
|
+
urls = []
|
|
251
|
+
|
|
252
|
+
def fake_request(method, url, data=None, headers=None, timeout=None):
|
|
253
|
+
urls.append(url)
|
|
254
|
+
return FakeResponse(_ok({"file_id": "fid", "put_url": "https://cos.example/put"}))
|
|
255
|
+
|
|
256
|
+
def fake_put(url, data=None, headers=None, timeout=None):
|
|
257
|
+
return FakeResponse(status_code=403)
|
|
258
|
+
|
|
259
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
260
|
+
monkeypatch.setattr(client._session, "put", fake_put)
|
|
261
|
+
resp = client.create_scene_enhancement(theme_id="theme1", image_path=str(image))
|
|
262
|
+
assert resp["code"] == -1
|
|
263
|
+
assert "403" in resp["msg"]
|
|
264
|
+
assert urls == ["https://openapi.example" + PATH_FILE]
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def test_choose_none_or_many():
|
|
268
|
+
client = _client()
|
|
269
|
+
none = client.create_scene_enhancement(theme_id="t")
|
|
270
|
+
both = client.create_scene_enhancement(theme_id="t", file_id="a", image_url="https://x")
|
|
271
|
+
assert none["code"] == -1
|
|
272
|
+
assert both["code"] == -1
|
|
273
|
+
assert "三选一" in none["msg"]
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def test_missing_file_and_bad_ext(tmp_path):
|
|
277
|
+
client = _client()
|
|
278
|
+
missing = client.upload_file(str(tmp_path / "no.jpg"))
|
|
279
|
+
bad = tmp_path / "a.gif"
|
|
280
|
+
bad.write_bytes(b"gif")
|
|
281
|
+
unsupported = client.upload_file(str(bad))
|
|
282
|
+
assert missing["code"] == -1
|
|
283
|
+
assert "不存在" in missing["msg"]
|
|
284
|
+
assert unsupported["code"] == -1
|
|
285
|
+
assert "格式不支持" in unsupported["msg"]
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def test_network_error(monkeypatch):
|
|
289
|
+
client = _client()
|
|
290
|
+
|
|
291
|
+
def fake_request(*args, **kwargs):
|
|
292
|
+
raise requests.ConnectionError("down")
|
|
293
|
+
|
|
294
|
+
monkeypatch.setattr(client._session, "request", fake_request)
|
|
295
|
+
resp = client.list_themes()
|
|
296
|
+
assert resp["code"] == -1
|
|
297
|
+
assert "请求失败" in resp["msg"]
|