diff --git a/apps/spotlight/service.py b/apps/spotlight/service.py index a091546..7455841 100644 --- a/apps/spotlight/service.py +++ b/apps/spotlight/service.py @@ -1,9 +1,19 @@ -from typing import Dict +from typing import Callable, Dict + +import requests + +from apps.fyle.helpers import get_access_token +from apps.workspaces.models import FyleCredential from .prompts.support_genie import PROMPT as SUPPORT_GENIE_PROMPT from .prompts.spotlight_prompt import PROMPT as SPOTLIGHT_PROMPT from . import llm +code_action_map = { + "trigger_export": 'http://localhost:8000/api/workspaces/2/trigger_export/' +} + + class HelpService: @classmethod def extract_citations(cls, *, citations: list) -> list: @@ -42,3 +52,41 @@ def get_suggestions(cls, *, user_query: str) -> str: user_query=user_query ) return llm.get_openai_response(system_prompt=formatted_prompt) + + +class ActionService: + + @classmethod + def _get_action_function_from_code(cls, *, code: str) -> Callable: + code_to_function_map = { + "trigger_export": cls.trigger_export + } + return code_to_function_map[code] + + @classmethod + def get_headers(cls, *, access_token: str) -> Dict: + return { + "Authorization": f"Bearer {access_token}", + "Content-Type": "application/json" + } + + @classmethod + def get_access_token(cls, *, workspace_id: int) -> str: + creds = FyleCredential.objects.get(workspace_id=workspace_id) + return get_access_token(creds.refresh_token) + + @classmethod + def trigger_export(cls, *, workspace_id: int): + access_token = cls.get_access_token(workspace_id=workspace_id) + headers = cls.get_headers(access_token=access_token) + headers = { + "Authorization": f"Bearer {access_token}", + "Content-Type": "application/json" + } + url = 'http://localhost:8000/api/workspaces/2/trigger_export/' + action_response = requests.post(url, json={}, headers=headers) + + @classmethod + def action(cls, *, code: str, workspace_id: str): + action_function = cls._get_action_function_from_code(code=code) + action_function(workspace_id=workspace_id) diff --git a/apps/spotlight/views.py b/apps/spotlight/views.py index 0be187b..4d61b0c 100644 --- a/apps/spotlight/views.py +++ b/apps/spotlight/views.py @@ -8,7 +8,7 @@ from apps.spotlight.models import Query from apps.spotlight.serializers import QuerySerializer -from .service import HelpService, QueryService +from .service import ActionService, HelpService, QueryService from apps.workspaces.models import FyleCredential from apps.fyle.helpers import get_access_token @@ -91,19 +91,10 @@ def post(self, request, *args, **kwargs): workspace_id = self.kwargs.get('workspace_id') payload = json.loads(request.body) code = payload["code"] - - creds = FyleCredential.objects.get(workspace_id=workspace_id) - - access_token = get_access_token(creds.refresh_token) try: - headers = { - "Authorization": f"Bearer {access_token}", - "Content-Type": "application/json" - } - action_response = requests.post(code_action_map[code], json={}, headers=headers) - print(action_response) + ActionService.action(code=code, workspace_id=workspace_id) + return JsonResponse(data={"message": "Action triggered successfully"}, status_code=200) except Exception as e: print(e) - - return JsonResponse(data={"message": "Action triggered successfully"}) \ No newline at end of file + return JsonResponse(data={"message": "Action failed"}, status_code=500)