syncthing-monitor/syncthing_monitor/syncthing_client.py

60 lines
1.8 KiB
Python

import json
import requests
from retrying import retry
DEFAULT_GUI_PORT = 8384
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_ids):
for device_id in device_ids:
self.add_device(device_id)
def add_device(self, device_id):
post_data = {
"deviceID": device_id,
"name": "Laptop",
"addresses": [
"dynamic",
"tcp://192.168.1.2:22000"
],
"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_id, 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)