From 2d3428307ef4fe8a0473b102f8328cae483fbd82 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 13 Jun 2023 12:49:41 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(constants.tsx):=20remov?= =?UTF-8?q?e=20flowId=20from=20API=5FURL=20and=20add=20it=20to=20the=20hea?= =?UTF-8?q?ders=20as=20an=20Authorization=20token=20The=20flowId=20is=20no?= =?UTF-8?q?w=20passed=20as=20an=20Authorization=20token=20in=20the=20heade?= =?UTF-8?q?rs=20of=20the=20API=20request.=20This=20improves=20security=20a?= =?UTF-8?q?s=20the=20flowId=20is=20not=20exposed=20in=20the=20URL.=20The?= =?UTF-8?q?=20API=5FURL=20now=20only=20contains=20the=20base=20URL=20witho?= =?UTF-8?q?ut=20the=20flowId.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/constants.tsx | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/frontend/src/constants.tsx b/src/frontend/src/constants.tsx index 82d256c6e..9671ab5c2 100644 --- a/src/frontend/src/constants.tsx +++ b/src/frontend/src/constants.tsx @@ -46,15 +46,28 @@ export const TEXT_DIALOG_SUBTITLE = "Edit you text."; export const getPythonApiCode = (flowId: string): string => { return `import requests - FLOW_ID = "${flowId}" - API_URL = f"${window.location.protocol}//${window.location.host}/predict/{FLOW_ID}" +FLOW_ID = "${flowId}" +API_URL = f"${window.location.protocol}//${window.location.host}/predict" - def predict(message): - payload = {'message': message} - response = requests.post(API_URL, json=payload) - return response.json() +def run_flow(message, tweaks=None): - print(predict("Your message"))`; + if tweaks: + payload = {'message': message, 'tweaks': tweaks} + else: + payload = {'message': message} + + headers = {'Authorization': + f'Bearer {FLOW_ID}', + 'Content-Type': 'application/json' + } + + response = requests.post(API_URL, json=payload) + return response.json() + +# Setup any tweaks you want to apply to the flow +tweaks = {} # {"nodeId": {"key": "value"}, "nodeId2": {"key": "value"}} + +print(run_flow("Your message", tweaks=tweaks))`; }; /** @@ -65,8 +78,9 @@ export const getPythonApiCode = (flowId: string): string => { export const getCurlCode = (flowId: string): string => { return `curl -X POST \\ -H "Content-Type: application/json" \\ + -H "Authorization: Bearer ${flowId}" \\ -d '{"message": "Your message"}' \\ - ${window.location.protocol}//${window.location.host}/predict/${flowId}`; + ${window.location.protocol}//${window.location.host}/predict`; }; /**