Skip to content

Commit

Permalink
finished the api for running flows
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Mar 27, 2024
1 parent 7fa2f40 commit 5c63887
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
46 changes: 40 additions & 6 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import requests
import time
import os
import supabase


class Flows():

def __init__(self):
self.flows = []
self.url = "https://primary-production-1817.up.railway.app"
self.supabase_client = supabase.create_client( # type: ignore
supabase_url=os.environ['SUPABASE_URL'], supabase_key=os.environ['SUPABASE_API_KEY'])

def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = ""):
if not api_key:
Expand Down Expand Up @@ -70,7 +74,19 @@ def get_executions(self, limit, id=None, pagination: bool = True, api_key: str =
else:
return all_executions

def get_workflows(self, limit, pagination: bool = True, api_key: str = "", active: bool = False):
def get_hook(self, name: str, api_key: str = ""):
work_flow = self.get_workflows(1, api_key=api_key, workflow_name=name)
for node in work_flow.get('nodes'): # type: ignore
if node['name'] == 'Webhook':
return node['webhookId']
pass

def get_workflows(self,
limit,
pagination: bool = True,
api_key: str = "",
active: bool = False,
workflow_name: str = ''):
if not api_key:
raise ValueError('api_key is required')
headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"}
Expand All @@ -94,6 +110,13 @@ def get_workflows(self, limit, pagination: bool = True, api_key: str = "", activ
workflows = response.json()
all_workflows.append(workflows['data'])
cursor = workflows.get('nextCursor')

if workflow_name:
for workflow in all_workflows[0]:
if workflow['name'] == workflow_name:
return workflow
else:
raise Exception('Workflow not found')
return all_workflows

# TODO: activate and disactivate workflows
Expand Down Expand Up @@ -121,20 +144,31 @@ def switch_workflow(self, id, api_key: str = "", activate: 'str' = 'True'):
def get_data(self, id):
self.get_executions(20, id)

def main_flow(self, hook: str, api_key: str = ""):
# TODO: Make the list of flows through supabase
def main_flow(self, name: str, api_key: str = ""):
if not api_key:
raise ValueError('api_key is required')
workflows = self.get_workflows(limit=1)
if len(self.flows) > 0:
id = self.flows[-1] + 1
hookId = self.get_hook(name, api_key)
hook = self.url + f"/webhook-test/{hookId}"

response = self.supabase_client.table('n8n_api_keys').select("*").execute()

ids = []
for row in dict(response)['data']:
ids.append(row['id'])

if len(ids) > 0:
id = max(ids) + 1
else:
id = workflows[0]['id'] + 1
self.flows.append(id)

self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute()
self.execute_flow(hook)
executions = self.get_executions(20, id, True, api_key)
while id not in executions:
executions = self.get_executions(10, id, True, api_key)
time.sleep(1)
self.flows.remove(id)
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()

return self.get_executions(1, id, False, api_key)
3 changes: 2 additions & 1 deletion ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ def get_all_workflows() -> Response:
limit = request.args.get('limit', default=100, type=int)
pagination = request.args.get('pagination', default=True, type=bool)
active = request.args.get('active', default=False, type=bool)
name = request.args.get('workflow_name', default='', type=str)
print(request.args)

if api_key == '':
Expand All @@ -718,7 +719,7 @@ def get_all_workflows() -> Response:

flows = Flows()
try:
response = flows.get_workflows(limit, pagination, api_key, active)
response = flows.get_workflows(limit, pagination, api_key, active, name)
response = jsonify(response)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
Expand Down

0 comments on commit 5c63887

Please sign in to comment.