refactored syncthing client methods into SyncthingClient class

This commit is contained in:
B.J. Dweck 2021-01-29 12:13:59 +02:00
parent 3589064dd0
commit 4c45a7d72b
2 changed files with 56 additions and 46 deletions

View File

@ -1,21 +1,24 @@
from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any
import syncthing_monitor.config_xml as xml
from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list
from syncthing_monitor.syncthing_rest import get_device_id, print_debug_info, post_devices
from syncthing_monitor.syncthing_rest import SyncthingClient
DEFAULT_CONFIG_XML_PATH = '/config/config.xml'
def loop(gui_port="8384", host="sync"):
api_key = parse_api_key()
api_key = xml.parse_api_key(DEFAULT_CONFIG_XML_PATH)
print("Found API Key: {0}".format(api_key))
set_listen_ip_to_any()
xml.set_listen_ip_to_any(DEFAULT_CONFIG_XML_PATH, DEFAULT_CONFIG_XML_PATH, 8384)
device_id = get_device_id(host, gui_port, api_key)
print("Found Device ID: {0}".format(device_id))
syncthing = SyncthingClient(host, gui_port, api_key)
my_device_id = syncthing.get_my_device_id()
print("Found Device ID: {0}".format(my_device_id))
append_device_id(device_id, 'etcd')
append_device_id(my_device_id, 'etcd')
device_ids = get_device_list('etcd')
post_devices(device_ids, host, gui_port, api_key)
print_debug_info(host, gui_port, api_key)
syncthing.post_devices(device_ids)
syncthing.print_debug_info()
if __name__ == "__main__":

View File

@ -4,22 +4,29 @@ import requests
from retrying import retry
class SyncthingClient:
def __init__(self, host, port, key):
self.host = host
self.port = port
self.key = key
@retry
def get_device_id(host, gui_port, api_key):
syncthing_headers = {'X-API-Key': api_key}
response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers)
def get_my_device_id(self):
syncthing_headers = {'X-API-Key': self.key}
response = requests.get("http://" + self.host + ":" + self.port + "/rest/system/status",
headers=syncthing_headers)
return json.loads(response.content)["myID"]
@retry
def print_debug_info(host, gui_port, api_key):
syncthing_headers = {'X-API-Key': api_key}
response = requests.get("http://" + host + ":" + gui_port + "/rest/config/devices", headers=syncthing_headers)
def print_debug_info(self):
syncthing_headers = {'X-API-Key': self.key}
response = requests.get("http://" + self.host + ":" + self.port + "/rest/config/devices",
headers=syncthing_headers)
print("/rest/config/devices: {0}".format(response.content))
def post_devices(device_ids, host, gui_port, api_key):
syncthing_headers = {'X-API-Key': api_key}
def post_devices(self, device_ids):
syncthing_headers = {'X-API-Key': self.key}
for device_id in device_ids:
post_data = {
@ -44,6 +51,6 @@ def post_devices(device_ids, host, gui_port, api_key):
"maxRequestKiB": 0
}
response = requests.post("http://" + host + ":" + gui_port + "/rest/config/devices",
response = requests.post("http://" + self.host + ":" + self.port + "/rest/config/devices",
headers=syncthing_headers, data=json.dumps(post_data))
print("Attempt to add device {0} to syncthing: {1}".format(device_id, response.content))