Start to create Python client
This commit is contained in:
		@@ -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),
 | 
			
		||||
 
 | 
			
		||||
@@ -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())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								python_device/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								python_device/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
*.pyc
 | 
			
		||||
storage
 | 
			
		||||
							
								
								
									
										7
									
								
								python_device/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								python_device/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
# Python client
 | 
			
		||||
 | 
			
		||||
Run the client:
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
python3 -m src.main
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										0
									
								
								python_device/src/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								python_device/src/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										8
									
								
								python_device/src/api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								python_device/src/api.py
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										12
									
								
								python_device/src/args.py
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										19
									
								
								python_device/src/main.py
									
									
									
									
									
										Normal 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}")
 | 
			
		||||
		Reference in New Issue
	
	Block a user