Skip to content

Commit 232e0c4

Browse files
committed
Unifying detail and delete methods
1 parent 9cde3a7 commit 232e0c4

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

tidy3d/web/api/webapi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,7 @@ def get_info(task_id: TaskId, verbose: bool = True) -> TaskInfo | BatchDetail:
671671
task = TaskFactory.get(task_id, verbose=verbose)
672672
if not task:
673673
raise ValueError("Task not found.")
674-
if isinstance(task, BatchTask):
675-
return task.detail()
676-
return TaskInfo(**{"taskId": task.task_id, "taskType": task.task_type, **task.dict()})
674+
return task.detail()
677675

678676

679677
@wait_for_connection

tidy3d/web/core/task_core.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .http_util import http
3434
from .s3utils import download_file, download_gz_file, upload_file
3535
from .stub import TaskStub
36-
from .task_info import BatchDetail
36+
from .task_info import BatchDetail, TaskInfo
3737
from .types import PayType, Queryable, ResourceLifecycle, Submittable, Tidy3DResource
3838

3939

@@ -204,6 +204,7 @@ def create(
204204
"groupName": task_name,
205205
"folderId": folder.folder_id,
206206
"fileType": file_type,
207+
"taskType": task_type,
207208
}
208209
resp = http.post("rf/task", payload)
209210
else:
@@ -363,6 +364,33 @@ def is_batch(resource_id: str) -> bool:
363364
except Exception:
364365
return False
365366

367+
def delete(self, versions: bool = False) -> None:
368+
"""Delete current task from server.
369+
370+
Parameters
371+
----------
372+
versions : bool = False
373+
If ``True``, delete all versions of the task in the task group. Otherwise, delete only
374+
the version associated with the current task ID.
375+
"""
376+
if not self.task_id:
377+
raise ValueError("Task id not found.")
378+
379+
task_details = self.detail()
380+
381+
if task_details and "groupId" in task_details:
382+
group_id = task_details["groupId"]
383+
if versions:
384+
http.delete("tidy3d/group", json={"groupIds": [group_id]})
385+
return
386+
elif "version" in task_details:
387+
version = task_details["version"]
388+
http.delete(f"tidy3d/group/{group_id}/versions", json={"versions": [version]})
389+
return
390+
391+
# Fallback to old method if we can't get the groupId and version
392+
http.delete(f"tidy3d/tasks/{self.task_id}")
393+
366394

367395
class SimulationTask(WebTask):
368396
"""Interface for managing the running of solver tasks on the server."""
@@ -455,28 +483,16 @@ def get_running_tasks(cls) -> list[SimulationTask]:
455483
return []
456484
return parse_obj_as(list[SimulationTask], resp)
457485

458-
def delete(self, versions: bool = False) -> None:
459-
"""Delete current task from server.
486+
def detail(self) -> TaskInfo:
487+
"""Fetches the detailed information and status of the task.
460488
461-
Parameters
462-
----------
463-
versions : bool = False
464-
If ``True``, delete all versions of the task in the task group. Otherwise, delete only the version associated with the current task ID.
489+
Returns
490+
-------
491+
TaskInfo
492+
An object containing the task's latest data.
465493
"""
466-
if not self.task_id:
467-
raise ValueError("Task id not found.")
468-
469-
task_details = http.get(f"tidy3d/tasks/{self.task_id}")
470-
471-
if task_details and "groupId" in task_details and "version" in task_details:
472-
group_id = task_details["groupId"]
473-
version = task_details["version"]
474-
if versions:
475-
http.delete("tidy3d/group", json={"groupIds": [group_id]})
476-
else:
477-
http.delete(f"tidy3d/group/{group_id}/versions", json={"versions": [version]})
478-
else: # Fallback to old method if we can't get the groupId and version
479-
http.delete(f"tidy3d/tasks/{self.task_id}")
494+
resp = http.get(f"tidy3d/tasks/{self.task_id}/detail")
495+
return TaskInfo(**{"taskId": self.task_id, "taskType": self.task_type, **resp})
480496

481497
def get_simulation_json(self, to_file: PathLike, verbose: bool = True) -> None:
482498
"""Get json file for a :class:`.Simulation` from server.
@@ -945,20 +961,6 @@ def abort(self) -> requests.Response:
945961
raise ValueError("Batch id not found.")
946962
return http.put(f"rf/task/{self.task_id}/abort", {})
947963

948-
def delete(self, versions: bool = False) -> None:
949-
"""Delete current batch task from server.
950-
951-
Parameters
952-
----------
953-
versions : bool = False
954-
Ignored for batch tasks; present for API compatibility.
955-
"""
956-
if not self.task_id:
957-
raise ValueError("Batch id not found.")
958-
if versions:
959-
raise NotImplementedError("Deleting all versions is not supported for batch tasks.")
960-
http.delete(f"rf/task/{self.task_id}")
961-
962964

963965
class TaskFactory:
964966
"""Factory for obtaining the correct task subclass."""

0 commit comments

Comments
 (0)