//https://github.com/ferrous-systems/espressif-trainings/blob/main/intro/http-client/src/main.rs
use core::str;

use bsc::wifi::wifi;
use embedded_svc::{
    http::{
        client::{Client, Request, RequestWrite, Response},
        Headers, Status,
    },
    io::Read,
};
use esp32_c3_dkc02_bsc as bsc;
use esp_idf_svc::http::client::{EspHttpClient, EspHttpClientConfiguration, EspHttpRequest};
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported

#[toml_cfg::toml_config]
pub struct Config {
    #[default("Wokwi-GUEST")]
    wifi_ssid: &'static str,
    #[default("")]
    wifi_psk: &'static str,
}

fn main() -> anyhow::Result<()> {
    esp_idf_sys::link_patches();
    println!("starting");
    let _wifi = wifi(CONFIG.wifi_ssid, CONFIG.wifi_psk)?;
    println!("connected");
    // TODO your code here
    //get(...)?;
    get("https://example.org/")?;

    Ok(())
}

fn get(url: impl AsRef<str>) -> anyhow::Result<()> {
    //println!("getting  {}", url.as_ref());

    // 1. Create a new EspHttpClient. (Check documentation)
    let mut client = match EspHttpClient::new(&EspHttpClientConfiguration {
        use_global_ca_store: true,
        crt_bundle_attach: Some(esp_idf_sys::esp_crt_bundle_attach),

        ..Default::default()
    }) {
        Ok(http_client) => http_client,
        Err(error) => panic!("Problem creating EspHttpClient: {:?}", error),
    };
    // 2. Open a GET request to `url`
    let request = match client.get(url.as_ref()) {
        Ok(request) => request,
        Err(error) => panic!("Problem creating EspHttpRequest: {:?}", error),
    };

    // 3. Requests *may* send data to the server. Turn the request into a writer, specifying 0 bytes as write length
    // (since we don't send anything - but have to do the writer step anyway)
    //
    // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_http_client.html
    // If this were a POST request, you'd set a write length > 0 and then writer.do_write(&some_buf);

    // let writer = request...;

    let writer = match request.into_writer(0) {
        Ok(writer) => writer,
        Err(error) => panic!("Problem converting into writer : {:?}", error),
    };

    // 4. Turn the writer into a response and check its status. Successful http status codes are in the 200..=299 range.

    // let response = writer...;
    // let status = ...;
    // println!("response code: {}\n", status);

    let mut response = match writer.submit() {
        Ok(response) => response,
        Err(error) => panic!("Problem converting into response : {:?}", error),
    };

    match response.status() {
        200..=299 => {
            let mut buf = [0_u8; 256];
            let mut reader = response.reader();
            loop {
                 match reader.read(&mut buf) {
                    Ok(size) => {
                        print!("{}", &String::from_utf8_lossy(&buf));
                        if size == 0_usize {break;}
                    }
                    _ => anyhow::bail!("unexpected response code: {}", response.status()),
                } 
            }
        }
        _ => (),
    };
    // 5. If the status is OK, read response data chunk by chunk into a buffer and print it until done.
    // 6. Try converting the bytes into a Rust (UTF-8) string and print it.

    Ok(())
}
Loading
esp32-c3-rust-1