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,
name: String,
network: String,
client: monerod::Client,
}
#[derive(Clone, Debug)]
@ -184,6 +185,7 @@ pub struct MoneroWalletRpc {
rpc_port: u16,
name: String,
network: String,
client: wallet::Client,
}
impl<'c> Monerod {
@ -211,19 +213,20 @@ impl<'c> Monerod {
rpc_port: monerod_rpc_port,
name,
network,
client: monerod::Client::localhost(monerod_rpc_port)?,
},
docker,
))
}
pub fn client(&self) -> monerod::Client {
monerod::Client::localhost(self.rpc_port)
pub fn client(&self) -> &monerod::Client {
&self.client
}
/// Spawns a task to mine blocks in a regular interval to the provided
/// address
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()));
Ok(())
}
@ -256,23 +259,25 @@ impl<'c> MoneroWalletRpc {
let docker = cli.run_with_args(image, run_args);
// 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())
.await
.unwrap();
.await?;
Ok((
Self {
rpc_port: wallet_rpc_port,
name: name.to_string(),
network,
client,
},
docker,
))
}
pub fn client(&self) -> wallet::Client {
wallet::Client::localhost(self.rpc_port)
pub fn client(&self) -> &wallet::Client {
&self.client
}
// 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;
#[jsonrpc_client::api(version = "2.0")]
@ -17,13 +18,15 @@ pub struct Client {
impl Client {
/// New local host monerod RPC client.
pub fn localhost(port: u16) -> Self {
Self {
inner: reqwest::Client::new(),
pub fn localhost(port: u16) -> Result<Self> {
Ok(Self {
inner: reqwest::ClientBuilder::new()
.connection_verbose(true)
.build()?,
base_url: format!("http://127.0.0.1:{}/json_rpc", port)
.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};
#[jsonrpc_client::api(version = "2.0")]
@ -43,20 +43,22 @@ pub struct Client {
impl Client {
/// Constructs a monero-wallet-rpc client with localhost endpoint.
pub fn localhost(port: u16) -> Self {
pub fn localhost(port: u16) -> Result<Self> {
Client::new(
format!("http://127.0.0.1:{}/json_rpc", port)
.parse()
.expect("url is well formed"),
.context("url is well formed")?,
)
}
/// Constructs a monero-wallet-rpc client with `url` endpoint.
pub fn new(url: reqwest::Url) -> Self {
Self {
inner: reqwest::Client::new(),
pub fn new(url: reqwest::Url) -> Result<Self> {
Ok(Self {
inner: reqwest::ClientBuilder::new()
.connection_verbose(true)
.build()?,
base_url: url,
}
})
}
/// Transfers `amount` monero from `account_index` to `address`.

View File

@ -25,7 +25,7 @@ pub struct Wallet {
impl Wallet {
/// 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> {
let client = wallet::Client::new(url);
let client = wallet::Client::new(url)?;
let open_wallet_response = client.open_wallet(name.clone()).await;
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
Client::localhost(port).get_version().await?;
Client::localhost(port)?.get_version().await?;
Ok(WalletRpcProcess {
_child: child,

View File

@ -44,7 +44,7 @@ where
let cli = Cli::default();
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()
.set_default();
@ -276,7 +276,7 @@ async fn init_test_wallets(
.unwrap();
let xmr_wallet = swap::monero::Wallet::connect(
monero.wallet(name).unwrap().client(),
monero.wallet(name).unwrap().client().clone(),
name.to_string(),
env_config,
)