curl implementation

This commit is contained in:
Martin Vrhovšek 2025-01-16 13:26:07 +01:00
parent 6ca98da28b
commit c42d550acf
4 changed files with 61 additions and 9 deletions

View File

@ -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 request(auth: HashMap<String, String>) -> 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]);
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();
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}"),
}
}

View File

@ -31,7 +31,7 @@ pub fn get_json(path: &Path) -> HashMap<String, String> {
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 ..."),

View File

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

3
src/serial.rs Normal file
View File

@ -0,0 +1,3 @@
fn get_port() {}
fn read_messages() {}