Skip to content

Commit

Permalink
Refactor into POST
Browse files Browse the repository at this point in the history
  • Loading branch information
KastanDay committed Apr 2, 2024
1 parent e4b5617 commit 32cf087
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
44 changes: 24 additions & 20 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,30 @@ def get_hook(self, name: str, api_key: str = ""):
raise Exception('No nodes found in the workflow')

def format_data(self, inputted, api_key: str, workflow_name):
work_flow = self.get_workflows(100, api_key=api_key, workflow_name=workflow_name)
print("Got workflow")
values = []
if isinstance(work_flow, dict) and 'nodes' in work_flow:
for node in work_flow['nodes']:
if node['name'] == 'n8n Form Trigger':
values = node['parameters']['formFields']['values']
data = {}
inputted = json.loads(inputted)
inputted = dict(inputted)
for i, value in enumerate(values):
field_name = 'field-' + str(i)
data[value['fieldLabel']] = field_name
new_data = {}
for k, v in inputted.items():
if isinstance(v, list):
new_data[data[k]] = json.dumps(v)
else:
new_data[data[k]] = v
return new_data
try:
work_flow = self.get_workflows(100, api_key=api_key, workflow_name=workflow_name)
print("Got workflow")
values = []
if isinstance(work_flow, dict) and 'nodes' in work_flow:
for node in work_flow['nodes']:
if node['name'] == 'n8n Form Trigger':
values = node['parameters']['formFields']['values']
data = {}
# Check if inputted is already a dict, if not, try to load it as JSON
if not isinstance(inputted, dict):
inputted = json.loads(inputted)
for i, value in enumerate(values):
field_name = 'field-' + str(i)
data[value['fieldLabel']] = field_name
new_data = {}
for k, v in inputted.items():
if isinstance(v, list):
new_data[data[k]] = json.dumps(v)
else:
new_data[data[k]] = v
return new_data
except Exception as e:
print("Error in format_data: ", e)

# TODO: activate and disactivate workflows

Expand Down
14 changes: 7 additions & 7 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from flask_cors import CORS
from flask_executor import Executor
from posthog import Posthog
import ray
# import ray
import sentry_sdk

from ai_ta_backend.canvas import CanvasAPI
Expand Down Expand Up @@ -45,7 +45,7 @@
# load API keys from globally-availabe .env file
load_dotenv()

ray.init()
# ray.init()

print("NUM ACTIVE THREADS (top of main):", threading.active_count())

Expand Down Expand Up @@ -764,17 +764,17 @@ def switch_workflow() -> Response:
abort(400, description=f"Bad request: {e}")


@app.route('/run_flow', methods=['GET'])
@app.route('/run_flow', methods=['POST'])
def run_flow() -> Response:
"""
Run flow for a user and return results.
"""

api_key = request.args.get('api_key', default='', type=str)
name = request.args.get('name', default='', type=str)
data = request.args.get('data', default='', type=str)
api_key = request.json.get('api_key', '')
name = request.json.get('name', '')
data = request.json.get('data', '')

print(request.args)
print("Got /run_flow request:", request.json)

if api_key == '':
# proper web error "400 Bad request"
Expand Down

0 comments on commit 32cf087

Please sign in to comment.