Skip to content

Commit

Permalink
updates to activation of workflows and have mainflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Mar 21, 2024
1 parent 7793fc4 commit 7fa2f40
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
29 changes: 24 additions & 5 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import time


class Flows():
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand All @@ -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)
30 changes: 30 additions & 0 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7fa2f40

Please sign in to comment.