From c42d550acf0b6f7cd4ed88c1f47b0f8553170951 Mon Sep 17 00:00:00 2001 From: Marto Date: Thu, 16 Jan 2025 13:26:07 +0100 Subject: [PATCH] curl implementation --- src/curl.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++------ src/json.rs | 4 ++-- src/main.rs | 4 +++- src/serial.rs | 3 +++ 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/serial.rs diff --git a/src/curl.rs b/src/curl.rs index 0b55902..26e1aa1 100644 --- a/src/curl.rs +++ b/src/curl.rs @@ -1,11 +1,58 @@ +use std::io::Read; use std::string::String; use std::collections::HashMap; -use curl::easy::List; +use curl::easy::{Easy, List}; -pub fn curl_request(auth: HashMap) { - // add token - let token: String = String::from("Authorization: Bearer "); +pub fn request(auth: HashMap) -> u32 { + let url = auth.get("ha_url").expect("[ERROR] Failed to read the URL").as_str(); + + // 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 body = format!("{{\"entity_id\": {entity_id}}}"); + let mut data = body.as_bytes(); + + 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 mut list = List::new(); - // fixme - list.append(token.as_str()).unwrap(); + list.append(&auth).unwrap(); + list.append("Content-Type: application/json").unwrap(); + + let mut request = Easy::new(); + // proper error handling + + request.url(&url[1..url.len()-1]).unwrap_or_else(|e| { + panic!("[ERROR] Failed to set the URL for the CURL request:\n{e}"); + }); + + request.post(true).unwrap_or_else(|e| { + panic!("[ERROR] Failed to set the CURL request as POST:\n{e}"); + }); + + request.http_headers(list).unwrap_or_else(|e| { + panic!("[ERROR] Failed to apply HTTP headers:\n{e}"); + }); + request.post_field_size(data.len() as u64).unwrap_or_else(|e| { + panic!("[ERROR] Failed to set the POST field size for CURL request:\n{e}"); + }); + + let mut transfer = request.transfer(); + + transfer.read_function(|buf| { + Ok(data.read(buf).unwrap_or(0)) + }).unwrap_or_else(|e| { + panic!("[ERROR] Failed to read the data:\n{e}"); + }); + + transfer.perform().unwrap_or_else(|e| { + panic!("[ERROR] Failed to perform the CURL request:\n{e}"); + }); + + // drop transfer so we can access request.response_code below + drop(transfer); + + match request.response_code() { + Ok(t) => t, + Err(e) => panic!("[ERROR] Failed to retrieve the response code:\n{e}"), + } } \ No newline at end of file diff --git a/src/json.rs b/src/json.rs index ff80803..d998144 100644 --- a/src/json.rs +++ b/src/json.rs @@ -31,7 +31,7 @@ pub fn get_json(path: &Path) -> HashMap { fn read_file(path: &Path) -> String { let read_result = File::open(path); - let mut result= String::new(); + let mut result = String::new(); match read_result { Ok(mut t) => { @@ -45,7 +45,7 @@ fn read_file(path: &Path) -> String { }, Err(err) => match err.kind() { ErrorKind::NotFound => { - println!("[ERROR] The file '{}' does not exist", path.display()); + eprintln!("[ERROR] The file '{}' does not exist", path.display()); exit(1); }, _ => panic!("[ERROR] Unexpected error, closing ..."), diff --git a/src/main.rs b/src/main.rs index d99e51d..41b9182 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::path::Path; mod json; mod curl; +mod serial; fn main() { let data_file = Path::new("data.json"); @@ -8,6 +9,7 @@ fn main() { // some loop for getting data from arduino // send curl request - let res = curl::curl_request(data); + let res = curl::request(data); + println!("Response code: {res}\n"); } diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..734044b --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,3 @@ +fn get_port() {} + +fn read_messages() {} \ No newline at end of file