Skip to content

Commit

Permalink
added post request checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Mar 27, 2024
1 parent 44a8a02 commit e1b8690
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = "")

return all_users

def execute_flow(self, hook: str, api_key: str = ""):
def execute_flow(self, hook: str, api_key: str = "", post: str = ""):
if not api_key:
raise ValueError('api_key is required')
headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"}
url = hook
response = requests.get(url, headers=headers, timeout=8)
body = response.content
return body.decode('utf-8')
if post:
response = requests.post(url, headers=headers, json=post, timeout=8)
else:
response = requests.get(url, headers=headers, timeout=8)
body = response.json()
if not response.ok:
raise Exception(f"Error: {response.status_code} \n Message: {body['message']} \n Hint: {body['hint']}")
pass

def get_executions(self, limit, id=None, pagination: bool = True, api_key: str = ""):
if not api_key:
raise ValueError('api_key is required')
headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"}
url = self.url + f"/api/v1/executions?includeData=true&status=success&limit={limit}"
url = self.url + f"/api/v1/executions?includeData=true&limit={limit}"
response = requests.get(url, headers=headers, timeout=8)
executions = response.json()
if not pagination:
Expand All @@ -58,13 +63,14 @@ def get_executions(self, limit, id=None, pagination: bool = True, api_key: str =
all_executions.append(executions['data'])
cursor = executions.get('nextCursor')
while cursor is not None:
url = self.url + f'/api/v1/executions?includeData=true&status=success&limit={str(limit)}&cursor={quote(cursor)}'
url = self.url + f'/api/v1/executions?includeData=true&limit={str(limit)}&cursor={quote(cursor)}'
response = requests.get(url, headers=headers, timeout=8)
executions = response.json()
all_executions.append(executions['data'])
cursor = executions.get('nextCursor')
if id:
for execution in all_executions:
print(execution[0]['id'], ":ID:")
if execution[0]['id'] == id:
return execution

Expand Down Expand Up @@ -149,12 +155,20 @@ def get_data(self, id):
def main_flow(self, name: str, api_key: str = ""):
if not api_key:
raise ValueError('api_key is required')
print("Starting")
execution = self.get_executions(limit=1, api_key=api_key)
print("Got executions")
hookId = self.get_hook(name, api_key)
hook = self.url + f"/webhook/{hookId}"
print("Hook!!!: ", hook)

response = self.supabase_client.table('n8n_api_keys').select("*").execute()
print("Got response")
workflow = self.get_workflows(limit=100, api_key=api_key, workflow_name=name)
print("Got workflow")
print(workflow)
workflow_post = workflow['nodes'][0]['parameters'].get('httpMethod') # type: ignore
print("Got workflow post")

ids = []
for row in dict(response)['data']:
Expand All @@ -171,9 +185,9 @@ def main_flow(self, name: str, api_key: str = ""):
raise Exception('No executions found')
id = str(id)

self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute()
try:
self.execute_flow(hook, api_key)
self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute()
self.execute_flow(hook, api_key, workflow_post)
print("Executed")
executions = self.get_executions(20, id, True, api_key)
print("Got executions", executions)
Expand All @@ -182,11 +196,11 @@ def main_flow(self, name: str, api_key: str = ""):
print("Executions: ", executions)
print("Can't find id in executions")
time.sleep(1)
print("Found id in executions ")
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()
print("Deleted id")
print("Returning")
except Exception as e:
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()
return {"error": str(e)}
print("Found id in executions ")
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()
print("Deleted id")
print("Returning")
return executions

0 comments on commit e1b8690

Please sign in to comment.