2021-01-28 13:31:25 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from retrying import retry
|
|
|
|
|
|
|
|
|
2021-01-29 10:13:59 +00:00
|
|
|
class SyncthingClient:
|
|
|
|
|
2021-01-29 13:55:51 +00:00
|
|
|
def __init__(self, api_key, host, port):
|
2021-01-29 10:35:18 +00:00
|
|
|
self.api_key = api_key
|
2021-01-29 10:13:59 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2021-01-29 10:35:18 +00:00
|
|
|
self.headers = {'X-API-Key': self.api_key}
|
2021-01-29 10:13:59 +00:00
|
|
|
|
|
|
|
@retry
|
|
|
|
def get_my_device_id(self):
|
2021-01-29 10:35:18 +00:00
|
|
|
response = requests.get(self.make_url("/rest/system/status"), headers=self.headers)
|
2021-01-29 10:13:59 +00:00
|
|
|
return json.loads(response.content)["myID"]
|
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
def add_devices(self, device_list):
|
|
|
|
for device in device_list:
|
|
|
|
self.add_device(device)
|
2021-01-29 10:35:18 +00:00
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
def add_device(self, device):
|
2021-01-29 10:35:18 +00:00
|
|
|
post_data = {
|
2021-01-29 12:56:07 +00:00
|
|
|
"deviceID": device['id'],
|
|
|
|
"name": device['hostname'],
|
2021-01-29 10:35:18 +00:00
|
|
|
"addresses": [
|
|
|
|
"dynamic",
|
2021-01-29 12:56:07 +00:00
|
|
|
"tcp://{0}:22000".format(device['hostname'])
|
2021-01-29 10:35:18 +00:00
|
|
|
],
|
|
|
|
"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))
|
|
|
|
|
2021-01-29 12:56:07 +00:00
|
|
|
print("Attempt to add device {0} to syncthing: {1}".format(device, response.content))
|
2021-01-29 10:35:18 +00:00
|
|
|
|
2021-01-29 10:13:59 +00:00
|
|
|
@retry
|
2021-01-29 10:35:18 +00:00
|
|
|
def print_config(self):
|
|
|
|
response = requests.get(self.make_url("/rest/config/devices"), headers=self.headers)
|
2021-01-29 10:13:59 +00:00
|
|
|
print("/rest/config/devices: {0}".format(response.content))
|
|
|
|
|
2021-01-29 10:35:18 +00:00
|
|
|
def make_url(self, endpoint):
|
|
|
|
return "http://{0}:{1}{2}".format(self.host, self.port, endpoint)
|