Initialize reqwest clients with verbose logging

This commit is contained in:
Thomas Eizinger 2021-04-19 10:10:46 +10:00
parent 7e688eb7e8
commit 0970c2bc72
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
6 changed files with 34 additions and 24 deletions

View File

@ -177,6 +177,7 @@ pub struct Monerod {
rpc_port: u16, rpc_port: u16,
name: String, name: String,
network: String, network: String,
client: monerod::Client,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -184,6 +185,7 @@ pub struct MoneroWalletRpc {
rpc_port: u16, rpc_port: u16,
name: String, name: String,
network: String, network: String,
client: wallet::Client,
} }
impl<'c> Monerod { impl<'c> Monerod {
@ -211,19 +213,20 @@ impl<'c> Monerod {
rpc_port: monerod_rpc_port, rpc_port: monerod_rpc_port,
name, name,
network, network,
client: monerod::Client::localhost(monerod_rpc_port)?,
}, },
docker, docker,
)) ))
} }
pub fn client(&self) -> monerod::Client { pub fn client(&self) -> &monerod::Client {
monerod::Client::localhost(self.rpc_port) &self.client
} }
/// Spawns a task to mine blocks in a regular interval to the provided /// Spawns a task to mine blocks in a regular interval to the provided
/// address /// address
pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> { pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> {
let monerod = self.client(); let monerod = self.client().clone();
let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string())); let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
Ok(()) Ok(())
} }
@ -256,23 +259,25 @@ impl<'c> MoneroWalletRpc {
let docker = cli.run_with_args(image, run_args); let docker = cli.run_with_args(image, run_args);
// create new wallet // create new wallet
wallet::Client::localhost(wallet_rpc_port) let client = wallet::Client::localhost(wallet_rpc_port)?;
client
.create_wallet(name.to_owned(), "English".to_owned()) .create_wallet(name.to_owned(), "English".to_owned())
.await .await?;
.unwrap();
Ok(( Ok((
Self { Self {
rpc_port: wallet_rpc_port, rpc_port: wallet_rpc_port,
name: name.to_string(), name: name.to_string(),
network, network,
client,
}, },
docker, docker,
)) ))
} }
pub fn client(&self) -> wallet::Client { pub fn client(&self) -> &wallet::Client {
wallet::Client::localhost(self.rpc_port) &self.client
} }
// It takes a little while for the wallet to sync with monerod. // It takes a little while for the wallet to sync with monerod.

View File

@ -1,3 +1,4 @@
use anyhow::{Context, Result};
use serde::Deserialize; use serde::Deserialize;
#[jsonrpc_client::api(version = "2.0")] #[jsonrpc_client::api(version = "2.0")]
@ -17,13 +18,15 @@ pub struct Client {
impl Client { impl Client {
/// New local host monerod RPC client. /// New local host monerod RPC client.
pub fn localhost(port: u16) -> Self { pub fn localhost(port: u16) -> Result<Self> {
Self { Ok(Self {
inner: reqwest::Client::new(), inner: reqwest::ClientBuilder::new()
.connection_verbose(true)
.build()?,
base_url: format!("http://127.0.0.1:{}/json_rpc", port) base_url: format!("http://127.0.0.1:{}/json_rpc", port)
.parse() .parse()
.expect("url is well formed"), .context("url is well formed")?,
} })
} }
} }

View File

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use serde::{de::Error, Deserialize, Deserializer, Serialize}; use serde::{de::Error, Deserialize, Deserializer, Serialize};
#[jsonrpc_client::api(version = "2.0")] #[jsonrpc_client::api(version = "2.0")]
@ -43,20 +43,22 @@ pub struct Client {
impl Client { impl Client {
/// Constructs a monero-wallet-rpc client with localhost endpoint. /// Constructs a monero-wallet-rpc client with localhost endpoint.
pub fn localhost(port: u16) -> Self { pub fn localhost(port: u16) -> Result<Self> {
Client::new( Client::new(
format!("http://127.0.0.1:{}/json_rpc", port) format!("http://127.0.0.1:{}/json_rpc", port)
.parse() .parse()
.expect("url is well formed"), .context("url is well formed")?,
) )
} }
/// Constructs a monero-wallet-rpc client with `url` endpoint. /// Constructs a monero-wallet-rpc client with `url` endpoint.
pub fn new(url: reqwest::Url) -> Self { pub fn new(url: reqwest::Url) -> Result<Self> {
Self { Ok(Self {
inner: reqwest::Client::new(), inner: reqwest::ClientBuilder::new()
.connection_verbose(true)
.build()?,
base_url: url, base_url: url,
} })
} }
/// Transfers `amount` monero from `account_index` to `address`. /// Transfers `amount` monero from `account_index` to `address`.

View File

@ -25,7 +25,7 @@ pub struct Wallet {
impl Wallet { impl Wallet {
/// Connect to a wallet RPC and load the given wallet by name. /// Connect to a wallet RPC and load the given wallet by name.
pub async fn open_or_create(url: Url, name: String, env_config: Config) -> Result<Self> { pub async fn open_or_create(url: Url, name: String, env_config: Config) -> Result<Self> {
let client = wallet::Client::new(url); let client = wallet::Client::new(url)?;
let open_wallet_response = client.open_wallet(name.clone()).await; let open_wallet_response = client.open_wallet(name.clone()).await;
if open_wallet_response.is_err() { if open_wallet_response.is_err() {

View File

@ -165,7 +165,7 @@ impl WalletRpc {
} }
// Send a json rpc request to make sure monero_wallet_rpc is ready // Send a json rpc request to make sure monero_wallet_rpc is ready
Client::localhost(port).get_version().await?; Client::localhost(port)?.get_version().await?;
Ok(WalletRpcProcess { Ok(WalletRpcProcess {
_child: child, _child: child,

View File

@ -44,7 +44,7 @@ where
let cli = Cli::default(); let cli = Cli::default();
let _guard = tracing_subscriber::fmt() let _guard = tracing_subscriber::fmt()
.with_env_filter("warn,swap=debug,monero_harness=debug,monero_rpc=debug,bitcoin_harness=info,testcontainers=info") .with_env_filter("warn,swap=debug,monero_harness=debug,monero_rpc=debug,bitcoin_harness=info,testcontainers=info") // add `reqwest::connect::verbose=trace` if you want to logs of the RPC clients
.with_test_writer() .with_test_writer()
.set_default(); .set_default();
@ -276,7 +276,7 @@ async fn init_test_wallets(
.unwrap(); .unwrap();
let xmr_wallet = swap::monero::Wallet::connect( let xmr_wallet = swap::monero::Wallet::connect(
monero.wallet(name).unwrap().client(), monero.wallet(name).unwrap().client().clone(),
name.to_string(), name.to_string(),
env_config, env_config,
) )