Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""API server, with a web client for free"""
# System modules
# Third-party modules
# Custom modules
from api import api_blueprint as api
app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api')
Davide Ricci
committed
@app.route('/all/<string:namespace>')
def get_status(namespace):
Davide Ricci
committed
endpoints = {}
# Check for ping before going to all others
power = f"/api/{namespace}/ping"
with app.test_client() as client:
res = client.get(power)
name = "-".join(power.split("/")[2:]) # "dome-shutter-movement"
endpoints[name] = res.get_json()
if not res.get_json()["raw"]:
return endpoints
Davide Ricci
committed
for rule in app.url_map.iter_rules():
if rule.rule.startswith(f"/api/{namespace}/"): # "/api/dome/shutter/movement"
if "GET" in rule.methods:
with app.test_client() as client:
res = client.get(rule.rule)
name = "-".join(rule.rule.split("/")[2:]) # "dome-shutter-movement"
endpoints[name] = res.get_json()
Davide Ricci
committed
return endpoints
if __name__ == "__main__":
# System modules
import sys
arg = sys.argv[1]
if arg.startswith("--noweb"):
with_web = False
port = 5533
else:
port = arg
except IndexError as e:
port = 5533
print(f"hostname {hostname}")
socketio = instance.enable(app)
socketio.run(app, host=hostname, port=port)