Struct HttpOptions

Source
pub struct HttpOptions {
    pub method: String,
    pub connect_timeout: u32,
    pub read_timeout: u32,
    pub body: Option<String>,
    pub headers: Option<BTreeMap<String, String>>,
}

Fields§

§method: String§connect_timeout: u32§read_timeout: u32§body: Option<String>§headers: Option<BTreeMap<String, String>>

Implementations§

Source§

impl HttpOptions

Source

pub fn new(method: &str, connect_timeout: u32, read_timeout: u32) -> Self

Examples found in repository?
examples/httpbin.rs (line 4)
3fn main() {
4    let mut opts = HttpOptions::new("GET", 30, 10);
5    opts.headers = Some(std::collections::BTreeMap::from([(
6        "X-Test".to_string(),
7        "123".to_string(),
8    )]));
9
10    let http = BlocklessHttp::open("http://httpbin.org/anything", &opts);
11    let http = http.unwrap();
12    let body = http.get_all_body().unwrap();
13    let body = String::from_utf8(body).unwrap();
14    let bodies = match json::parse(&body).unwrap() {
15        json::JsonValue::Object(o) => o,
16        _ => panic!("must be object"),
17    };
18
19    let headers = match bodies.get("headers") {
20        Some(json::JsonValue::Object(headers)) => headers,
21        _ => panic!("must be array"),
22    };
23    headers.iter().for_each(|s| {
24        println!("{} = {}", s.0, s.1);
25    });
26}
More examples
Hide additional examples
examples/coingecko_oracle.rs (line 22)
13fn main() {
14    // read coin id from stdin
15    let mut buf = [0; 1024];
16    let len = read_stdin(&mut buf).unwrap();
17    let coin_id = std::str::from_utf8(&buf[..len as usize])
18        .unwrap_or_default()
19        .trim();
20
21    // perform http request
22    let http_opts = HttpOptions::new("GET", 30, 10);
23    let http_res = BlocklessHttp::open(
24        format!(
25            "https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd",
26            coin_id
27        )
28        .as_str(),
29        &http_opts,
30    )
31    .unwrap();
32    let body = http_res.get_all_body().unwrap(); // e.g. {"bitcoin":{"usd":67675}}
33
34    // println!("{}", String::from_utf8(body.clone()).unwrap());
35
36    // parse the json response; extrac usd price
37    let json: serde_json::Result<HashMap<String, HashMap<String, f64>>> =
38        serde_json::from_slice(&body);
39    let Ok(data) = json else {
40        eprintln!("Failed to parse JSON");
41        return;
42    };
43    let Some(coin_data) = data.get(coin_id) else {
44        eprintln!("Coin not found in response.");
45        return;
46    };
47    let Some(usd_price) = coin_data.get("usd") else {
48        eprintln!("USD price not found for {}.", coin_id);
49        return;
50    };
51
52    let coin_price = CoinPrice {
53        id: coin_id.to_string(),
54        price: (*usd_price * 1_000_000.0) as u64, // price in 6 decimals
55        currency: "usd".to_string(),
56    };
57    println!("{}", json!(coin_price));
58}
Source

pub fn dump(&self) -> String

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,