Skip to content

Commit

Permalink
action api
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyanshs7 committed Sep 12, 2024
1 parent 7ff8109 commit 70b7e49
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
50 changes: 49 additions & 1 deletion apps/spotlight/service.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
17 changes: 4 additions & 13 deletions apps/spotlight/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"})
return JsonResponse(data={"message": "Action failed"}, status_code=500)

0 comments on commit 70b7e49

Please sign in to comment.