From dca8848ec96f1d4fa3c5b598a37393c5fdda7d84 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 29 Jun 2024 18:05:58 +0200 Subject: [PATCH] Start to create Python client --- central_backend/src/server/servers.rs | 4 ++++ .../unsecure_server_controller.rs | 5 +++++ python_device/.gitignore | 2 ++ python_device/README.md | 7 +++++++ python_device/src/__init__.py | 0 python_device/src/api.py | 8 ++++++++ python_device/src/args.py | 12 ++++++++++++ python_device/src/main.py | 19 +++++++++++++++++++ 8 files changed, 57 insertions(+) create mode 100644 python_device/.gitignore create mode 100644 python_device/README.md create mode 100644 python_device/src/__init__.py create mode 100644 python_device/src/api.py create mode 100644 python_device/src/args.py create mode 100644 python_device/src/main.py diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index 09b4748..87cdaf8 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -31,6 +31,10 @@ pub async fn unsecure_server() -> anyhow::Result<()> { "/", web::get().to(unsecure_server_controller::unsecure_home), ) + .route( + "/secure_origin", + web::get().to(unsecure_server_controller::secure_origin), + ) .route( "/pki/{file}", web::get().to(unsecure_pki_controller::serve_pki_file), diff --git a/central_backend/src/server/unsecure_server/unsecure_server_controller.rs b/central_backend/src/server/unsecure_server/unsecure_server_controller.rs index db2b282..7506bd7 100644 --- a/central_backend/src/server/unsecure_server/unsecure_server_controller.rs +++ b/central_backend/src/server/unsecure_server/unsecure_server_controller.rs @@ -1,3 +1,4 @@ +use crate::app_config::AppConfig; use actix_web::HttpResponse; pub async fn unsecure_home() -> HttpResponse { @@ -5,3 +6,7 @@ pub async fn unsecure_home() -> HttpResponse { .content_type("text/plain") .body("SolarEnergy unsecure central backend") } + +pub async fn secure_origin() -> HttpResponse { + HttpResponse::Ok().body(AppConfig::get().secure_origin()) +} diff --git a/python_device/.gitignore b/python_device/.gitignore new file mode 100644 index 0000000..0f236ba --- /dev/null +++ b/python_device/.gitignore @@ -0,0 +1,2 @@ +*.pyc +storage \ No newline at end of file diff --git a/python_device/README.md b/python_device/README.md new file mode 100644 index 0000000..346b7e2 --- /dev/null +++ b/python_device/README.md @@ -0,0 +1,7 @@ +# Python client + +Run the client: + +```bash +python3 -m src.main +``` \ No newline at end of file diff --git a/python_device/src/__init__.py b/python_device/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python_device/src/api.py b/python_device/src/api.py new file mode 100644 index 0000000..e46155d --- /dev/null +++ b/python_device/src/api.py @@ -0,0 +1,8 @@ +import requests +from src.args import args + +def get_secure_origin() -> str: + res = requests.get(f"{args.unsecure_origin}/secure_origin") + if res.status_code < 200 or res.status_code > 299: + raise Exception(f"Get secure origin failed with status {res.status_code}") + return res.text \ No newline at end of file diff --git a/python_device/src/args.py b/python_device/src/args.py new file mode 100644 index 0000000..e3859ec --- /dev/null +++ b/python_device/src/args.py @@ -0,0 +1,12 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + description='SolarEnergy Python-based client') + +parser.add_argument("--unsecure_origin", help="Change unsecure API origin", default="http://localhost:8080") +parser.add_argument("--storage", help="Change storage location", default="storage") + +args = parser.parse_args() + +args.secure_origin_path = os.path.join(args.storage, "SECURE_ORIGIN") \ No newline at end of file diff --git a/python_device/src/main.py b/python_device/src/main.py new file mode 100644 index 0000000..ee2af42 --- /dev/null +++ b/python_device/src/main.py @@ -0,0 +1,19 @@ +from src.args import args +import src.api as api +import os + +print("Check storage") +if not os.path.isdir(args.storage): + print("Create storage") + os.makedirs(args.storage, exist_ok=True) + + +print("Check secure origin...") +if not os.path.isfile(args.secure_origin_path): + origin = api.get_secure_origin() + with open(args.secure_origin_path, "w") as f: + f.write(origin) + +with open(args.secure_origin_path, "r") as f: + args.secure_origin = f.read() +print(f"Secure origin = {args.secure_origin}") \ No newline at end of file