From 6ca98da28bb76aacc79f53c882b2b14f30ed3fbb Mon Sep 17 00:00:00 2001 From: Marto Date: Wed, 15 Jan 2025 15:39:52 +0100 Subject: [PATCH] curl setup --- Cargo.lock | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/curl.rs | 11 +++ src/json.rs | 54 +++++++++++++++ src/main.rs | 45 ++---------- 5 files changed, 268 insertions(+), 40 deletions(-) create mode 100644 src/curl.rs create mode 100644 src/json.rs diff --git a/Cargo.lock b/Cargo.lock index ee3af52..6b2b188 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,11 +6,206 @@ version = 4 name = "ArduinoHABridge" version = "0.1.0" dependencies = [ + "curl", "json", ] +[[package]] +name = "cc" +version = "1.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8293772165d9345bdaaa39b45b2109591e63fe5e6fbc23c6ff930a048aa310b" +dependencies = [ + "shlex", +] + +[[package]] +name = "curl" +version = "0.4.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9fb4d13a1be2b58f14d60adba57c9834b78c62fd86c3e76a148f732686e9265" +dependencies = [ + "curl-sys", + "libc", + "openssl-probe", + "openssl-sys", + "schannel", + "socket2", + "windows-sys 0.52.0", +] + +[[package]] +name = "curl-sys" +version = "0.4.78+curl-8.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eec768341c5c7789611ae51cf6c459099f22e64a5d5d0ce4892434e33821eaf" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", + "windows-sys 0.52.0", +] + [[package]] name = "json" version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "libz-sys" +version = "1.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9b68e50e6e0b26f672573834882eb57759f6db9b3be2ea3c35c91188bb4eaa" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "schannel" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 3867779..129c4a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -json = "0.12.4" \ No newline at end of file +json = "0.12.4" +curl = "0.4.47" \ No newline at end of file diff --git a/src/curl.rs b/src/curl.rs new file mode 100644 index 0000000..0b55902 --- /dev/null +++ b/src/curl.rs @@ -0,0 +1,11 @@ +use std::string::String; +use std::collections::HashMap; +use curl::easy::List; + +pub fn curl_request(auth: HashMap) { + // add token + let token: String = String::from("Authorization: Bearer "); + let mut list = List::new(); + // fixme + list.append(token.as_str()).unwrap(); +} \ No newline at end of file diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 0000000..ff80803 --- /dev/null +++ b/src/json.rs @@ -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 { + 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 ..."), + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4da1f12..d99e51d 100644 --- a/src/main.rs +++ b/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 { - 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 ..."), - } - } -} \ No newline at end of file