hashmap -> struct
This commit is contained in:
		
							
								
								
									
										17
									
								
								src/curl.rs
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/curl.rs
									
									
									
									
									
								
							@@ -1,23 +1,18 @@
 | 
			
		||||
use std::collections;
 | 
			
		||||
 | 
			
		||||
use std::io::Read;
 | 
			
		||||
 | 
			
		||||
pub fn startup(auth: &collections::HashMap<String, String>) -> (curl::easy::Easy, String) {
 | 
			
		||||
    let url = auth.get("ha_url").expect("[ERROR] Failed to read the URL").as_str();
 | 
			
		||||
 | 
			
		||||
pub fn startup(json: &crate::json::JsonStruct) -> (curl::easy::Easy, String) {
 | 
			
		||||
    // escape character for { is {{ and for } is }}
 | 
			
		||||
    let entity_id = auth.get("light_entity_id").expect("[ERROR] Failed to read the entity id").as_str();
 | 
			
		||||
    let data = format!("{{\"entity_id\": {entity_id}}}");
 | 
			
		||||
    
 | 
			
		||||
    let token = auth.get("access_token").expect("[ERROR] Failed to read the token").as_str();
 | 
			
		||||
    let auth = format!("Authorization: Bearer {}", &token[1..token.len()-1]);
 | 
			
		||||
    let data = format!("{{\"entity_id\": {}}}", &json.light_entity_id);
 | 
			
		||||
    let auth = format!("Authorization: Bearer {}", &json.access_token[1..json.access_token.len()-1]);
 | 
			
		||||
 | 
			
		||||
    let mut list = curl::easy::List::new();
 | 
			
		||||
    list.append(&auth).unwrap();
 | 
			
		||||
    list.append(&auth).expect("[ERROR] Failed to set the authentication token");
 | 
			
		||||
    list.append("Content-Type: application/json").unwrap();
 | 
			
		||||
 | 
			
		||||
    let mut request = curl::easy::Easy::new();
 | 
			
		||||
 | 
			
		||||
    request.url(&url[1..url.len()-1]).expect("[ERROR] Failed to set the URL for the CURL request");
 | 
			
		||||
    request.url(&json.ha_url[1..&json.ha_url.len()-1]).expect("[ERROR] Failed to set the URL for the CURL request");
 | 
			
		||||
    request.post(true).expect("[ERROR] Failed to set the CURL request as POST");
 | 
			
		||||
    request.http_headers(list).expect("ERROR] Failed to apply HTTP headers");
 | 
			
		||||
    request.post_field_size(data.len() as u64).expect("[ERROR] Failed to set the POST field size for CURL request");
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								src/json.rs
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/json.rs
									
									
									
									
									
								
							@@ -1,26 +1,33 @@
 | 
			
		||||
use std::path::Path;
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::{ErrorKind, Read};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
pub fn get_json(path: &Path) -> HashMap<String, String> {
 | 
			
		||||
    let mut data = HashMap::new();
 | 
			
		||||
pub struct JsonStruct {
 | 
			
		||||
    pub ha_url: String,
 | 
			
		||||
    pub access_token: String,
 | 
			
		||||
    pub light_entity_id: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl JsonStruct {
 | 
			
		||||
    fn validate_content(&self) -> bool {
 | 
			
		||||
        !self.ha_url.is_empty() && !self.access_token.is_empty() && !self.light_entity_id.is_empty()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn get_json(path: &Path) -> JsonStruct {
 | 
			
		||||
    let json_data = read_file(path);
 | 
			
		||||
 | 
			
		||||
    let parsed_data = json::parse(json_data.as_str()).expect("[ERROR] Invalid JSON");
 | 
			
		||||
 | 
			
		||||
    let ha_url = parsed_data["ha_url"].clone();
 | 
			
		||||
    let access_token = parsed_data["access_token"].clone();
 | 
			
		||||
    let light_entity_id = parsed_data["light_entity_id"].clone();
 | 
			
		||||
 | 
			
		||||
    if ha_url.is_null() || access_token.is_null() || light_entity_id.is_null() {
 | 
			
		||||
    
 | 
			
		||||
    let data = JsonStruct {
 | 
			
		||||
        ha_url: json::stringify(parsed_data["ha_url"].clone()),
 | 
			
		||||
        access_token: json::stringify(parsed_data["access_token"].clone()),
 | 
			
		||||
        light_entity_id: json::stringify(parsed_data["light_entity_id"].clone()),
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    if !data.validate_content() {
 | 
			
		||||
        panic!("[ERROR] JSON data is NULL");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    data.insert("ha_url".to_string(), json::stringify(ha_url));
 | 
			
		||||
    data.insert("access_token".to_string(), json::stringify(access_token));
 | 
			
		||||
    data.insert("light_entity_id".to_string(), json::stringify(light_entity_id));
 | 
			
		||||
 | 
			
		||||
    data
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user