#204: Added file upload baseline to app creator

This commit is contained in:
frikky
2021-01-18 16:09:01 +01:00
parent 79b6c6444c
commit 5338bf2485
4 changed files with 78 additions and 14 deletions
+5 -3
View File
@@ -397,7 +397,7 @@ class AppBase:
} }
ret1 = requests.get("%s%s" % (self.url, get_path), headers=headers) ret1 = requests.get("%s%s" % (self.url, get_path), headers=headers)
print("RET1: %s" % ret1.text) print("RET1 (file get): %s" % ret1.text)
if ret1.status_code != 200: if ret1.status_code != 200:
returns.append({ returns.append({
"filename": "", "filename": "",
@@ -408,7 +408,7 @@ class AppBase:
content_path = "/api/v1/files/%s/content?execution_id=%s" % (item, full_execution["execution_id"]) content_path = "/api/v1/files/%s/content?execution_id=%s" % (item, full_execution["execution_id"])
ret2 = requests.get("%s%s" % (self.url, content_path), headers=headers) ret2 = requests.get("%s%s" % (self.url, content_path), headers=headers)
print("Ret2: %s" % ret2.text) print("RET2 (file get): %s" % ret2.text)
if ret2.status_code == 200: if ret2.status_code == 200:
tmpdata = ret1.json() tmpdata = ret1.json()
returndata = { returndata = {
@@ -418,6 +418,8 @@ class AppBase:
} }
returns.append(returndata) returns.append(returndata)
print("RET3 (file get done)")
if len(returns) == 0: if len(returns) == 0:
return { return {
"success": False, "success": False,
@@ -1687,7 +1689,7 @@ class AppBase:
print("[INFO] APP_SDK DONE: Starting NORMAL execution of function") print("[INFO] APP_SDK DONE: Starting NORMAL execution of function")
print("[INFO] Running with params (0): %s" % params) print("[INFO] Running with params (0): %s" % params)
newres = await func(**params) newres = await func(**params)
print("[INFO] Returned from execution.") print("[INFO] Returned from execution:", newres)
if isinstance(newres, tuple): if isinstance(newres, tuple):
print("[INFO] Handling return as tuple") print("[INFO] Handling return as tuple")
# Handles files. # Handles files.
+20 -5
View File
@@ -365,6 +365,16 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
preparedHeaders += "}" preparedHeaders += "}"
} }
fileBalance := ""
fileAdder := ``
fileGrabber := ``
if method == "post" && strings.Contains(functionname, "filescan") {
fileGrabber = `filedata = self.get_file("63264006-5958-451a-bff1-1975495fb4d8")`
//fileGrabber += "\n print(filedata)"
fileAdder = `files = {"file": (filedata["filename"], filedata["data"])}`
fileBalance = ", files=files"
}
// Extra param for url if it's changeable // Extra param for url if it's changeable
// Extra param for authentication scheme(s) // Extra param for authentication scheme(s)
// The last weird one is the body.. Tabs & spaces sucks. // The last weird one is the body.. Tabs & spaces sucks.
@@ -375,7 +385,9 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
%s %s
%s %s
%s %s
return requests.%s(url, headers=headers%s%s%s).text %s
%s
return requests.%s(url, headers=headers%s%s%s%s).text
`, `,
functionname, functionname,
authenticationParameter, authenticationParameter,
@@ -391,17 +403,20 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
authenticationSetup, authenticationSetup,
queryData, queryData,
bodyFormatter, bodyFormatter,
fileGrabber,
fileAdder,
method, method,
authenticationAddin, authenticationAddin,
bodyAddin, bodyAddin,
verifyAddin, verifyAddin,
fileBalance,
) )
//log.Printf("FUNCTION: %s", data) //log.Printf("FUNCTION: %s", data)
//if strings.Contains(functionname, "search") { if strings.Contains(functionname, "filescan") {
// log.Println(data) log.Println(data)
// log.Printf("Queries: %s", queryString) log.Printf("Queries: %s", queryString)
//} }
//log.Printf(data) //log.Printf(data)
return functionname, data return functionname, data
+43
View File
@@ -226,6 +226,7 @@ const AppCreator = (props) => {
const [errorCode, setErrorCode] = useState("") const [errorCode, setErrorCode] = useState("")
const [appBuilding, setAppBuilding] = useState(false) const [appBuilding, setAppBuilding] = useState(false)
const [extraBodyFields, setExtraBodyFields] = useState([]) const [extraBodyFields, setExtraBodyFields] = useState([])
const [fileUploadEnabled, setFileUploadEnabled] = useState(false)
//const [actions, setActions] = useState([{ //const [actions, setActions] = useState([{
// "name": "Get workflows", // "name": "Get workflows",
@@ -254,6 +255,7 @@ const AppCreator = (props) => {
const [currentActionMethod, setCurrentActionMethod] = useState(actionNonBodyRequest[0]) const [currentActionMethod, setCurrentActionMethod] = useState(actionNonBodyRequest[0])
const [currentAction, setCurrentAction] = useState({ const [currentAction, setCurrentAction] = useState({
"name": "", "name": "",
"file_field": "",
"description": "", "description": "",
"url": "", "url": "",
"headers": "", "headers": "",
@@ -494,6 +496,7 @@ const AppCreator = (props) => {
"name": tmpname, "name": tmpname,
"description": methodvalue.description, "description": methodvalue.description,
"url": path, "url": path,
"file_field": "",
"method": method.toUpperCase(), "method": method.toUpperCase(),
"headers": "", "headers": "",
"queries": [], "queries": [],
@@ -959,6 +962,11 @@ const AppCreator = (props) => {
data.paths[item.url][item.method.toLowerCase()].parameters.push(newitem) data.paths[item.url][item.method.toLowerCase()].parameters.push(newitem)
} }
// https://swagger.io/docs/specification/describing-request-body/file-upload/
if (item.file_field !== undefined && item.file_field !== null && item.file_field.length > 0) {
console.log("HANDLE FILEFIELD SAVE: ", item.file_field)
}
if (item.headers.length > 0) { if (item.headers.length > 0) {
const required = false const required = false
@@ -1171,6 +1179,7 @@ const AppCreator = (props) => {
"name": "", "name": "",
"description": "", "description": "",
"url": "", "url": "",
"file_field": "",
"headers": "", "headers": "",
"paths": [], "paths": [],
"queries": [], "queries": [],
@@ -1609,6 +1618,7 @@ const AppCreator = (props) => {
"name": "", "name": "",
"description": "", "description": "",
"url": "", "url": "",
"file_field": "",
"headers": "", "headers": "",
"paths": [], "paths": [],
"queries": [], "queries": [],
@@ -1619,6 +1629,7 @@ const AppCreator = (props) => {
setCurrentActionMethod(apikeySelection[0]) setCurrentActionMethod(apikeySelection[0])
setUrlPathQueries([]) setUrlPathQueries([])
setActionsModalOpen(false) setActionsModalOpen(false)
setFileUploadEnabled(false)
}} }}
> >
<FormControl style={{backgroundColor: surfaceColor, color: "white",}}> <FormControl style={{backgroundColor: surfaceColor, color: "white",}}>
@@ -1806,6 +1817,36 @@ const AppCreator = (props) => {
<Button color="primary" style={{marginTop: "5px", marginBottom: "10px", borderRadius: "0px"}} variant="outlined" onClick={() => { <Button color="primary" style={{marginTop: "5px", marginBottom: "10px", borderRadius: "0px"}} variant="outlined" onClick={() => {
addPathQuery() addPathQuery()
}}>New query</Button> }}>New query</Button>
{currentActionMethod === "POST" ?
<Button color="primary" variant={fileUploadEnabled ? "contained" : "outlined"} style={{marginLeft: 10, marginTop: "5px", marginBottom: "10px", borderRadius: "0px"}} onClick={() => {
setFileUploadEnabled(!fileUploadEnabled)
if (fileUploadEnabled && currentAction["file_field"].length > 0) {
setActionField("file_field", "")
}
setUpdate(Math.random())
}}>Enable Fileupload</Button>
: null}
{fileUploadEnabled ?
<TextField
required
style={{backgroundColor: inputColor, display: "inline-block",}}
placeholder={"file"}
margin="normal"
variant="outlined"
id="standard-required"
defaultValue={currentAction["file_field"]}
onChange={e => setActionField("file_field", e.target.value)}
helperText={<span style={{color:"white", marginBottom: "2px",}}>The File field to interact with</span>}
InputProps={{
classes: {
notchedOutline: classes.notchedOutline,
},
style:{
color: "white",
},
}}
/>
: null}
<div/> <div/>
<b>Headers</b>: static for the action <b>Headers</b>: static for the action
<TextField <TextField
@@ -1848,6 +1889,7 @@ const AppCreator = (props) => {
setActionsModalOpen(false) setActionsModalOpen(false)
setUrlPathQueries([]) setUrlPathQueries([])
setUrlPath("") setUrlPath("")
setFileUploadEnabled(false)
}}> }}>
Submit Submit
</Button> </Button>
@@ -1916,6 +1958,7 @@ const AppCreator = (props) => {
"name": "", "name": "",
"description": "", "description": "",
"url": "", "url": "",
"file_field": "",
"headers": "", "headers": "",
"queries": [], "queries": [],
"paths": [], "paths": [],
+10 -6
View File
@@ -807,12 +807,16 @@ const Apps = (props) => {
const reader = new FileReader(); const reader = new FileReader();
reader.addEventListener('load', (e) => { try {
const content = e.target.result; reader.addEventListener('load', (e) => {
setOpenApiData(content); const content = e.target.result;
setIsDropzone(isDropzone); setOpenApiData(content);
setOpenApiModal(true) setIsDropzone(isDropzone);
}) setOpenApiModal(true)
})
} catch (e) {
console.log("Error in dropzone: ", e)
}
reader.readAsText(files[0]); reader.readAsText(files[0]);
}; };