Onionr/onionr/httpapi/configapi/__init__.py

62 lines
2.0 KiB
Python
Raw Normal View History

2019-04-27 15:43:16 +00:00
'''
Onionr - Private P2P Communication
2019-04-27 15:43:16 +00:00
This file handles configuration setting and getting from the HTTP API
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import json
from flask import Blueprint, request, Response, abort
import config, onionrutils
config.reload()
config_BP = Blueprint('config_BP', __name__)
@config_BP.route('/config/get')
def get_all_config():
'''Simply return all configuration as JSON string'''
2019-04-29 02:08:10 +00:00
return Response(json.dumps(config.get_config(), indent=4, sort_keys=True))
2019-04-27 15:43:16 +00:00
@config_BP.route('/config/get/<key>')
def get_by_key(key):
'''Return a config setting by key'''
return Response(json.dumps(config.get(key)))
@config_BP.route('/config/setall', methods=['POST'])
def set_all_config():
'''Overwrite existing JSON config with new JSON string'''
try:
2019-04-29 02:08:10 +00:00
new_config = request.get_json(force=True)
2019-04-27 15:43:16 +00:00
except json.JSONDecodeError:
abort(400)
else:
config.set_config(new_config)
2019-04-29 02:08:10 +00:00
config.save()
2019-04-27 15:43:16 +00:00
return Response('success')
@config_BP.route('/config/set/<key>', methods=['POST'])
def set_by_key(key):
'''Overwrite/set only 1 config key'''
'''
{
'data': data
}
'''
try:
data = json.loads(onionrutils.OnionrUtils.bytesToStr(request.data))['data']
except (json.JSONDecodeError, KeyError):
abort(400)
config.set(key, data, True)
return Response('success')