ddf-blocking-http-client

Crates.ioddf-blocking-http-client
lib.rsddf-blocking-http-client
version0.0.3
sourcesrc
created_at2022-08-26 12:08:19.180552
updated_at2022-08-29 00:39:54.623126
descriptionConvenience crate for DiamondDDF projects requiring a blocking http client.
homepage
repository
max_upload_size
id652773
size11,791
(DiamondDDF)

documentation

README

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);
Commit count: 0

cargo fmt