Skip to content

Commit

Permalink
added an active paramater to getting workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Mar 6, 2024
1 parent 2aa8bc3 commit 7192f21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
27 changes: 19 additions & 8 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'])
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 7192f21

Please sign in to comment.