#http-client #blocking #dotenv #deserialize #requiring #ddf #diamond-ddf

ddf-blocking-http-client

Convenience crate for DiamondDDF projects requiring a blocking http client

3 releases

0.0.3 Aug 29, 2022
0.0.2 Aug 26, 2022
0.0.1 Aug 26, 2022

#1725 in Parser implementations

40 downloads per month

MIT/Apache

12KB
119 lines

This is a convenience crate for handling http requests and errors.

Examples

use ddf_blocking_http_client::Client;
use ddf_blocking_http_client::HttpResult;
use serde::Deserialize;

use dotenv::dotenv;
use std::env;

// dotenv is being used for lack of an example not requiring private keys and public wallet addresses.
// dotenv is not required for this crate to work.
dotenv::dotenv().ok();
let api_key = env::var("API_KEY").unwrap();
let wallet_address = env::var("PUBLIC_WALLET_ADDRESS").unwrap();

#[derive(Debug, Deserialize)]
struct Balance {
    result: String
}

let client = Client::new();

let url = 
    &[
        "https://api.etherscan.io/api",
        "?module=account",
        "&action=balance",
        "&address=", &wallet_address,
        "&tag=latest",
        "&apikey=", &api_key
    ].concat();

// Here is the main feature, Balance is the data structure expected back, defined above.
// You define your own struct and use the serde crate and tag the struct with #[derive(Deserialize)]
// and put the name of your struct where our example is using "Balance".
// the 10 means we are going to try 10 times, if there is a too_many_requests response, or 
// an unknown error (perhaps our crate just doesn't handle it yet) then it will wait 1 second and try again.
// each time it will double the wait time. The return type is HttpResult<T>

let balance = client.get::<Balance>(url, 10).unwrap().result;

dbg!(&balance);

// If you are unsure the shape of the data, you can at first use the "get_as_text" function.
// Here is an example of that "get_as_text" function, so you can get a look at all the fields to base your struct off of:

let balance = client.get_as_text(&url);

dbg!(&balance);

Dependencies

~4–18MB
~258K SLoC