diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index 4f90803a..484850b8 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -1,4 +1,5 @@ import requests +import time class Flows(): @@ -69,7 +70,6 @@ def get_executions(self, limit, id=None, pagination: bool = True, api_key: str = else: return all_executions - # 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') @@ -98,6 +98,20 @@ def get_workflows(self, limit, pagination: bool = True, api_key: str = "", activ # TODO: activate and disactivate workflows + def switch_workflow(self, id, api_key: str = "", activate: 'str' = 'True'): + if not api_key: + raise ValueError('api_key is required') + headers = {"X-N8N-API-KEY": api_key, "Accept": "application/json"} + if activate == "True" or activate == "true": + url = self.url + f"/api/v1/workflows/{id}/activate" + else: + url = self.url + f"/api/v1/workflows/{id}/deactivate" + response = requests.post(url, headers=headers, timeout=8) + result = response.json() + # if result.get('message'): + # raise Exception(result.get('message')) + return result + # 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. @@ -110,12 +124,17 @@ def get_data(self, id): def main_flow(self, hook: str, api_key: str = ""): if not api_key: raise ValueError('api_key is required') - workflows = self.get_workflows(limit=20) - id = workflows[0]['id'] + 1 + workflows = self.get_workflows(limit=1) + if len(self.flows) > 0: + id = self.flows[-1] + 1 + else: + id = workflows[0]['id'] + 1 self.flows.append(id) self.execute_flow(hook) executions = self.get_executions(20, id, True, api_key) while id not in executions: - executions = self.get_executions(20, id, True, api_key) + executions = self.get_executions(10, id, True, api_key) + time.sleep(1) + self.flows.remove(id) - return self.get_data(id) + return self.get_executions(1, id, False, api_key) diff --git a/ai_ta_backend/main.py b/ai_ta_backend/main.py index a93d81b5..4e7e9e01 100644 --- a/ai_ta_backend/main.py +++ b/ai_ta_backend/main.py @@ -727,5 +727,35 @@ def get_all_workflows() -> Response: abort(401, description=f"Unauthorized: 'api_key' is invalid. Search query: `{api_key}`") +@app.route('/switch_workflow', methods=['GET']) +def switch_workflow() -> Response: + """ + Get all workflows from user. + """ + + api_key = request.args.get('api_key', default='', type=str) + activate = request.args.get('activate', default='', type=str) + id = request.args.get('id', default='', type=str) + + 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() + try: + print("activation!!!!!!!!!!!", activate) + response = flows.switch_workflow(id, api_key, activate) + 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}`") + else: + abort(400, description=f"Bad request: {e}") + + if __name__ == '__main__': app.run(debug=True, port=int(os.getenv("PORT", default=8000))) # nosec -- reasonable bandit error suppression