Skip to content

Commit 99fbb13

Browse files
feat(cli): add fetch, wait, cancel, and list commands for async tasks
- Add fetch command to get task status or result for image generation, synthesis, and transcription - Add wait command to wait for task completion for all three task types - Implement cancel command to cancel pending tasks with success confirmation - Add list command to list tasks with filtering options for all three task types - Update CLI modules to import and use success helper for cancel command feedback - Add comprehensive unit tests for new commands in image generation, synthesis, and transcription CLI tests
1 parent a25f7e1 commit 99fbb13

6 files changed

Lines changed: 760 additions & 2 deletions

File tree

‎dashscope/cli/image_generation.py‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ensure_ok,
1313
handle_sdk_error,
1414
normalize_local_path_or_url,
15+
success,
1516
)
1617

1718
app = typer.Typer(
@@ -86,3 +87,107 @@ def create(
8687
usage = getattr(response, "usage", None)
8788
if usage:
8889
console.print_json(json.dumps(usage, ensure_ascii=False))
90+
91+
92+
@app.command("fetch")
93+
@handle_sdk_error("Fetch image generation task failed")
94+
def fetch(
95+
task_id: str = typer.Argument(..., help="The image generation task id"),
96+
workspace: Optional[str] = typer.Option(
97+
None,
98+
"-w",
99+
"--workspace",
100+
help="The DashScope workspace id",
101+
),
102+
):
103+
"""Fetch image generation task status or result."""
104+
response = ImageGeneration.fetch(task_id, workspace=workspace)
105+
output = ensure_ok(response)
106+
console.print_json(json.dumps(output, ensure_ascii=False))
107+
108+
109+
@app.command("wait")
110+
@handle_sdk_error("Wait image generation task failed")
111+
def wait(
112+
task_id: str = typer.Argument(..., help="The image generation task id"),
113+
workspace: Optional[str] = typer.Option(
114+
None,
115+
"-w",
116+
"--workspace",
117+
help="The DashScope workspace id",
118+
),
119+
):
120+
"""Wait for an image generation task to complete."""
121+
response = ImageGeneration.wait(task_id, workspace=workspace)
122+
output = ensure_ok(response)
123+
console.print_json(json.dumps(output, ensure_ascii=False))
124+
125+
126+
@app.command("cancel")
127+
@handle_sdk_error("Cancel image generation task failed")
128+
def cancel(
129+
task_id: str = typer.Argument(..., help="The image generation task id"),
130+
workspace: Optional[str] = typer.Option(
131+
None,
132+
"-w",
133+
"--workspace",
134+
help="The DashScope workspace id",
135+
),
136+
):
137+
"""Cancel a pending image generation task."""
138+
ensure_ok(ImageGeneration.cancel(task_id, workspace=workspace))
139+
success(f"Cancel image generation task: {task_id} success")
140+
141+
142+
@app.command("list")
143+
@handle_sdk_error("List image generation tasks failed")
144+
def list_tasks(
145+
start_time: Optional[str] = typer.Option(
146+
None,
147+
"--start-time",
148+
help="Task start time",
149+
),
150+
end_time: Optional[str] = typer.Option(
151+
None,
152+
"--end-time",
153+
help="Task end time",
154+
),
155+
model_name: Optional[str] = typer.Option(
156+
None,
157+
"--model-name",
158+
help="Model name",
159+
),
160+
api_key_id: Optional[str] = typer.Option(
161+
None,
162+
"--api-key-id",
163+
help="API key id",
164+
),
165+
region: Optional[str] = typer.Option(
166+
None,
167+
"--region",
168+
help="Service region",
169+
),
170+
status: Optional[str] = typer.Option(None, "--status", help="Task status"),
171+
page: int = typer.Option(1, "-p", "--page", help="Page number"),
172+
size: int = typer.Option(10, "-s", "--size", help="Page size"),
173+
workspace: Optional[str] = typer.Option(
174+
None,
175+
"-w",
176+
"--workspace",
177+
help="The DashScope workspace id",
178+
),
179+
):
180+
"""List image generation tasks."""
181+
response = ImageGeneration.list(
182+
start_time=start_time,
183+
end_time=end_time,
184+
model_name=model_name,
185+
api_key_id=api_key_id,
186+
region=region,
187+
status=status,
188+
page_no=page,
189+
page_size=size,
190+
workspace=workspace,
191+
)
192+
output = ensure_ok(response)
193+
console.print_json(json.dumps(output, ensure_ascii=False))

‎dashscope/cli/image_synthesis.py‎

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import typer
77

88
import dashscope
9-
from dashscope.cli.common import console, ensure_ok, handle_sdk_error
9+
from dashscope.cli.common import (
10+
console,
11+
ensure_ok,
12+
handle_sdk_error,
13+
success,
14+
)
1015

1116
app = typer.Typer(
1217
name="image-synthesis",
@@ -66,3 +71,107 @@ def create(
6671
usage = getattr(response, "usage", None)
6772
if usage:
6873
console.print_json(json.dumps(usage, ensure_ascii=False))
74+
75+
76+
@app.command("fetch")
77+
@handle_sdk_error("Fetch image synthesis task failed")
78+
def fetch(
79+
task_id: str = typer.Argument(..., help="The image synthesis task id"),
80+
workspace: Optional[str] = typer.Option(
81+
None,
82+
"-w",
83+
"--workspace",
84+
help="The DashScope workspace id",
85+
),
86+
):
87+
"""Fetch image synthesis task status or result."""
88+
response = dashscope.ImageSynthesis.fetch(task_id, workspace=workspace)
89+
output = ensure_ok(response)
90+
console.print_json(json.dumps(output, ensure_ascii=False))
91+
92+
93+
@app.command("wait")
94+
@handle_sdk_error("Wait image synthesis task failed")
95+
def wait(
96+
task_id: str = typer.Argument(..., help="The image synthesis task id"),
97+
workspace: Optional[str] = typer.Option(
98+
None,
99+
"-w",
100+
"--workspace",
101+
help="The DashScope workspace id",
102+
),
103+
):
104+
"""Wait for an image synthesis task to complete."""
105+
response = dashscope.ImageSynthesis.wait(task_id, workspace=workspace)
106+
output = ensure_ok(response)
107+
console.print_json(json.dumps(output, ensure_ascii=False))
108+
109+
110+
@app.command("cancel")
111+
@handle_sdk_error("Cancel image synthesis task failed")
112+
def cancel(
113+
task_id: str = typer.Argument(..., help="The image synthesis task id"),
114+
workspace: Optional[str] = typer.Option(
115+
None,
116+
"-w",
117+
"--workspace",
118+
help="The DashScope workspace id",
119+
),
120+
):
121+
"""Cancel a pending image synthesis task."""
122+
ensure_ok(dashscope.ImageSynthesis.cancel(task_id, workspace=workspace))
123+
success(f"Cancel image synthesis task: {task_id} success")
124+
125+
126+
@app.command("list")
127+
@handle_sdk_error("List image synthesis tasks failed")
128+
def list_tasks(
129+
start_time: Optional[str] = typer.Option(
130+
None,
131+
"--start-time",
132+
help="Task start time",
133+
),
134+
end_time: Optional[str] = typer.Option(
135+
None,
136+
"--end-time",
137+
help="Task end time",
138+
),
139+
model_name: Optional[str] = typer.Option(
140+
None,
141+
"--model-name",
142+
help="Model name",
143+
),
144+
api_key_id: Optional[str] = typer.Option(
145+
None,
146+
"--api-key-id",
147+
help="API key id",
148+
),
149+
region: Optional[str] = typer.Option(
150+
None,
151+
"--region",
152+
help="Service region",
153+
),
154+
status: Optional[str] = typer.Option(None, "--status", help="Task status"),
155+
page: int = typer.Option(1, "-p", "--page", help="Page number"),
156+
size: int = typer.Option(10, "-s", "--size", help="Page size"),
157+
workspace: Optional[str] = typer.Option(
158+
None,
159+
"-w",
160+
"--workspace",
161+
help="The DashScope workspace id",
162+
),
163+
):
164+
"""List image synthesis tasks."""
165+
response = dashscope.ImageSynthesis.list(
166+
start_time=start_time,
167+
end_time=end_time,
168+
model_name=model_name,
169+
api_key_id=api_key_id,
170+
region=region,
171+
status=status,
172+
page_no=page,
173+
page_size=size,
174+
workspace=workspace,
175+
)
176+
output = ensure_ok(response)
177+
console.print_json(json.dumps(output, ensure_ascii=False))

‎dashscope/cli/transcription.py‎

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import typer
77

88
import dashscope
9-
from dashscope.cli.common import console, ensure_ok, handle_sdk_error
9+
from dashscope.cli.common import (
10+
console,
11+
ensure_ok,
12+
handle_sdk_error,
13+
success,
14+
)
1015

1116
app = typer.Typer(
1217
name="transcription",
@@ -100,3 +105,107 @@ def create(
100105
usage = getattr(response, "usage", None)
101106
if usage:
102107
console.print_json(json.dumps(usage, ensure_ascii=False))
108+
109+
110+
@app.command("fetch")
111+
@handle_sdk_error("Fetch transcription task failed")
112+
def fetch(
113+
task_id: str = typer.Argument(..., help="The transcription task id"),
114+
workspace: Optional[str] = typer.Option(
115+
None,
116+
"-w",
117+
"--workspace",
118+
help="The DashScope workspace id",
119+
),
120+
):
121+
"""Fetch transcription task status or result."""
122+
response = dashscope.Transcription.fetch(task_id, workspace=workspace)
123+
output = ensure_ok(response)
124+
console.print_json(json.dumps(output, ensure_ascii=False))
125+
126+
127+
@app.command("wait")
128+
@handle_sdk_error("Wait transcription task failed")
129+
def wait(
130+
task_id: str = typer.Argument(..., help="The transcription task id"),
131+
workspace: Optional[str] = typer.Option(
132+
None,
133+
"-w",
134+
"--workspace",
135+
help="The DashScope workspace id",
136+
),
137+
):
138+
"""Wait for a transcription task to complete."""
139+
response = dashscope.Transcription.wait(task_id, workspace=workspace)
140+
output = ensure_ok(response)
141+
console.print_json(json.dumps(output, ensure_ascii=False))
142+
143+
144+
@app.command("cancel")
145+
@handle_sdk_error("Cancel transcription task failed")
146+
def cancel(
147+
task_id: str = typer.Argument(..., help="The transcription task id"),
148+
workspace: Optional[str] = typer.Option(
149+
None,
150+
"-w",
151+
"--workspace",
152+
help="The DashScope workspace id",
153+
),
154+
):
155+
"""Cancel a pending transcription task."""
156+
ensure_ok(dashscope.Transcription.cancel(task_id, workspace=workspace))
157+
success(f"Cancel transcription task: {task_id} success")
158+
159+
160+
@app.command("list")
161+
@handle_sdk_error("List transcription tasks failed")
162+
def list_tasks(
163+
start_time: Optional[str] = typer.Option(
164+
None,
165+
"--start-time",
166+
help="Task start time",
167+
),
168+
end_time: Optional[str] = typer.Option(
169+
None,
170+
"--end-time",
171+
help="Task end time",
172+
),
173+
model_name: Optional[str] = typer.Option(
174+
None,
175+
"--model-name",
176+
help="Model name",
177+
),
178+
api_key_id: Optional[str] = typer.Option(
179+
None,
180+
"--api-key-id",
181+
help="API key id",
182+
),
183+
region: Optional[str] = typer.Option(
184+
None,
185+
"--region",
186+
help="Service region",
187+
),
188+
status: Optional[str] = typer.Option(None, "--status", help="Task status"),
189+
page: int = typer.Option(1, "-p", "--page", help="Page number"),
190+
size: int = typer.Option(10, "-s", "--size", help="Page size"),
191+
workspace: Optional[str] = typer.Option(
192+
None,
193+
"-w",
194+
"--workspace",
195+
help="The DashScope workspace id",
196+
),
197+
):
198+
"""List transcription tasks."""
199+
response = dashscope.Transcription.list(
200+
start_time=start_time,
201+
end_time=end_time,
202+
model_name=model_name,
203+
api_key_id=api_key_id,
204+
region=region,
205+
status=status,
206+
page_no=page,
207+
page_size=size,
208+
workspace=workspace,
209+
)
210+
output = ensure_ok(response)
211+
console.print_json(json.dumps(output, ensure_ascii=False))

0 commit comments

Comments
 (0)