|
5 | 5 | from nisystemlink.clients.core._uplink._base_client import BaseClient |
6 | 6 | from nisystemlink.clients.core._uplink._methods import get, post |
7 | 7 | from nisystemlink.clients.work_item import models |
8 | | -from uplink import Field, retry |
| 8 | +from uplink import Field, Path, retry |
| 9 | + |
| 10 | + |
| 11 | +class WorkItemExecuteApiException(core.ApiException): |
| 12 | + """Raised when the execute work item API returns an error with a structured response body. |
| 13 | +
|
| 14 | + The API may return partial results (e.g. cancelled job IDs) alongside an error. |
| 15 | + This exception exposes those results via the :attr:`result` property. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + msg: str, |
| 21 | + *, |
| 22 | + http_status_code: int, |
| 23 | + error: core.ApiError | None, |
| 24 | + result: models.ExecutionResult | None, |
| 25 | + ) -> None: |
| 26 | + super().__init__(msg, error=error, http_status_code=http_status_code) |
| 27 | + self._result = result |
| 28 | + |
| 29 | + @property |
| 30 | + def result(self) -> models.ExecutionResult | None: |
| 31 | + """The partial execution result returned alongside the error, if any.""" |
| 32 | + return self._result |
9 | 33 |
|
10 | 34 |
|
11 | 35 | @retry( |
@@ -132,6 +156,54 @@ def delete_work_items( |
132 | 156 | """ |
133 | 157 | ... |
134 | 158 |
|
| 159 | + def execute_work_item( |
| 160 | + self, work_item_id: str, action: str |
| 161 | + ) -> models.ExecuteWorkItemResponse: |
| 162 | + """Executes the specified action for the work item. |
| 163 | +
|
| 164 | + Args: |
| 165 | + work_item_id: The ID of the work item the action will be performed on. |
| 166 | + action: The action to execute on the work item. |
| 167 | +
|
| 168 | + Returns: |
| 169 | + The response containing the execution result. |
| 170 | +
|
| 171 | + Raises: |
| 172 | + WorkItemExecuteApiException: if the API returns an error response with |
| 173 | + an execute-specific body. The :attr:`~WorkItemExecuteApiException.result` |
| 174 | + property may contain partial results (e.g. cancelled job IDs). |
| 175 | + ApiException: if unable to communicate with the `/niworkitem` service |
| 176 | + or provided invalid arguments. |
| 177 | + """ |
| 178 | + try: |
| 179 | + return self._execute_work_item(work_item_id=work_item_id, action=action) |
| 180 | + except core.ApiException as e: |
| 181 | + data = e.response_data |
| 182 | + if not data or "result" not in data: |
| 183 | + raise |
| 184 | + |
| 185 | + try: |
| 186 | + response = models.ExecuteWorkItemResponse.model_validate(data) |
| 187 | + except Exception: |
| 188 | + raise e |
| 189 | + |
| 190 | + raise WorkItemExecuteApiException( |
| 191 | + str(e), |
| 192 | + http_status_code=e.http_status_code or 0, |
| 193 | + error=response.error, |
| 194 | + result=response.result, |
| 195 | + ) from e |
| 196 | + |
| 197 | + @post( |
| 198 | + "workitems/{workItemId}/execute", |
| 199 | + args=[Path(name="workItemId"), Field("action")], |
| 200 | + ) |
| 201 | + def _execute_work_item( |
| 202 | + self, work_item_id: str, action: str |
| 203 | + ) -> models.ExecuteWorkItemResponse: |
| 204 | + """Internal implementation of execute_work_item.""" |
| 205 | + ... |
| 206 | + |
135 | 207 | @post("workitem-templates", args=[Field("workItemTemplates")]) |
136 | 208 | def create_work_item_templates( |
137 | 209 | self, work_item_templates: List[models.CreateWorkItemTemplateRequest] |
|
0 commit comments