Skip to content

Commit

Permalink
added action response
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyanshs7 committed Sep 12, 2024
1 parent 7cf4f49 commit 160568b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 12 additions & 2 deletions apps/spotlight/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import Callable, Dict

import requests
Expand All @@ -14,6 +15,12 @@
}


@dataclass
class ActionResponse:
message: str = None
is_success: bool = None


class HelpService:
@classmethod
def extract_citations(cls, *, citations: list) -> list:
Expand Down Expand Up @@ -85,8 +92,11 @@ def trigger_export(cls, *, workspace_id: int):
}
url = f'http://localhost:8000/api/workspaces/{workspace_id}/trigger_export/'
action_response = requests.post(url, json={}, headers=headers)
if action_response.status_code == 200:
return ActionResponse(message="Export triggered successfully", is_success=True)
return ActionResponse(message="Export triggered failed", is_success=False)

@classmethod
def action(cls, *, code: str, workspace_id: str):
def action(cls, *, code: str, workspace_id: str) -> ActionResponse:
action_function = cls._get_action_function_from_code(code=code)
action_function(workspace_id=workspace_id)
return action_function(workspace_id=workspace_id)
7 changes: 5 additions & 2 deletions apps/spotlight/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ def post(self, request, *args, **kwargs):
code = payload["code"]

try:
ActionService.action(code=code, workspace_id=workspace_id)
return JsonResponse(data={"message": "Action triggered successfully"}, status=200)
action_response = ActionService.action(code=code, workspace_id=workspace_id)
if action_response.is_success is True:
return JsonResponse(data={"message": action_response.message}, status=200)
else:
return JsonResponse(data={"message": action_response.message}, status=500)
except Exception as e:
print(e)
return JsonResponse(data={"message": "Action failed"}, status=500)

0 comments on commit 160568b

Please sign in to comment.