diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index 1b569581..b7655243 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -12,7 +12,7 @@ def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = "") raise ValueError('api_key is required') all_users = [] headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"} - url = self.url+'/api/v1/users?limit=%s&includeRole=true' % str(limit) + url = self.url + '/api/v1/users?limit=%s&includeRole=true' % str(limit) response = requests.get(url, headers=headers, timeout=8) data = response.json() if not pagination: @@ -21,8 +21,7 @@ def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = "") all_users.append(data['data']) cursor = data.get('nextCursor') while cursor is not None: - url = self.url+'/api/v1/users?limit=%s&cursor=%s&includeRole=true' % ( - str(limit), cursor) + url = self.url + '/api/v1/users?limit=%s&cursor=%s&includeRole=true' % (str(limit), cursor) response = requests.get(url, headers=headers, timeout=8) data = response.json() all_users.append(data['data']) @@ -43,7 +42,7 @@ 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&status=success&limit={limit}" response = requests.get(url, headers=headers, timeout=8) executions = response.json() if not pagination: @@ -70,14 +69,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 = ""): + # TODO: Active or Inactive + def get_workflows(self, limit, pagination: bool = True, api_key: str = "", active: bool = False): 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/workflows?limit={limit}" + url = self.url + f"/api/v1/workflows?limit={limit}" + if active: + url = url + f"&active=true" response = requests.get(url, headers=headers, timeout=8) - workflows = response.json() + response.ok + if workflows.get('message') == 'unauthorized' and not response.ok: + raise Exception('Unauthorized') if not pagination: return workflows['data'] @@ -86,13 +90,20 @@ def get_workflows(self, limit, pagination: bool = True, api_key: str = ""): all_workflows.append(workflows['data']) cursor = workflows.get('nextCursor') while cursor is not None: - url = self.url+f"/api/v1/workflows?limit={limit}&cursor={cursor}" + url = self.url + f"/api/v1/workflows?limit={limit}&cursor={cursor}" response = requests.get(url, headers=headers, timeout=8) workflows = response.json() all_workflows.append(workflows['data']) cursor = workflows.get('nextCursor') return all_workflows + # TODO: activate and disactivate workflows + + # Making this so it can be synchronous so that OpenAi API can call it. + # TODO: Status update on ID, running/done/error + # Todo: Before running, check if it is active by fetching the latest execution, increment if necessary and then run the flow. + # TODO: Create a dummy endpoint to pass to openai function call expecting n8n webhook and necessary parameters in the request. + # TODO: Take the last one/ ALWAYS end in JSON def get_data(self, id): self.get_executions(20, id) diff --git a/ai_ta_backend/main.py b/ai_ta_backend/main.py index b222d7a5..a93d81b5 100644 --- a/ai_ta_backend/main.py +++ b/ai_ta_backend/main.py @@ -709,16 +709,22 @@ def get_all_workflows() -> Response: api_key = request.args.get('api_key', default='', type=str) 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) + print(request.args) if api_key == '': # proper web error "400 Bad request" abort(400, description=f"Missing N8N API_KEY: 'api_key' must be provided. Search query: `{api_key}`") flows = Flows() - response = flows.get_workflows(limit, pagination, api_key) - response = jsonify(response) - response.headers.add('Access-Control-Allow-Origin', '*') - return response + try: + response = flows.get_workflows(limit, pagination, api_key, active) + response = jsonify(response) + response.headers.add('Access-Control-Allow-Origin', '*') + return response + except Exception as e: + if e == "Unauthorized": + abort(401, description=f"Unauthorized: 'api_key' is invalid. Search query: `{api_key}`") if __name__ == '__main__':