syncthing-monitor/syncthing_monitor/syncthing_rest.py

57 lines
1.9 KiB
Python

import json
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_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(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 add_devices(self, device_ids):
syncthing_headers = {'X-API-Key': self.key}
for device_id in device_ids:
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("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))