Start to create Python client

This commit is contained in:
Pierre HUBERT 2024-06-29 18:05:58 +02:00
parent 1d32ca1559
commit dca8848ec9
8 changed files with 57 additions and 0 deletions

View File

@ -31,6 +31,10 @@ pub async fn unsecure_server() -> anyhow::Result<()> {
"/", "/",
web::get().to(unsecure_server_controller::unsecure_home), web::get().to(unsecure_server_controller::unsecure_home),
) )
.route(
"/secure_origin",
web::get().to(unsecure_server_controller::secure_origin),
)
.route( .route(
"/pki/{file}", "/pki/{file}",
web::get().to(unsecure_pki_controller::serve_pki_file), web::get().to(unsecure_pki_controller::serve_pki_file),

View File

@ -1,3 +1,4 @@
use crate::app_config::AppConfig;
use actix_web::HttpResponse; use actix_web::HttpResponse;
pub async fn unsecure_home() -> HttpResponse { pub async fn unsecure_home() -> HttpResponse {
@ -5,3 +6,7 @@ pub async fn unsecure_home() -> HttpResponse {
.content_type("text/plain") .content_type("text/plain")
.body("SolarEnergy unsecure central backend") .body("SolarEnergy unsecure central backend")
} }
pub async fn secure_origin() -> HttpResponse {
HttpResponse::Ok().body(AppConfig::get().secure_origin())
}

2
python_device/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
storage

7
python_device/README.md Normal file
View File

@ -0,0 +1,7 @@
# Python client
Run the client:
```bash
python3 -m src.main
```

View File

8
python_device/src/api.py Normal file
View File

@ -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

12
python_device/src/args.py Normal file
View File

@ -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")

19
python_device/src/main.py Normal file
View File

@ -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}")