59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
import requests
|
||
|
import json
|
||
|
import salt.exceptions
|
||
|
import xml.etree.ElementTree as ET
|
||
|
|
||
|
def get_apikey(configfile="/root/.config/syncthing/config.xml"):
|
||
|
try:
|
||
|
tree = ET.parse(configfile)
|
||
|
root = tree.getroot()
|
||
|
apikey = root.find("./gui/apikey").text
|
||
|
return apikey
|
||
|
except (FileNotFoundError,ET.ParseError,AttributeError) as e:
|
||
|
raise "Exception {0} occured".format(e)
|
||
|
return ""
|
||
|
|
||
|
def get_config(url, verify, apikey):
|
||
|
fullurl = "{0}/rest/system/config".format(url)
|
||
|
ret = dict()
|
||
|
try:
|
||
|
req = requests.request("get", fullurl, verify=verify, headers={"X-API-Key": apikey})
|
||
|
except (requests.exceptions.RequestException) as exc:
|
||
|
raise "Exception {0} occured".format(exc)
|
||
|
ret = req.json()
|
||
|
if req.status_code == 200:
|
||
|
return ret
|
||
|
return None
|
||
|
|
||
|
def set_config(url, verify, apikey, config):
|
||
|
fullurl = "{0}/rest/system/config".format(url)
|
||
|
try:
|
||
|
req = requests.request("post", fullurl, verify=verify, headers={"X-API-Key": apikey}, json=config)
|
||
|
except (requests.exceptions.RequestException) as exc:
|
||
|
raise "Exception {0} occured".format(exc)
|
||
|
if req.status_code == 200:
|
||
|
return True
|
||
|
return None
|
||
|
|
||
|
def insync(url, verify, apikey):
|
||
|
fullurl = "{0}/rest/system/config/insync".format(url)
|
||
|
try:
|
||
|
req = requests.request("get", fullurl, verify=verify, headers={"X-API-Key": apikey})
|
||
|
except (requests.exceptions.RequestException) as exc:
|
||
|
raise "Exception {0} occured".format(exc)
|
||
|
ret = req.json()
|
||
|
if req.status_code == 200:
|
||
|
return ret
|
||
|
return None
|
||
|
|
||
|
def restart(url, verify, apikey):
|
||
|
fullurl = "{0}/rest/system/restart".format(url)
|
||
|
try:
|
||
|
req = requests.post(fullurl, verify=verify, headers={"X-API-Key": apikey})
|
||
|
except (requests.exceptions.RequestException) as exc:
|
||
|
raise "Exception {0} occured".format(exc)
|
||
|
if req.status_code == 200:
|
||
|
return {}
|
||
|
return None
|