feat: add placeholder backend #7
60
src/judas_server/web/routes/api.py
Normal file
60
src/judas_server/web/routes/api.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import flask
|
||||||
|
import flask_login
|
||||||
|
import threading
|
||||||
|
import logging as lg
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from werkzeug.wrappers import Response
|
||||||
|
|
||||||
|
bp: flask.Blueprint = flask.Blueprint("api", __name__, url_prefix="/api")
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/client/<client_id>", methods=["GET"])
|
||||||
|
@flask_login.login_required
|
||||||
|
def get_client_data(client_id: str) -> tuple[Response, int]:
|
||||||
|
"""API endpoint to get client data by ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client_id (str): The ID of the client.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response: JSON response with client data or error message.
|
||||||
|
"""
|
||||||
|
backend = flask.current_app.config["BACKEND"]
|
||||||
|
data = backend.get_client_data(client_id)
|
||||||
|
if data is None:
|
||||||
|
return flask.jsonify({"error": "Client not found"}), 404
|
||||||
|
return flask.jsonify(data), 200
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/clients", methods=["GET"])
|
||||||
|
@flask_login.login_required
|
||||||
|
def list_clients() -> tuple[Response, int]:
|
||||||
|
"""API endpoint to list all clients.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response: JSON response with list of client IDs.
|
||||||
|
"""
|
||||||
|
backend = flask.current_app.config["BACKEND"]
|
||||||
|
client_ids = list(backend.clients.keys())
|
||||||
|
return flask.jsonify({"clients": client_ids}), 200
|
||||||
|
|
||||||
|
|
||||||
|
def emit_polled_data(app, socketio):
|
||||||
|
backend = app.config["BACKEND"]
|
||||||
|
|
||||||
|
def poll_loop():
|
||||||
|
import time
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for client_id, data in backend.clients.items():
|
||||||
|
socketio.emit("update_data", {client_id: data})
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
threading.Thread(name="Socketio", target=poll_loop, daemon=True).start()
|
||||||
Reference in New Issue
Block a user