Skip to content

Commit

Permalink
Merge pull request #12 from helium/lthiery/api_agent
Browse files Browse the repository at this point in the history
Use a user agent for all clients
  • Loading branch information
madninja authored Nov 19, 2021
2 parents 421a526 + 7f12fe0 commit 30f4250
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 20 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ anyhow = "1.0"
thiserror = "1"
byteorder = "1"
structopt = "0"
helium-api = "3"
helium-wallet = { git = "https://github.com/helium/helium-wallet-rs", tag = "v1.6.8" }
helium-crypto = {git = "https://github.com/helium/helium-crypto-rs", tag="v0.3.0"}
helium-proto = { git = "https://github.com/helium/proto", branch="master"}
helium-api = "3.2"
helium-wallet = { git = "https://github.com/helium/helium-wallet-rs", tag = "v1.6.9" }
helium-crypto = {git = "https://github.com/helium/helium-crypto-rs", tag = "v0.3.1"}
helium-proto = { git = "https://github.com/helium/proto", branch = "master"}
prettytable-rs = "0"
bs58 = {version = "0", features=["check"]}
base64 = "0"
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate prettytable;

use helium_api::models::{Hnt, PendingTxnStatus};
use helium_api::models::{transactions::PendingTxnStatus, Hnt};
use helium_proto::BlockchainTxn;
use helium_wallet::keypair::Network;
use ledger_transport::exchange::Exchange as LedgerTransport;
Expand All @@ -16,6 +16,8 @@ pub use error::Error;
pub type Result<T = ()> = std::result::Result<T, Error>;

const DEFAULT_TESTNET_BASE_URL: &str = "https://testnet-api.helium.wtf/v1";
pub(crate) static USER_AGENT: &str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

#[derive(StructOpt, Debug)]
enum Units {
Expand Down Expand Up @@ -136,6 +138,11 @@ pub async fn submit_txn(client: &Client, txn: &BlockchainTxn) -> Result<PendingT
.map_err(|e| e.into())
}

fn new_client(network: Network) -> Client {
println!("{}", USER_AGENT);
Client::new_with_base_url(api_url(network), USER_AGENT)
}

fn api_url(network: Network) -> String {
match network {
Network::MainNet => {
Expand Down
2 changes: 1 addition & 1 deletion src/txns/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn print_balance(pubkeys: &[PublicKey]) -> Result {
// sample the first pubkey to determine network
let network = pubkeys[0].network;

let client = Client::new_with_base_url(api_url(network));
let client = new_client(network);
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
let balance = match network {
Expand Down
2 changes: 1 addition & 1 deletion src/txns/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn ledger(opts: Opts, cmd: Cmd) -> Result<Response<BlockchainTxnTokenBurnV

// get nonce
let pubkey = get_pubkey(opts.account, &ledger_transport, PubkeyDisplay::Off).await?;
let client = Client::new_with_base_url(api_url(pubkey.network));
let client = new_client(pubkey.network);

let account = accounts::get(&client, &pubkey.to_string()).await?;
let nonce: u64 = if let Some(nonce) = cmd.nonce {
Expand Down
2 changes: 1 addition & 1 deletion src/txns/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::*;
use byteorder::{LittleEndian as LE, WriteBytesExt};
use helium_api::{accounts, models::Hnt, Client};
use helium_api::{accounts, models::Hnt};
use helium_proto::{
BlockchainTxnPaymentV1, BlockchainTxnStakeValidatorV1, BlockchainTxnTokenBurnV1,
BlockchainTxnTransferValidatorStakeV1, BlockchainTxnUnstakeValidatorV1,
Expand Down
2 changes: 1 addition & 1 deletion src/txns/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn ledger(opts: Opts, cmd: Cmd) -> Result<Response<BlockchainTxnPaymentV1>

// get nonce
let pubkey = get_pubkey(opts.account, &ledger_transport, PubkeyDisplay::Off).await?;
let client = Client::new_with_base_url(api_url(pubkey.network));
let client = new_client(pubkey.network);

let account = accounts::get(&client, &pubkey.to_string()).await?;
let nonce: u64 = if let Some(nonce) = cmd.nonce {
Expand Down
2 changes: 1 addition & 1 deletion src/txns/validator/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Cmd {
// get account from API so we can get nonce and balance
let owner = get_pubkey(opts.account, &ledger_transport, PubkeyDisplay::Off).await?;

let client = Client::new_with_base_url(api_url(owner.network));
let client = new_client(owner.network);

let account = accounts::get(&client, &owner.to_string()).await?;

Expand Down
4 changes: 2 additions & 2 deletions src/txns/validator/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub(crate) async fn ledger_create(
return Ok(None);
}

let client = Client::new_with_base_url(api_url(old_owner.network));
let client = new_client(old_owner.network);
// calculate fee
let mut txn = BlockchainTxnTransferValidatorStakeV1 {
new_owner: new_owner.to_vec(),
Expand Down Expand Up @@ -231,7 +231,7 @@ pub(crate) async fn ledger_accept(
}

// submit the signed transaction to the API
let client = Client::new_with_base_url(api_url(old_owner.network));
let client = new_client(old_owner.network);
let pending_txn_status = submit_txn(&client, &input_txn.in_envelope()).await?;

Ok(Some(Response::Txn(
Expand Down
2 changes: 1 addition & 1 deletion src/txns/validator/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) async fn ledger(
// get account from API so we can get nonce and balance
let owner = get_pubkey(opts.account, &ledger, PubkeyDisplay::Off).await?;

let client = Client::new_with_base_url(api_url(owner.network));
let client = new_client(owner.network);

let mut txn = BlockchainTxnUnstakeValidatorV1 {
owner: owner.to_vec(),
Expand Down

0 comments on commit 30f4250

Please sign in to comment.