pub struct BlocklessHttp { /* private fields */ }Implementations§
Source§impl BlocklessHttp
impl BlocklessHttp
Sourcepub fn open(url: &str, opts: &HttpOptions) -> Result<Self, HttpErrorKind>
pub fn open(url: &str, opts: &HttpOptions) -> Result<Self, HttpErrorKind>
Examples found in repository?
examples/httpbin.rs (line 10)
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 (lines 23-30)
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 get_code(&self) -> u32
Sourcepub fn get_all_body(&self) -> Result<Vec<u8>, HttpErrorKind>
pub fn get_all_body(&self) -> Result<Vec<u8>, HttpErrorKind>
Examples found in repository?
examples/httpbin.rs (line 12)
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 32)
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 get_header(&self, header: &str) -> Result<String, HttpErrorKind>
pub fn close(self)
pub fn read_body(&self, buf: &mut [u8]) -> Result<u32, HttpErrorKind>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlocklessHttp
impl RefUnwindSafe for BlocklessHttp
impl Send for BlocklessHttp
impl Sync for BlocklessHttp
impl Unpin for BlocklessHttp
impl UnwindSafe for BlocklessHttp
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