How to change the configuration of Syncthing service by using the REST API via Python — REST API…

Written by esergozcu | Published 2018/11/09
Tech Story Tags: programming | rest-api | python | syncthing-service | rest-api-via-python

TLDRvia the TL;DR App

I have seen so many job advertisements that are asking for a REST API knowledge or the relevant experience. Therefore I thought it will be useful to prepare a tutorial at least to being with. Let’s use the REST API of Syncthing.

Note: Ultimate purpose at this article is to show you how to make basic REST API calls. Provided Python code is far away from being a production ready quality. No error handling, no function definitions, no object orienteed code whatsoever.

Syncthing is my favorite file synchronization program and it does have REST API to pull or push config values.

Following are the steps one can follow/.

1 — Check if the service you intent to interact with has a REST API.

Syncthing indeed has the REST API support for developers. Awesome!

3.10. REST API - Syncthing v0.14 documentation_To use the REST API an API key must be set and used. The API key can be generated in the GUI, or set in the…_docs.syncthing.net

2 — Check what kind of end points REST API exposes for you, and if any of those is an interest to you.

After checking the above link, I see tremendous possibilities to practice/learn REST API.

3 — Let’s start with pulling the configuration values of Syncthing via REST API.

GET /rest/system/config - Syncthing v0.14 documentation_Edit description_docs.syncthing.net

Great! I can use /rest/system/config to get the configuration values of my Syncthing Instance :)

Below is the initial code with line by line explanation.

Great we have the fundementals of the REST API usage in Python. Basically what we did was to check documentation of Syncthing to figure out the end points and the authentication method. Then it was a matter of selecting an end point to play with.

Let’s go one step further, what if not only I want to print the results to the terminal or save the config as .json file but also change a configuration option of our Syncthing instance.

Digging into Syncthing documentation shows me that POST function can be used for /rest/system/config (same end point) to push full configuration in the same format we had by using GET before. Doing a little bit more googling reveals that not all configuration options are tied to Syncthing restart when modified. Here are the details of the configuration options and whether they require service restart or not.

syncthing/syncthing_Open Source Continuous File Synchronization. Contribute to syncthing/syncthing development by creating an account on…_github.com

Okay, let’s try to change the MAxRecvKbps, this option is to determine limit of the file download speed by Sycnthing. It takes an integer value and does not require a restart after a change.

MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`

Syncthing GUI → Actions → Options → Max Recv Kbps can be used if you want to edit this value manually.

Remember, previous code created a config file named config.json

5 — Let’s duplicate that file and rename the new one to config_edited.json There is a line where you can see the defined setting for Max Recv Kbps. Let’s change the value manually to 4000 which is equvailent of 4 Mbps.

6 — Above small piece of code will push the config_edited.json as new configuration for our Syncthing instance.

And Syncthing GUI → Actions → Options → Max Recv Kbps also confirms that the result is a success.

I hope this was article helpful. In this article we learned how to check documentation of a service with a REST API support. We also used python to pull and push data to our service. If you are also interested in doing same without writing a single line of code, check postman :)


Published by HackerNoon on 2018/11/09