dxpos-openapi 0.0.2__tar.gz → 0.0.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dxpos-openapi
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: DXPOS OpenAPI Python SDK
5
5
  Author-email: DXPOS <support@dxpos.co>
6
6
  License: MIT License
@@ -115,7 +115,7 @@ client = DxposClient(
115
115
 
116
116
  ## 方法
117
117
 
118
- 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
118
+ 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement`、`create_remove_bg` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
119
119
 
120
120
  ### 新增客户标签
121
121
 
@@ -169,6 +169,26 @@ resp = client.create_scene_enhancement(
169
169
  task_id = resp["data"]["task_id"]
170
170
  ```
171
171
 
172
+ ### 创建灵感复刻任务
173
+
174
+ 原图和参考图各自在 `file_id`、`image_path`、`image_url` 里三选一。不需要主题。结果用查询任务和取消任务。
175
+
176
+ ```python
177
+ resp = client.create_scene_migration(
178
+ image_url="https://example.com/source.jpg",
179
+ ref_image_url="https://example.com/reference.jpg",
180
+ customer_tag_id=customer_tag_id,
181
+ )
182
+
183
+ resp = client.create_scene_migration(
184
+ image_path="/path/to/source.jpg",
185
+ ref_image_path="/path/to/reference.jpg",
186
+ customer_tag_id=customer_tag_id,
187
+ )
188
+
189
+ task_id = resp["data"]["task_id"]
190
+ ```
191
+
172
192
  ### 查询任务
173
193
 
174
194
  ```python
@@ -184,6 +204,35 @@ resp = client.get_task(task_id, customer_tag_id=customer_tag_id)
184
204
  resp = client.cancel_task(task_id, customer_tag_id=customer_tag_id)
185
205
  ```
186
206
 
207
+ ### 创建抠图任务
208
+
209
+ `file_id`、`image_path` 必须二选一。`keep_props` 表示是否保留道具:`0` 保留(默认),`1` 不保留只抠人像。
210
+
211
+ ```python
212
+ # 已经 upload_file 拿到 file_id
213
+ resp = client.create_remove_bg(file_id=file_id, keep_props=0)
214
+
215
+ # 本地路径:自动上传再建任务
216
+ resp = client.create_remove_bg(
217
+ image_path="/path/to/portrait.jpg",
218
+ keep_props=1,
219
+ customer_tag_id=customer_tag_id,
220
+ )
221
+
222
+ task_id = resp["data"]["task_id"]
223
+ ```
224
+
225
+ ### 查询抠图任务
226
+
227
+ ```python
228
+ resp = client.get_remove_bg_task(task_id, customer_tag_id=customer_tag_id)
229
+ # data.status: waiting / processing / done / failed
230
+ # done 时 data.result.remove_bg_url 为按人像裁剪的透明底图,
231
+ # data.result.remove_bg_src_url 为未裁剪、与原图同尺寸的透明底图
232
+ ```
233
+
234
+ 抠图失败是终态,不会自动重推,本次扣的额度会自动退回。
235
+
187
236
  ## 维护者:发布到 PyPI
188
237
 
189
238
  不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
@@ -195,7 +244,7 @@ pip install -e ".[dev]"
195
244
  pytest
196
245
  ```
197
246
 
198
- 首次 `0.0.2`:
247
+ 当前版本 `0.0.4`(新增灵感复刻任务):
199
248
 
200
249
  ```bash
201
250
  rm -rf dist build src/*.egg-info
@@ -203,9 +252,9 @@ python -m build
203
252
  twine check dist/*
204
253
  twine upload -r testpypi dist/*
205
254
  # 干净环境验证:
206
- # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.2
255
+ # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.4
207
256
  twine upload dist/*
208
- git tag v0.0.2
257
+ git tag v0.0.4
209
258
  git push origin main --tags
210
259
  ```
211
260
 
@@ -58,7 +58,7 @@ client = DxposClient(
58
58
 
59
59
  ## 方法
60
60
 
61
- 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
61
+ 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement`、`create_remove_bg` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
62
62
 
63
63
  ### 新增客户标签
64
64
 
@@ -112,6 +112,26 @@ resp = client.create_scene_enhancement(
112
112
  task_id = resp["data"]["task_id"]
113
113
  ```
114
114
 
115
+ ### 创建灵感复刻任务
116
+
117
+ 原图和参考图各自在 `file_id`、`image_path`、`image_url` 里三选一。不需要主题。结果用查询任务和取消任务。
118
+
119
+ ```python
120
+ resp = client.create_scene_migration(
121
+ image_url="https://example.com/source.jpg",
122
+ ref_image_url="https://example.com/reference.jpg",
123
+ customer_tag_id=customer_tag_id,
124
+ )
125
+
126
+ resp = client.create_scene_migration(
127
+ image_path="/path/to/source.jpg",
128
+ ref_image_path="/path/to/reference.jpg",
129
+ customer_tag_id=customer_tag_id,
130
+ )
131
+
132
+ task_id = resp["data"]["task_id"]
133
+ ```
134
+
115
135
  ### 查询任务
116
136
 
117
137
  ```python
@@ -127,6 +147,35 @@ resp = client.get_task(task_id, customer_tag_id=customer_tag_id)
127
147
  resp = client.cancel_task(task_id, customer_tag_id=customer_tag_id)
128
148
  ```
129
149
 
150
+ ### 创建抠图任务
151
+
152
+ `file_id`、`image_path` 必须二选一。`keep_props` 表示是否保留道具:`0` 保留(默认),`1` 不保留只抠人像。
153
+
154
+ ```python
155
+ # 已经 upload_file 拿到 file_id
156
+ resp = client.create_remove_bg(file_id=file_id, keep_props=0)
157
+
158
+ # 本地路径:自动上传再建任务
159
+ resp = client.create_remove_bg(
160
+ image_path="/path/to/portrait.jpg",
161
+ keep_props=1,
162
+ customer_tag_id=customer_tag_id,
163
+ )
164
+
165
+ task_id = resp["data"]["task_id"]
166
+ ```
167
+
168
+ ### 查询抠图任务
169
+
170
+ ```python
171
+ resp = client.get_remove_bg_task(task_id, customer_tag_id=customer_tag_id)
172
+ # data.status: waiting / processing / done / failed
173
+ # done 时 data.result.remove_bg_url 为按人像裁剪的透明底图,
174
+ # data.result.remove_bg_src_url 为未裁剪、与原图同尺寸的透明底图
175
+ ```
176
+
177
+ 抠图失败是终态,不会自动重推,本次扣的额度会自动退回。
178
+
130
179
  ## 维护者:发布到 PyPI
131
180
 
132
181
  不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
@@ -138,7 +187,7 @@ pip install -e ".[dev]"
138
187
  pytest
139
188
  ```
140
189
 
141
- 首次 `0.0.2`:
190
+ 当前版本 `0.0.4`(新增灵感复刻任务):
142
191
 
143
192
  ```bash
144
193
  rm -rf dist build src/*.egg-info
@@ -146,9 +195,9 @@ python -m build
146
195
  twine check dist/*
147
196
  twine upload -r testpypi dist/*
148
197
  # 干净环境验证:
149
- # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.2
198
+ # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.4
150
199
  twine upload dist/*
151
- git tag v0.0.2
200
+ git tag v0.0.4
152
201
  git push origin main --tags
153
202
  ```
154
203
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "dxpos-openapi"
7
- version = "0.0.2"
7
+ version = "0.0.4"
8
8
  description = "DXPOS OpenAPI Python SDK"
9
9
  readme = "README.md"
10
10
  license = {file = "LICENSE"}
@@ -1,6 +1,6 @@
1
1
  from dxpos_openapi.client import DxposClient
2
2
  from dxpos_openapi.constants import DEFAULT_BASE_URL
3
3
 
4
- __version__ = "0.0.2"
4
+ __version__ = "0.0.4"
5
5
 
6
6
  __all__ = ["DxposClient", "DEFAULT_BASE_URL", "__version__"]
@@ -12,9 +12,12 @@ from dxpos_openapi.constants import (
12
12
  HEADER_CUSTOMER_TAG,
13
13
  PATH_CUSTOMER_TAG,
14
14
  PATH_FILE,
15
+ PATH_REMOVE_BG,
15
16
  PATH_SCENE_ENHANCEMENT,
17
+ PATH_SCENE_MIGRATION,
16
18
  PATH_THEME,
17
19
  path_cancel,
20
+ path_remove_bg_task,
18
21
  path_task,
19
22
  )
20
23
 
@@ -114,12 +117,81 @@ class DxposClient(object):
114
117
  customer_tag_id=customer_tag_id,
115
118
  )
116
119
 
120
+ def _one_image(self, file_id, image_path, image_url, customer_tag_id, label):
121
+ picked = [item for item in (file_id, image_path, image_url) if item]
122
+ if len(picked) != 1:
123
+ return None, _fail(f"{label}的 file_id、image_path、image_url 必须三选一")
124
+ if image_url:
125
+ return {"type": IMAGE_TYPE_URL, "url": image_url}, None
126
+ if image_path:
127
+ uploaded = self.upload_file(image_path, customer_tag_id=customer_tag_id)
128
+ if uploaded.get("code") != 0:
129
+ return None, uploaded
130
+ file_id = (uploaded.get("data") or {}).get("file_id")
131
+ if not file_id:
132
+ return None, _fail("上传成功但缺少 file_id")
133
+ return {"type": IMAGE_TYPE_FILE_ID, "file_id": file_id}, None
134
+
135
+ def create_scene_migration(
136
+ self,
137
+ file_id=None,
138
+ image_path=None,
139
+ image_url=None,
140
+ ref_file_id=None,
141
+ ref_image_path=None,
142
+ ref_image_url=None,
143
+ customer_tag_id=None,
144
+ ):
145
+ image, error = self._one_image(
146
+ file_id, image_path, image_url, customer_tag_id, "原图"
147
+ )
148
+ if error is not None:
149
+ return error
150
+ ref_image, error = self._one_image(
151
+ ref_file_id, ref_image_path, ref_image_url, customer_tag_id, "参考图"
152
+ )
153
+ if error is not None:
154
+ return error
155
+ return self._request(
156
+ "POST",
157
+ PATH_SCENE_MIGRATION,
158
+ {"image": image, "ref_image": ref_image},
159
+ customer_tag_id=customer_tag_id,
160
+ )
161
+
117
162
  def get_task(self, task_id, customer_tag_id=None):
118
163
  return self._request("GET", path_task(task_id), customer_tag_id=customer_tag_id)
119
164
 
120
165
  def cancel_task(self, task_id, customer_tag_id=None):
121
166
  return self._request("POST", path_cancel(task_id), customer_tag_id=customer_tag_id)
122
167
 
168
+ def create_remove_bg(
169
+ self, file_id=None, image_path=None, keep_props=0, customer_tag_id=None
170
+ ):
171
+ picked = [item for item in (file_id, image_path) if item]
172
+ if len(picked) != 1:
173
+ return _fail("file_id、image_path 必须二选一")
174
+ if keep_props not in (0, 1):
175
+ return _fail("keep_props 只能是 0(保留道具)或 1(不保留)")
176
+ if image_path:
177
+ uploaded = self.upload_file(image_path, customer_tag_id=customer_tag_id)
178
+ if uploaded.get("code") != 0:
179
+ return uploaded
180
+ file_id = (uploaded.get("data") or {}).get("file_id")
181
+ if not file_id:
182
+ return _fail("上传成功但缺少 file_id")
183
+ return self._request(
184
+ "POST",
185
+ PATH_REMOVE_BG,
186
+ {"file_id": file_id, "keep_props": keep_props},
187
+ customer_tag_id=customer_tag_id,
188
+ )
189
+
190
+ def get_remove_bg_task(self, task_id, customer_tag_id=None):
191
+ return self._request(
192
+ "GET", path_remove_bg_task(task_id), customer_tag_id=customer_tag_id
193
+ )
194
+
123
195
  def _request(self, method, path, payload=None, customer_tag_id=None):
124
196
  body = b"" if payload is None else _encode_json(payload)
125
197
  headers = sign_headers(self.access_key, self.secret_key, method, path, body)
@@ -3,6 +3,8 @@ DEFAULT_BASE_URL = "https://openapi.dxpos.co"
3
3
  PATH_THEME = "/api/v1/theme"
4
4
  PATH_FILE = "/api/v1/file"
5
5
  PATH_SCENE_ENHANCEMENT = "/api/v1/task/scene_enhancement"
6
+ PATH_SCENE_MIGRATION = "/api/v1/task/scene_migration"
7
+ PATH_REMOVE_BG = "/api/v1/task/remove_bg"
6
8
  PATH_CUSTOMER_TAG = "/api/v1/customer_tag"
7
9
 
8
10
  HEADER_ACCESS_KEY = "X-Access-Key"
@@ -28,3 +30,7 @@ def path_task(task_id):
28
30
 
29
31
  def path_cancel(task_id):
30
32
  return "/api/v1/task/{}/cancel".format(task_id)
33
+
34
+
35
+ def path_remove_bg_task(task_id):
36
+ return "/api/v1/task/remove_bg/{}".format(task_id)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dxpos-openapi
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: DXPOS OpenAPI Python SDK
5
5
  Author-email: DXPOS <support@dxpos.co>
6
6
  License: MIT License
@@ -115,7 +115,7 @@ client = DxposClient(
115
115
 
116
116
  ## 方法
117
117
 
118
- 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
118
+ 可选参数 `customer_tag_id` 只加在预创建、创建任务、查询、取消上。不传或传空不带头;有值时请求加上 `X-Customer-Tag-Id`。这个头不进入签名串。`create_scene_enhancement`、`create_remove_bg` 走 `image_path` 时,预创建和创建任务使用同一个 `customer_tag_id`。
119
119
 
120
120
  ### 新增客户标签
121
121
 
@@ -169,6 +169,26 @@ resp = client.create_scene_enhancement(
169
169
  task_id = resp["data"]["task_id"]
170
170
  ```
171
171
 
172
+ ### 创建灵感复刻任务
173
+
174
+ 原图和参考图各自在 `file_id`、`image_path`、`image_url` 里三选一。不需要主题。结果用查询任务和取消任务。
175
+
176
+ ```python
177
+ resp = client.create_scene_migration(
178
+ image_url="https://example.com/source.jpg",
179
+ ref_image_url="https://example.com/reference.jpg",
180
+ customer_tag_id=customer_tag_id,
181
+ )
182
+
183
+ resp = client.create_scene_migration(
184
+ image_path="/path/to/source.jpg",
185
+ ref_image_path="/path/to/reference.jpg",
186
+ customer_tag_id=customer_tag_id,
187
+ )
188
+
189
+ task_id = resp["data"]["task_id"]
190
+ ```
191
+
172
192
  ### 查询任务
173
193
 
174
194
  ```python
@@ -184,6 +204,35 @@ resp = client.get_task(task_id, customer_tag_id=customer_tag_id)
184
204
  resp = client.cancel_task(task_id, customer_tag_id=customer_tag_id)
185
205
  ```
186
206
 
207
+ ### 创建抠图任务
208
+
209
+ `file_id`、`image_path` 必须二选一。`keep_props` 表示是否保留道具:`0` 保留(默认),`1` 不保留只抠人像。
210
+
211
+ ```python
212
+ # 已经 upload_file 拿到 file_id
213
+ resp = client.create_remove_bg(file_id=file_id, keep_props=0)
214
+
215
+ # 本地路径:自动上传再建任务
216
+ resp = client.create_remove_bg(
217
+ image_path="/path/to/portrait.jpg",
218
+ keep_props=1,
219
+ customer_tag_id=customer_tag_id,
220
+ )
221
+
222
+ task_id = resp["data"]["task_id"]
223
+ ```
224
+
225
+ ### 查询抠图任务
226
+
227
+ ```python
228
+ resp = client.get_remove_bg_task(task_id, customer_tag_id=customer_tag_id)
229
+ # data.status: waiting / processing / done / failed
230
+ # done 时 data.result.remove_bg_url 为按人像裁剪的透明底图,
231
+ # data.result.remove_bg_src_url 为未裁剪、与原图同尺寸的透明底图
232
+ ```
233
+
234
+ 抠图失败是终态,不会自动重推,本次扣的额度会自动退回。
235
+
187
236
  ## 维护者:发布到 PyPI
188
237
 
189
238
  不需要把 token 写进仓库。本机配置 `~/.pypirc`,用 `__token__` 和 API token。
@@ -195,7 +244,7 @@ pip install -e ".[dev]"
195
244
  pytest
196
245
  ```
197
246
 
198
- 首次 `0.0.2`:
247
+ 当前版本 `0.0.4`(新增灵感复刻任务):
199
248
 
200
249
  ```bash
201
250
  rm -rf dist build src/*.egg-info
@@ -203,9 +252,9 @@ python -m build
203
252
  twine check dist/*
204
253
  twine upload -r testpypi dist/*
205
254
  # 干净环境验证:
206
- # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.2
255
+ # pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple dxpos-openapi==0.0.4
207
256
  twine upload dist/*
208
- git tag v0.0.2
257
+ git tag v0.0.4
209
258
  git push origin main --tags
210
259
  ```
211
260
 
@@ -16,7 +16,9 @@ from dxpos_openapi.constants import (
16
16
  HEADER_TIMESTAMP,
17
17
  PATH_CUSTOMER_TAG,
18
18
  PATH_FILE,
19
+ PATH_REMOVE_BG,
19
20
  PATH_SCENE_ENHANCEMENT,
21
+ PATH_SCENE_MIGRATION,
20
22
  PATH_THEME,
21
23
  )
22
24
 
@@ -338,6 +340,149 @@ def test_image_path_reuses_customer_tag(tmp_path, monkeypatch):
338
340
  assert tags == ["tag1", "tag1"]
339
341
 
340
342
 
343
+ def test_create_remove_bg_with_file_id(monkeypatch):
344
+ client = _client()
345
+ calls = []
346
+
347
+ def fake_request(method, url, data=None, headers=None, timeout=None):
348
+ calls.append({"method": method, "url": url, "data": data, "headers": headers})
349
+ return FakeResponse(_ok({"task_id": "rid", "status": "waiting"}))
350
+
351
+ monkeypatch.setattr(client._session, "request", fake_request)
352
+ resp = client.create_remove_bg(file_id="fid", keep_props=1)
353
+ assert resp["code"] == 0
354
+ assert len(calls) == 1
355
+ assert calls[0]["url"].endswith(PATH_REMOVE_BG)
356
+ payload = json.loads(calls[0]["data"].decode("utf-8"))
357
+ assert payload == {"file_id": "fid", "keep_props": 1}
358
+ _assert_signed("POST", PATH_REMOVE_BG, calls[0]["data"], calls[0]["headers"])
359
+
360
+
361
+ def test_create_remove_bg_with_image_path_chains_upload(tmp_path, monkeypatch):
362
+ image = tmp_path / "a.png"
363
+ image.write_bytes(b"png-bytes")
364
+ client = _client()
365
+ calls = []
366
+
367
+ def fake_request(method, url, data=None, headers=None, timeout=None):
368
+ calls.append({"url": url, "data": data, "tag": headers.get(HEADER_CUSTOMER_TAG)})
369
+ if url.endswith(PATH_FILE):
370
+ return FakeResponse(_ok({"file_id": "fid3", "put_url": "https://cos.example/put"}))
371
+ return FakeResponse(_ok({"task_id": "rid3"}))
372
+
373
+ def fake_put(url, data=None, headers=None, timeout=None):
374
+ calls.append({"put": True})
375
+ return FakeResponse(status_code=200)
376
+
377
+ monkeypatch.setattr(client._session, "request", fake_request)
378
+ monkeypatch.setattr(client._session, "put", fake_put)
379
+ resp = client.create_remove_bg(image_path=str(image), customer_tag_id="tag1")
380
+ assert resp["data"]["task_id"] == "rid3"
381
+ assert [item.get("url", "PUT") for item in calls] == [
382
+ "https://openapi.example" + PATH_FILE,
383
+ "PUT",
384
+ "https://openapi.example" + PATH_REMOVE_BG,
385
+ ]
386
+ assert calls[0]["tag"] == calls[2]["tag"] == "tag1"
387
+ payload = json.loads(calls[2]["data"].decode("utf-8"))
388
+ assert payload == {"file_id": "fid3", "keep_props": 0}
389
+
390
+
391
+ def test_create_remove_bg_invalid_args():
392
+ client = _client()
393
+ none = client.create_remove_bg()
394
+ both = client.create_remove_bg(file_id="a", image_path="/tmp/a.jpg")
395
+ bad_keep = client.create_remove_bg(file_id="a", keep_props=2)
396
+ assert none["code"] == both["code"] == bad_keep["code"] == -1
397
+ assert "二选一" in none["msg"]
398
+ assert "keep_props" in bad_keep["msg"]
399
+
400
+
401
+ def test_get_remove_bg_task(monkeypatch):
402
+ client = _client()
403
+ captured = {}
404
+
405
+ def fake_request(method, url, data=None, headers=None, timeout=None):
406
+ captured.update({"method": method, "url": url, "headers": headers})
407
+ return FakeResponse(_ok({"task_id": "rid", "status": "done"}))
408
+
409
+ monkeypatch.setattr(client._session, "request", fake_request)
410
+ resp = client.get_remove_bg_task("rid")
411
+ assert resp["data"]["status"] == "done"
412
+ assert captured["method"] == "GET"
413
+ assert captured["url"].endswith("/api/v1/task/remove_bg/rid")
414
+ _assert_signed("GET", "/api/v1/task/remove_bg/rid", b"", captured["headers"])
415
+
416
+
417
+ def test_create_scene_migration_urls(monkeypatch):
418
+ client = _client()
419
+ calls = []
420
+
421
+ def fake_request(method, url, data=None, headers=None, timeout=None):
422
+ calls.append({"method": method, "url": url, "data": data, "headers": headers})
423
+ return FakeResponse(_ok({"task_id": "mid"}))
424
+
425
+ monkeypatch.setattr(client._session, "request", fake_request)
426
+ resp = client.create_scene_migration(
427
+ image_url="https://example.com/source.jpg",
428
+ ref_file_id="ref1",
429
+ customer_tag_id="tag1",
430
+ )
431
+ assert resp["code"] == 0
432
+ assert len(calls) == 1
433
+ assert calls[0]["url"].endswith(PATH_SCENE_MIGRATION)
434
+ payload = json.loads(calls[0]["data"].decode("utf-8"))
435
+ assert payload == {
436
+ "image": {"type": "url", "url": "https://example.com/source.jpg"},
437
+ "ref_image": {"type": "file_id", "file_id": "ref1"},
438
+ }
439
+ assert calls[0]["headers"][HEADER_CUSTOMER_TAG] == "tag1"
440
+ _assert_signed("POST", PATH_SCENE_MIGRATION, calls[0]["data"], calls[0]["headers"])
441
+
442
+
443
+ def test_create_scene_migration_local_paths(tmp_path, monkeypatch):
444
+ source = tmp_path / "source.jpg"
445
+ reference = tmp_path / "reference.png"
446
+ source.write_bytes(b"source")
447
+ reference.write_bytes(b"reference")
448
+ client = _client()
449
+ calls = []
450
+
451
+ def fake_request(method, url, data=None, headers=None, timeout=None):
452
+ calls.append({"url": url, "data": data})
453
+ if url.endswith(PATH_FILE):
454
+ file_id = "src" if len(calls) == 1 else "ref"
455
+ return FakeResponse(_ok({"file_id": file_id, "put_url": "https://cos.example/put"}))
456
+ return FakeResponse(_ok({"task_id": "mid"}))
457
+
458
+ def fake_put(url, data=None, headers=None, timeout=None):
459
+ return FakeResponse(status_code=200)
460
+
461
+ monkeypatch.setattr(client._session, "request", fake_request)
462
+ monkeypatch.setattr(client._session, "put", fake_put)
463
+ resp = client.create_scene_migration(
464
+ image_path=str(source), ref_image_path=str(reference)
465
+ )
466
+ assert resp["code"] == 0
467
+ assert calls[-1]["url"].endswith(PATH_SCENE_MIGRATION)
468
+ payload = json.loads(calls[-1]["data"].decode("utf-8"))
469
+ assert payload["image"]["file_id"] == "src"
470
+ assert payload["ref_image"]["file_id"] == "ref"
471
+
472
+
473
+ def test_create_scene_migration_invalid_args():
474
+ client = _client()
475
+ missing = client.create_scene_migration(image_url="https://example.com/a.jpg")
476
+ both = client.create_scene_migration(
477
+ image_url="https://example.com/a.jpg",
478
+ file_id="a",
479
+ ref_image_url="https://example.com/b.jpg",
480
+ )
481
+ assert missing["code"] == both["code"] == -1
482
+ assert "参考图" in missing["msg"]
483
+ assert "原图" in both["msg"]
484
+
485
+
341
486
  def test_network_error(monkeypatch):
342
487
  client = _client()
343
488
 
File without changes
File without changes