From f5d9c90b5cb0f99210175019605a4b82b7270d0c Mon Sep 17 00:00:00 2001 From: Fang-Pen Lin Date: Sat, 4 Jan 2025 22:17:45 -0800 Subject: [PATCH] Use API client --- src/api/client.rs | 4 ++-- src/main.rs | 48 ++++++----------------------------------------- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/src/api/client.rs b/src/api/client.rs index dd6da4e..6551f77 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -40,9 +40,9 @@ pub struct APIClient<'a> { } impl<'a> APIClient<'a> { - pub fn new(endpoint: String, timeout: time::Duration) -> Self { + pub fn new(endpoint: &str, timeout: time::Duration) -> Self { Self { - endpoint, + endpoint: endpoint.to_string(), timeout, config: EspWebSocketClientConfig { // server_cert: Some(X509::pem_until_nul(SERVER_ROOT_CERT)), diff --git a/src/main.rs b/src/main.rs index 203edc6..71abcfe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod api; mod usb; mod wifi; +use crate::api::client::APIClient; use crate::usb::msc_device::MSCDevice; use crate::wifi::session::WifiSession; use embedded_svc::wifi::AuthMethod; @@ -11,48 +12,11 @@ use esp_idf_svc::hal::prelude::Peripherals; use esp_idf_svc::hal::task::block_on; use esp_idf_svc::http::client::EspHttpConnection; use std::time::Duration; -use std::{fs, thread}; +use std::{fs, thread, time}; const SSID: &str = env!("WIFI_SSID"); const PASSWORD: &str = env!("WIFI_PASS"); -fn post_chunked_request( - client: &mut HttpClient, - data: &[u8], -) -> anyhow::Result<()> { - // Prepare payload - - // Prepare headers and URL - let headers = [("content-type", "text/plain")]; - let url = "https://httpbin.org/post"; - - // Send request - let mut request = client.post(url, &headers)?; - request.write_all(data)?; - request.flush()?; - log::info!("-> CHUNKED POST {}", url); - let mut response = request.submit()?; - - // Process response - let status = response.status(); - log::info!("<- {}", status); - let mut buf = [0u8; 1024]; - let bytes_read = io::try_read_full(&mut response, &mut buf).map_err(|e| e.0)?; - log::info!("Read {} bytes", bytes_read); - match std::str::from_utf8(&buf[0..bytes_read]) { - Ok(body_string) => log::info!( - "Response body (truncated to {} bytes): {:?}", - buf.len(), - body_string - ), - Err(e) => log::error!("Error decoding response body: {}", e), - }; - - // Drain the remaining response bytes - while response.read(&mut buf)? > 0 {} - - Ok(()) -} async fn run_async() -> Result<(), anyhow::Error> { log::info!( @@ -76,7 +40,7 @@ async fn run_async() -> Result<(), anyhow::Error> { crt_bundle_attach: Some(esp_idf_svc::sys::esp_crt_bundle_attach), ..Default::default() }; - let mut client = HttpClient::wrap(EspHttpConnection::new(&config)?); + let mut client = APIClient::new("", time::Duration::from_secs(30)); loop { // Asynchronously wait for GPIO events, allowing other tasks @@ -84,9 +48,9 @@ async fn run_async() -> Result<(), anyhow::Error> { button.wait_for_low().await?; log::info!("Button pressed!"); - let contents = fs::read_to_string("/disk/myfile.txt").unwrap_or(String::from("N/A")); - log::info!("File content: {}", contents); - post_chunked_request(&mut client, contents.as_bytes())?; + client.connect(); + // let contents = fs::read_to_string("/disk/myfile.txt").unwrap_or(String::from("N/A")); + // log::info!("File content: {}", contents); button.wait_for_high().await?; log::info!("Button released!");