use { super::*, base64::Engine, hyper::{client::HttpConnector, Body, Client, Method, Request, Uri}, serde_json::{json, Value}, }; pub(crate) struct Fetcher { auth: String, client: Client, url: Uri, } #[derive(Deserialize, Debug)] struct JsonResponse { error: Option, id: usize, result: Option, } #[derive(Deserialize, Debug)] struct JsonError { code: i32, message: String, } impl Fetcher { pub(crate) fn new(options: &Options) -> Result { let client = Client::new(); let url = if options.rpc_url().starts_with("http://") { options.rpc_url() } else { "http://".to_string() + &options.rpc_url() }; let url = Uri::try_from(&url).map_err(|e| anyhow!("Invalid rpc url {url}: {e}"))?; let (user, password) = options.auth()?.get_user_pass()?; let auth = format!("{}:{}", user.unwrap(), password.unwrap()); let auth = format!( "Basic {}", &base64::engine::general_purpose::STANDARD.encode(auth) ); Ok(Fetcher { client, url, auth })