mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
Add working GET /config to the API
This commit is contained in:
parent
2a954830ce
commit
25a1e3b495
|
@ -137,15 +137,14 @@ class CONFIG:
|
||||||
self.log.error("[CFG] reading logfile", e=conferror)
|
self.log.error("[CFG] reading logfile", e=conferror)
|
||||||
|
|
||||||
|
|
||||||
def read_config(self):
|
def read(self):
|
||||||
"""
|
"""
|
||||||
read config file
|
read config file
|
||||||
"""
|
"""
|
||||||
if self.config_exists():
|
if not self.config_exists():
|
||||||
#print(self.config.read(self.config_name))
|
return False
|
||||||
#print(self.config.sections())
|
|
||||||
|
|
||||||
return self.config
|
return {s:dict(self.config.items(s)) for s in self.config.sections()}
|
||||||
|
|
||||||
def get(self, area, key, default):
|
def get(self, area, key, default):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,20 +1,28 @@
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from flask_sock import Sock
|
from flask_sock import Sock
|
||||||
import argparse, json
|
import os
|
||||||
|
import json
|
||||||
from config import CONFIG
|
from config import CONFIG
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
sock = Sock(app)
|
sock = Sock(app)
|
||||||
|
|
||||||
# CLI arguments
|
# set config file to use
|
||||||
def get_config_filename_from_args():
|
def set_config():
|
||||||
parser = argparse.ArgumentParser()
|
if 'FREEDATA_CONFIG' in os.environ:
|
||||||
parser.add_argument("-c", "--config", help = "Specifiy config file", default = 'config.ini')
|
config_file = os.environ['FREEDATA_CONFIG']
|
||||||
args, unknown = parser.parse_known_args()
|
else:
|
||||||
return args.config
|
config_file = 'config.ini'
|
||||||
|
|
||||||
config = CONFIG(get_config_filename_from_args())
|
if os.path.exists(config_file):
|
||||||
|
print("Using config from %s" % config_file)
|
||||||
|
else:
|
||||||
|
print("Config file '%s' not found. Exiting." % config_file)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
app.config_manager = CONFIG(config_file)
|
||||||
|
|
||||||
|
# returns a standard API response
|
||||||
def api_response(data, status = 'ok'):
|
def api_response(data, status = 'ok'):
|
||||||
response = {
|
response = {
|
||||||
'status': status,
|
'status': status,
|
||||||
|
@ -22,10 +30,12 @@ def api_response(data, status = 'ok'):
|
||||||
}
|
}
|
||||||
return jsonify(response)
|
return jsonify(response)
|
||||||
|
|
||||||
|
set_config()
|
||||||
|
|
||||||
## REST API
|
## REST API
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
return jsonify({'name': 'FreeDATA API',
|
return api_response({'name': 'FreeDATA API',
|
||||||
'description': '',
|
'description': '',
|
||||||
'api_version': 1,
|
'api_version': 1,
|
||||||
'license': 'GPL3.0',
|
'license': 'GPL3.0',
|
||||||
|
@ -44,28 +54,8 @@ def config():
|
||||||
response = api_response(set_config)
|
response = api_response(set_config)
|
||||||
return response
|
return response
|
||||||
elif request.method == 'GET':
|
elif request.method == 'GET':
|
||||||
return api_response(config.read())
|
return api_response(app.config_manager.read())
|
||||||
|
|
||||||
# get activity
|
|
||||||
@app.route('/activity', methods=['GET'])
|
|
||||||
def activity():
|
|
||||||
return "Not implemented yet."
|
|
||||||
|
|
||||||
# get received messages
|
|
||||||
@app.route('/messages', methods=['GET'])
|
|
||||||
def messages():
|
|
||||||
return "Not implemented yet."
|
|
||||||
|
|
||||||
# new message / delete message
|
|
||||||
""" @app.route('/message', methods=['POST', 'DELETE'])
|
|
||||||
def message():
|
|
||||||
if request.method == 'POST':
|
|
||||||
message = new Message(request.form['message'])
|
|
||||||
status = modem.send_message(message)
|
|
||||||
elif request.method == 'DELETE':
|
|
||||||
status = messageDb.deleteMessage(request.form['id'])
|
|
||||||
return status
|
|
||||||
"""
|
|
||||||
# Event websocket
|
# Event websocket
|
||||||
@sock.route('/events')
|
@sock.route('/events')
|
||||||
def echo(sock):
|
def echo(sock):
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import unittest
|
import unittest
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
import shlex
|
import shlex, os
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
import json
|
|
||||||
|
|
||||||
class TestConfigAPI(unittest.TestCase):
|
# API Server integration testst
|
||||||
|
class TestIntegration(unittest.TestCase):
|
||||||
|
|
||||||
process = None
|
process = None
|
||||||
url = "http://127.0.0.1:5000"
|
url = "http://127.0.0.1:5000"
|
||||||
|
@ -13,22 +13,39 @@ class TestConfigAPI(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cmd = "flask --app modem/server run"
|
cmd = "flask --app modem/server run"
|
||||||
cls.process = Popen(shlex.split(cmd), stdin=PIPE)
|
my_env = os.environ.copy()
|
||||||
|
my_env["FREEDATA_CONFIG"] = "modem/config.ini"
|
||||||
|
cls.process = Popen(shlex.split(cmd), stdin=PIPE, env=my_env)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
cls.process.stdin.close()
|
cls.process.stdin.close()
|
||||||
cls.process.terminate()
|
cls.process.terminate()
|
||||||
if cls.process.wait() != 0:
|
cls.process.wait()
|
||||||
print("There were some errors closing the process.")
|
|
||||||
|
|
||||||
def test_index(self):
|
def test_index(self):
|
||||||
r = requests.get(self.url)
|
r = requests.get(self.url)
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
data = r.json()
|
data = r.json()
|
||||||
self.assertEqual(data['api_version'], 1)
|
self.assertEqual(data['data']['api_version'], 1)
|
||||||
|
|
||||||
|
def test_config(self):
|
||||||
|
r = requests.get(self.url + '/config')
|
||||||
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
payload = r.json()
|
||||||
|
self.assertIn('data', payload)
|
||||||
|
self.assertIn('status', payload)
|
||||||
|
|
||||||
|
config = payload['data']
|
||||||
|
self.assertIsInstance(config, dict)
|
||||||
|
|
||||||
|
self.assertIn('NETWORK', config)
|
||||||
|
self.assertIn('STATION', config)
|
||||||
|
self.assertIn('AUDIO', config)
|
||||||
|
self.assertIn('Modem', config)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
2
tools/run-server.sh
Executable file
2
tools/run-server.sh
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
flask --app modem/server run
|
||||||
|
|
1
tools/run-tests.sh
Executable file
1
tools/run-tests.sh
Executable file
|
@ -0,0 +1 @@
|
||||||
|
python -m unittest discover tests
|
Loading…
Reference in a new issue