setup controller
This commit is contained in:
40
desktop_env/controllers/setup.py
Normal file
40
desktop_env/controllers/setup.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
class SetupController:
|
||||
def __init__(self, http_server: str):
|
||||
self.http_server = http_server + "/setup"
|
||||
|
||||
def setup(self, config):
|
||||
"""
|
||||
Setup Config:
|
||||
{
|
||||
download: list[tuple[string]], # a list of tuples of url of file to download and the save path
|
||||
...
|
||||
}
|
||||
"""
|
||||
self._download_setup(config)
|
||||
# can add other setup steps
|
||||
|
||||
|
||||
def _download_setup(self, config):
|
||||
if not 'download' in config:
|
||||
return
|
||||
for url, path in config['download']:
|
||||
if not url or not path:
|
||||
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
|
||||
|
||||
payload = json.dumps({"url": url, "path": path})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
# send request to server to download file
|
||||
try:
|
||||
response = requests.post(self.http_server + "/download_file", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
else:
|
||||
print("Failed to download file. Status code:", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
Reference in New Issue
Block a user