Using a FastAPI app, I upload a json file. The endpoint grabs the file, uploads it to a local folder, and then executes a command on the machine to generate an html API doc using Redoc CLI.
Using the CLI directly on Git Bash, it works perfectly:
$ redoc-cli build -o mobile-switch-settings-v2.json.html mobile-switch-settings-v2.json
[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0
Prerendering docs
bundled successfully in: mobile-switch-settings-v2.json.html (1066 KiB) [⏱ 0.275s]
But when I use subprocess to execute the command using Popen, it gives me this error:
File "D:\api_converter\.\utils.py", line 38, in save_to_sftp
raise ValueError(err)
ValueError: [WinError 2] Le fichier spécifié est introuvable
meaning: specified file not found
Here is the complete code:
@app.post("/uploadfile")
async def create_upload_file(uploaded_file: UploadFile = File(...)):
file_location = f"files/{uploaded_file.filename}"
with open(file_location, "wb+") as file_object:
file_object.write(uploaded_file.file.read())
# save_to_sftp(uploaded_file)
save_to_sftp(uploaded_file)
return {"info": f"file '{uploaded_file.filename}' saved at '{file_location}'"}
Function save_to_sftp in utils.py :
try:
subprocess.Popen(['touch', 'touching'], cwd="files") # Testing subprocess, and it works!
subprocess.Popen(['redoc-cli', 'build', '-o', "mobile-switch-processing-v2.html", "mobile-switch-processing-v2.json"], cwd="files") # This command raises the exception
except Exception as err:
raise ValueError(err)
PS: The original code was this:
subprocess.Popen(['redoc-cli', 'build', '-o', f'{file.filename}.html', file.filename], cwd='files')
and then this:
subprocess.run(['redoc-cli', 'build', '-o', f'files/{file.filename}.html', "files/"+file.filename])
But I hardcoded it just to make sure I'm not doing anything wrong, and yet I get the same error.