From 8a1fc057912913c38a097120590559a22d011afd Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 1 Jan 2026 15:28:12 +0100 Subject: [PATCH] Implement controllers --- api-customer/app.py | 26 -------------- .../src/controllers/AuthController.py | 34 +++++++++++++++++++ .../src/controllers/ClientController.py | 33 ++++++++++++++++++ 3 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 api-customer/src/controllers/AuthController.py create mode 100644 api-customer/src/controllers/ClientController.py diff --git a/api-customer/app.py b/api-customer/app.py index 78a657a3..5397a483 100644 --- a/api-customer/app.py +++ b/api-customer/app.py @@ -21,10 +21,6 @@ local_broker_port = int(os.environ.get("LOCAL_BROKER_PORT", 1883)) api_broker_address = os.environ.get("API_BROKER_ADDRESS", "127.0.0.1") api_broker_port = int(os.environ.get("API_BROKER_PORT", 1883)) -@app.route('/') -def hello_world(): - return 'Hello World!' - if __name__ == '__main__': local_broker = MQTTService( local_broker_address, @@ -45,26 +41,4 @@ if __name__ == '__main__': forwarder = MQTTForwarder(client_id, local_broker, api_broker) forwarder.start(f"/customer/telemetry/#", f"/board-mate/{client_id}/telemetry") - """config = Configuration( - host="https://192.168.15.120:8000", - ) - config.verify_ssl = False - - with ApiClient(config) as api_client: - client_api = ClientApi(api_client) - - new_client = ClientDto( - name=StrictStr("Alice Example"), - username=StrictStr("alice123"), - ) - - try: - # 5. Call the endpoint - response = client_api.create(client_dto=new_client) - print("Success:", response.success) - print("Message:", response.message) - print("Data:", response.data) - except ApiException as e: - print(f"Exception when calling ClientApi->create: {e}")""" - app.run(host="0.0.0.0", port=5000, debug=True) diff --git a/api-customer/src/controllers/AuthController.py b/api-customer/src/controllers/AuthController.py new file mode 100644 index 00000000..ec67a55e --- /dev/null +++ b/api-customer/src/controllers/AuthController.py @@ -0,0 +1,34 @@ +from board_mate.auth import * +from flask import jsonify, request +from pydantic import StrictStr + + +class AuthController: + def __init__(self, app): + self._register_routes(app) + self.config = Configuration(host="https://192.168.15.120:8000") + self.config.verify_ssl = False + + def _register_routes(self, app): + app.add_url_rule("/client/auth", view_func=self.auth, methods=['POST']) + + def auth(self): + try: + received_data = request.get_json() + with ApiClient(self.config) as client: + auth_api = AuthApi(client) + + auth_request = AuthRequestDto( + username=StrictStr(received_data["username"]), + key=StrictStr(received_data["key"]), + ) + + response = auth_api.login(auth_request_dto=auth_request) + print("Success:", response.success) + print("Message:", response.message) + print("Data:", response.data) + return jsonify(response), 200 + except Exception as e: + print(f"Exception when calling AuthAPI->login: {e}") + return jsonify(response), 500 + diff --git a/api-customer/src/controllers/ClientController.py b/api-customer/src/controllers/ClientController.py new file mode 100644 index 00000000..2ee9fe4b --- /dev/null +++ b/api-customer/src/controllers/ClientController.py @@ -0,0 +1,33 @@ +from board_mate.client import Configuration, ApiClient, ClientApi, ClientDto, ApiException +from flask import jsonify +from pydantic import StrictStr + + +class GameController: + def __init__(self, app): + self._register_routes(app) + self.config = Configuration(host="https://192.168.15.120:8000") + self.config.verify_ssl = False + + def _register_routes(self, app): + app.add_url_rule("/client/create", view_func=self.create, methods=['POST']) + + def create(self): + with ApiClient(self.config) as api_client: + client_api = ClientApi(api_client) + + new_client = ClientDto( + name=StrictStr("Alice Example"), + username=StrictStr("alice123"), + ) + + try: + response = client_api.create(client_dto=new_client) + print("Success:", response.success) + print("Message:", response.message) + print("Data:", response.data) + return jsonify(response), 200 + except ApiException as e: + print(f"Exception when calling ClientApi->create: {e}") + return jsonify(response), 500 +