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
impl HttpOptions
Sourcepub fn new(method: &str, connect_timeout: u32, read_timeout: u32) -> Self
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
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}pub fn dump(&self) -> String
Auto Trait Implementations§
impl Freeze for HttpOptions
impl RefUnwindSafe for HttpOptions
impl Send for HttpOptions
impl Sync for HttpOptions
impl Unpin for HttpOptions
impl UnwindSafe for HttpOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more