From 1f70f928e59dd1e64b87767fcbc71396d4cec676 Mon Sep 17 00:00:00 2001 From: ali-zipstack Date: Mon, 21 Apr 2025 12:45:46 +0530 Subject: [PATCH 1/2] update status codes --- backend/api_v2/api_deployment_views.py | 26 ++++++++++++++----- .../workflow_v2/workflow_helper.py | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/backend/api_v2/api_deployment_views.py b/backend/api_v2/api_deployment_views.py index 92bf5ce690..3d9b5faa6a 100644 --- a/backend/api_v2/api_deployment_views.py +++ b/backend/api_v2/api_deployment_views.py @@ -23,8 +23,8 @@ from rest_framework.request import Request from rest_framework.response import Response from rest_framework.serializers import Serializer -from utils.enums import CeleryTaskState from workflow_manager.workflow_v2.dto import ExecutionResponse +from workflow_manager.workflow_v2.enums import ExecutionStatus logger = logging.getLogger(__name__) @@ -89,8 +89,18 @@ def get( execution_id ) # Determine response status - response_status = status.HTTP_422_UNPROCESSABLE_ENTITY - if response.execution_status == CeleryTaskState.COMPLETED.value: + + # Handle result already acknowledged + if response.result_acknowledged: + return Response( + data={ + "status": response.execution_status, + "message": "Result already acknowledged", + }, + status=status.HTTP_406_NOT_ACCEPTABLE, + ) + + if response.execution_status == ExecutionStatus.COMPLETED: response_status = status.HTTP_200_OK if not settings.ENABLE_HIGHLIGHT_API_DEPLOYMENT: response.remove_result_metadata_keys(["highlight_data"]) @@ -98,9 +108,13 @@ def get( response.remove_result_metadata_keys() if not include_metrics: response.remove_result_metrics() - if response.result_acknowledged: - response_status = status.HTTP_406_NOT_ACCEPTABLE - response.result = "Result already acknowledged" + elif response.execution_status in ( + ExecutionStatus.EXECUTING, + ExecutionStatus.PENDING, + ): + response_status = status.HTTP_202_ACCEPTED + else: # STOPPED, ERROR etc.. + response_status = status.HTTP_422_UNPROCESSABLE_ENTITY return Response( data={ "status": response.execution_status, diff --git a/backend/workflow_manager/workflow_v2/workflow_helper.py b/backend/workflow_manager/workflow_v2/workflow_helper.py index cfca80ad37..d7b1b2ba5d 100644 --- a/backend/workflow_manager/workflow_v2/workflow_helper.py +++ b/backend/workflow_manager/workflow_v2/workflow_helper.py @@ -457,7 +457,7 @@ def get_status_of_async_task( Returns: ExecutionResponse: _description_ """ - execution = WorkflowExecution.objects.get(id=execution_id) + execution: WorkflowExecution = WorkflowExecution.objects.get(id=execution_id) if not execution.task_id: raise TaskDoesNotExistError( f"No task ID found for execution: {execution_id}" From 3712957f348350a8138e9ab8fc3548cbc0b48f3d Mon Sep 17 00:00:00 2001 From: ali Date: Sat, 12 Jul 2025 11:05:54 +0530 Subject: [PATCH 2/2] UN-2584 [FIX] Corrected HTTP status codes for workflow execution status API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed PENDING and EXECUTING states to return 200 OK instead of 422, resolving API integration issues where clients expected 200 OK for valid in-progress states per documentation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- backend/api_v2/api_deployment_views.py | 62 +++++++++++++++++++++----- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/backend/api_v2/api_deployment_views.py b/backend/api_v2/api_deployment_views.py index cb99218d49..852c8de203 100644 --- a/backend/api_v2/api_deployment_views.py +++ b/backend/api_v2/api_deployment_views.py @@ -92,6 +92,23 @@ def get( api_name: str, deployment_execution_dto: DeploymentExecutionDTO, ) -> Response: + """Get workflow execution status and results. + + Returns appropriate HTTP status codes based on execution state: + - 200 OK: COMPLETED, EXECUTING, PENDING (valid states) + - 406 Not Acceptable: Result already acknowledged + - 422 Unprocessable Entity: ERROR, STOPPED (failure states) + + Args: + request: HTTP request object + org_name: Organization name + api_name: API deployment name + deployment_execution_dto: Validated deployment context + + Returns: + Response with execution status and results + """ + # Validate query parameters serializer = ExecutionQuerySerializer(data=request.query_params) serializer.is_valid(raise_exception=True) @@ -99,11 +116,10 @@ def get( include_metadata = serializer.validated_data.get(ApiExecution.INCLUDE_METADATA) include_metrics = serializer.validated_data.get(ApiExecution.INCLUDE_METRICS) - # Fetch execution status + # Fetch current execution status response: ExecutionResponse = DeploymentHelper.get_execution_status(execution_id) - # Determine response status - # Handle result already acknowledged + # Check if result was already acknowledged (one-time access) if response.result_acknowledged: return Response( data={ @@ -113,21 +129,23 @@ def get( status=status.HTTP_406_NOT_ACCEPTABLE, ) + # Determine HTTP status code based on execution state if response.execution_status == ExecutionStatus.COMPLETED: + # Successful completion - prepare result data response_status = status.HTTP_200_OK - if not settings.ENABLE_HIGHLIGHT_API_DEPLOYMENT: - response.remove_result_metadata_keys(["highlight_data"]) - if not include_metadata: - response.remove_result_metadata_keys() - if not include_metrics: - response.remove_result_metrics() + self._filter_response_data(response, include_metadata, include_metrics) + elif response.execution_status in ( ExecutionStatus.EXECUTING, ExecutionStatus.PENDING, ): - response_status = status.HTTP_202_ACCEPTED - else: # STOPPED, ERROR etc.. + # Valid in-progress states - return 200 OK (not 422 as before) + response_status = status.HTTP_200_OK + + else: + # Error states (STOPPED, ERROR, etc.) - indicate processing failure response_status = status.HTTP_422_UNPROCESSABLE_ENTITY + return Response( data={ "status": response.execution_status, @@ -136,6 +154,28 @@ def get( status=response_status, ) + def _filter_response_data( + self, response: ExecutionResponse, include_metadata: bool, include_metrics: bool + ) -> None: + """Filter response data based on client preferences and settings. + + Args: + response: Execution response to filter + include_metadata: Whether to include metadata in response + include_metrics: Whether to include metrics in response + """ + # Remove highlight data if not enabled globally + if not settings.ENABLE_HIGHLIGHT_API_DEPLOYMENT: + response.remove_result_metadata_keys(["highlight_data"]) + + # Remove metadata if not requested by client + if not include_metadata: + response.remove_result_metadata_keys() + + # Remove metrics if not requested by client + if not include_metrics: + response.remove_result_metrics() + class APIDeploymentViewSet(viewsets.ModelViewSet): permission_classes = [IsOwner]