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')
import configparser
# Load the config file
config = configparser.ConfigParser()
config.read('config/api.ini')
Davide Ricci
committed
@app.route('/all/<string:namespace>')
def get_all(namespace):
# '''Build a global status for a given namespace'''
# wait = float(request.args.get('wait', 0))
Davide Ricci
committed
endpoints = {}
sections = []
# sorting GET priority
for section in config.sections():
if section.startswith("/"+namespace):
priority = config[section].getint('get-priority', float('inf'))
sections.append((priority, section))
# Sort sections based on priority (higher priority first)
sections.sort()
for _, section in sections:
with app.test_client() as client:
print(f"getting http://fork.orsa.unige.net:5533/api{section}")
# res = requests.get(f"http://fork.orsa.unige.net:5533/api{section}")
res = client.get(section)
if res.status_code != 405: # 405 Method Not Allowed
name = "-".join(section.split("/")[2:]) # "dome-ping"
# endpoints[name] = res.json()
endpoints[name] = res.get_json()
if "raw" in res.json():
if not res.json()["raw"]:
break
elif (res.json()["response"] == "FAILED 2"):
break
Davide Ricci
committed
return endpoints
# @app.route('/all/<string:namespace>')
# def get_status(namespace):
# '''Build a global status for a given namespace'''
# wait = float(request.args.get('wait', 0))
# 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-ping"
# endpoints[name] = res.get_json()
# if "raw" in res.get_json():
# if not res.get_json()["raw"]:
# return endpoints
# 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()
# time.sleep(wait)
# 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)