From acfb9b6dc75cb554cb5bbb73a954bbdc84d26750 Mon Sep 17 00:00:00 2001 From: jkmin3 Date: Wed, 27 Mar 2024 23:41:13 -0500 Subject: [PATCH] added post request with data sending --- ai_ta_backend/flows.py | 19 ++++++++++++++----- ai_ta_backend/main.py | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index d1dc994c..85930650 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -3,6 +3,7 @@ import os import supabase from urllib.parse import quote +import json class Flows(): @@ -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() @@ -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") @@ -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") @@ -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) diff --git a/ai_ta_backend/main.py b/ai_ta_backend/main.py index 7bcf0b20..f9c052cc 100644 --- a/ai_ta_backend/main.py +++ b/ai_ta_backend/main.py @@ -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) @@ -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