From e1b86903ff5b01dc3c9ffe2224663d8fb3ac90a9 Mon Sep 17 00:00:00 2001 From: jkmin3 Date: Wed, 27 Mar 2024 15:47:02 -0500 Subject: [PATCH] added post request checking --- ai_ta_backend/flows.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index c3e96502..d1dc994c 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -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: @@ -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 @@ -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']: @@ -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) @@ -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