Skip to content

Commit

Permalink
added post request with data sending
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Mar 28, 2024
1 parent e1b8690 commit acfb9b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 14 additions & 5 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import supabase
from urllib.parse import quote
import json


class Flows():
Expand Down Expand Up @@ -35,13 +36,16 @@ 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 = "", post: str = ""):
def execute_flow(self, hook: str, api_key: str = "", post: str = "", data={}):
if not api_key:
raise ValueError('api_key is required')
headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"}
url = hook
if post:
response = requests.post(url, headers=headers, json=post, timeout=8)
if data:
response = requests.post(url, headers=headers, json=post, timeout=8, data=data)
else:
response = requests.post(url, headers=headers, json=post, timeout=8)
else:
response = requests.get(url, headers=headers, timeout=8)
body = response.json()
Expand Down Expand Up @@ -151,8 +155,8 @@ def switch_workflow(self, id, api_key: str = "", activate: 'str' = 'True'):
def get_data(self, id):
self.get_executions(20, id)

# TODO: Make the list of flows through supabase
def main_flow(self, name: str, api_key: str = ""):
# TODO: NEED to have keyword args for workflows like Pest Detection.
def main_flow(self, name: str, api_key: str = "", data: str = ""):
if not api_key:
raise ValueError('api_key is required')
print("Starting")
Expand All @@ -161,6 +165,11 @@ def main_flow(self, name: str, api_key: str = ""):
hookId = self.get_hook(name, api_key)
hook = self.url + f"/webhook/{hookId}"
print("Hook!!!: ", hook)
print(data)
json_data = json.loads(data)
print("Data to json")
new_data = dict(json_data)
print("Got data to dictionary")

response = self.supabase_client.table('n8n_api_keys').select("*").execute()
print("Got response")
Expand All @@ -187,7 +196,7 @@ def main_flow(self, name: str, api_key: str = ""):

try:
self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute()
self.execute_flow(hook, api_key, workflow_post)
self.execute_flow(hook, api_key, workflow_post, new_data)
print("Executed")
executions = self.get_executions(20, id, True, api_key)
print("Got executions", executions)
Expand Down
3 changes: 2 additions & 1 deletion ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ def run_flow() -> Response:

api_key = request.args.get('api_key', default='', type=str)
name = request.args.get('name', default='', type=str)
data = request.args.get('data', default='', type=str)

print(request.args)

Expand All @@ -775,7 +776,7 @@ def run_flow() -> Response:

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

0 comments on commit acfb9b6

Please sign in to comment.