syncthing-monitor/syncthing_monitor/syncthing_client.py

60 lines
1.8 KiB
Python
Raw Normal View History

2021-01-28 13:31:25 +00:00
import json
import requests
from retrying import retry
DEFAULT_GUI_PORT = 8384
2021-01-28 13:31:25 +00:00
class SyncthingClient:
def __init__(self, api_key, host, port=DEFAULT_GUI_PORT):
self.api_key = api_key
self.host = host
self.port = port
self.headers = {'X-API-Key': self.api_key}
@retry
def get_my_device_id(self):
response = requests.get(self.make_url("/rest/system/status"), headers=self.headers)
return json.loads(response.content)["myID"]
def add_devices(self, device_list):
for device in device_list:
self.add_device(device)
def add_device(self, device):
post_data = {
"deviceID": device['id'],
"name": device['hostname'],
"addresses": [
"dynamic",
"tcp://{0}:22000".format(device['hostname'])
],
"compression": "metadata",
"certName": "",
"introducer": False,
"skipIntroductionRemovals": False,
"introducedBy": "",
"paused": False,
"allowedNetworks": [],
"autoAcceptFolders": False,
"maxSendKbps": 0,
"maxRecvKbps": 0,
"ignoredFolders": [],
"pendingFolders": [],
"maxRequestKiB": 0
}
response = requests.post(self.make_url("/rest/config/devices"),
headers=self.headers, data=json.dumps(post_data))
print("Attempt to add device {0} to syncthing: {1}".format(device, response.content))
@retry
def print_config(self):
response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers)
print("/rest/config/devices: {0}".format(response.content))
def make_url(self, endpoint):
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)