curl setup
This commit is contained in:
11
src/curl.rs
Normal file
11
src/curl.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use std::string::String;
|
||||
use std::collections::HashMap;
|
||||
use curl::easy::List;
|
||||
|
||||
pub fn curl_request(auth: HashMap<String, String>) {
|
||||
// add token
|
||||
let token: String = String::from("Authorization: Bearer ");
|
||||
let mut list = List::new();
|
||||
// fixme
|
||||
list.append(token.as_str()).unwrap();
|
||||
}
|
54
src/json.rs
Normal file
54
src/json.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::io::{ErrorKind, Read};
|
||||
use std::process::exit;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn get_json(path: &Path) -> HashMap<String, String> {
|
||||
let mut data = HashMap::new();
|
||||
let json_data = read_file(path);
|
||||
|
||||
let parsed_data = json::parse(json_data.as_str()).unwrap_or_else(|e| {
|
||||
eprintln!("[ERROR] Invalid JSON: {e}");
|
||||
exit(1);
|
||||
});
|
||||
|
||||
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() {
|
||||
eprintln!("[ERROR] JSON data is NULL");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn read_file(path: &Path) -> String {
|
||||
let read_result = File::open(path);
|
||||
let mut result= String::new();
|
||||
|
||||
match read_result {
|
||||
Ok(mut t) => {
|
||||
// file exists
|
||||
println!("[INFO] JSON file located, reading ...");
|
||||
match t.read_to_string(&mut result) {
|
||||
// handling the reading
|
||||
Ok(_r) => result,
|
||||
Err(e) => panic!("[ERROR] While reading the file: {e}"),
|
||||
}
|
||||
},
|
||||
Err(err) => match err.kind() {
|
||||
ErrorKind::NotFound => {
|
||||
println!("[ERROR] The file '{}' does not exist", path.display());
|
||||
exit(1);
|
||||
},
|
||||
_ => panic!("[ERROR] Unexpected error, closing ..."),
|
||||
}
|
||||
}
|
||||
}
|
45
src/main.rs
45
src/main.rs
@@ -1,46 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{ErrorKind, Read};
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
mod json;
|
||||
mod curl;
|
||||
|
||||
fn main() {
|
||||
let data_file = Path::new("data.json");
|
||||
get_json(data_file);
|
||||
let data = json::get_json(data_file);
|
||||
// some loop for getting data from arduino
|
||||
|
||||
// send curl request
|
||||
let res = curl::curl_request(data);
|
||||
}
|
||||
|
||||
fn get_json(path: &Path) -> HashMap<String, String> {
|
||||
let mut data = HashMap::new();
|
||||
let json_data = read_file(path);
|
||||
|
||||
// fixme
|
||||
let parsed_data = json::parse(json_data.as_str()).unwrap();
|
||||
println!("{}", parsed_data);
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
fn read_file(path: &Path) -> String {
|
||||
let read_result = File::open(path);
|
||||
let mut result= String::new();
|
||||
|
||||
match read_result {
|
||||
Ok(mut t) => {
|
||||
// file exists
|
||||
println!("[INFO] JSON file located, reading ...");
|
||||
match t.read_to_string(&mut result) {
|
||||
// handling the reading
|
||||
Ok(_r) => result,
|
||||
Err(e) => panic!("[ERROR] While reading the file: {e}"),
|
||||
}
|
||||
},
|
||||
Err(err) => match err.kind() {
|
||||
ErrorKind::NotFound => {
|
||||
println!("[ERROR] {} does not exist", path.display());
|
||||
exit(1);
|
||||
},
|
||||
_ => panic!("[ERROR] Unexpected error, closing ..."),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user