diff --git a/Cargo.lock b/Cargo.lock index 9bdbc40..a3e5c5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,7 +34,7 @@ dependencies = [ [[package]] name = "antelope-client" -version = "0.1.2" +version = "0.1.3" dependencies = [ "antelope-client-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64", diff --git a/Cargo.toml b/Cargo.toml index 1225a41..a48a907 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ ] [workspace.package] -version = "0.1.2" +version = "0.1.3" edition = "2021" rust-version = "1.75" authors = ["Jesse Schulman "] diff --git a/crates/antelope/src/api/client.rs b/crates/antelope/src/api/client.rs index 5350f9c..4f1bd36 100644 --- a/crates/antelope/src/api/client.rs +++ b/crates/antelope/src/api/client.rs @@ -1,6 +1,7 @@ -use crate::api::default_provider::DefaultProvider; use crate::api::v1::chain::ChainAPI; -use std::fmt::{Display, Formatter}; +use std::fmt::{Debug, Display, Formatter}; + +pub use crate::api::default_provider::DefaultProvider; pub enum HTTPMethod { GET, @@ -21,22 +22,23 @@ impl Display for HTTPMethod { } // TODO: Make this return an APIResponse with status code, timing, etc.. -pub trait Provider { +pub trait Provider: Debug + Default + Sync + Send { fn post(&self, path: String, body: Option) -> Result; fn get(&self, path: String) -> Result; } -pub struct APIClient { - pub v1_chain: ChainAPI, +#[derive(Debug, Default, Clone)] +pub struct APIClient { + pub v1_chain: ChainAPI

, } -impl APIClient { - pub fn default_provider(base_url: String) -> Result { - let provider = Box::new(DefaultProvider::new(base_url).unwrap()); +impl APIClient

{ + pub fn default_provider(base_url: String) -> Result, String> { + let provider = DefaultProvider::new(base_url).unwrap(); APIClient::custom_provider(provider) } - pub fn custom_provider(provider: Box) -> Result { + pub fn custom_provider(provider: P) -> Result { Ok(APIClient { v1_chain: ChainAPI::new(provider), }) diff --git a/crates/antelope/src/api/default_provider.rs b/crates/antelope/src/api/default_provider.rs index 81fad48..6e7e23b 100644 --- a/crates/antelope/src/api/default_provider.rs +++ b/crates/antelope/src/api/default_provider.rs @@ -1,6 +1,8 @@ use crate::api::client::Provider; use reqwest::blocking::Client; +use std::fmt::{Debug, Formatter}; +#[derive(Default, Clone)] pub struct DefaultProvider { base_url: String, client: Client, @@ -27,6 +29,12 @@ impl DefaultProvider { } } +impl Debug for DefaultProvider { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "DefaultProvider<{}>", self.base_url) + } +} + impl Provider for DefaultProvider { fn get(&self, path: String) -> Result { let res = self.client.get(self.base_url.to_string() + &path).send(); diff --git a/crates/antelope/src/api/mod.rs b/crates/antelope/src/api/mod.rs index 6c4424a..5b6c826 100644 --- a/crates/antelope/src/api/mod.rs +++ b/crates/antelope/src/api/mod.rs @@ -1,3 +1,3 @@ pub mod client; -mod default_provider; +pub mod default_provider; pub mod v1; diff --git a/crates/antelope/src/api/v1/chain.rs b/crates/antelope/src/api/v1/chain.rs index 372730e..504cf4b 100644 --- a/crates/antelope/src/api/v1/chain.rs +++ b/crates/antelope/src/api/v1/chain.rs @@ -14,13 +14,15 @@ use crate::name; use crate::serializer::formatter::{JSONObject, ValueTo}; use crate::util::hex_to_bytes; use serde_json::Value; +use std::fmt::Debug; -pub struct ChainAPI { - provider: Box, +#[derive(Debug, Default, Clone)] +pub struct ChainAPI { + provider: T, } -impl ChainAPI { - pub fn new(provider: Box) -> Self { +impl ChainAPI { + pub fn new(provider: T) -> Self { ChainAPI { provider } } @@ -107,10 +109,10 @@ impl ChainAPI { }) } - pub fn get_table_rows( + pub fn get_table_rows( &self, params: GetTableRowsParams, - ) -> Result, ClientError<()>> { + ) -> Result, ClientError<()>> { let result = self.provider.post( String::from("/v1/chain/get_table_rows"), Some(params.to_json()), @@ -121,12 +123,12 @@ impl ChainAPI { let more = response_obj.get_bool("more")?; let next_key_str = response_obj.get_string("next_key")?; let rows_value = response_obj.get_vec("rows")?; - let mut rows: Vec = Vec::with_capacity(rows_value.len()); + let mut rows: Vec

= Vec::with_capacity(rows_value.len()); for encoded_row in rows_value { let row_bytes_hex = &ValueTo::string(Some(encoded_row))?; let row_bytes = hex_to_bytes(row_bytes_hex); let mut decoder = Decoder::new(&row_bytes); - let mut row = T::default(); + let mut row = P::default(); decoder.unpack(&mut row); rows.push(row); } diff --git a/crates/antelope/src/chain/private_key.rs b/crates/antelope/src/chain/private_key.rs index 03aec19..ef75922 100644 --- a/crates/antelope/src/chain/private_key.rs +++ b/crates/antelope/src/chain/private_key.rs @@ -7,8 +7,9 @@ use crate::crypto::generate::generate; use crate::crypto::get_public::get_public; use crate::crypto::shared_secrets::shared_secret; use crate::crypto::sign::sign; -use std::fmt::{Display, Formatter}; +use std::fmt::{Debug, Display, Formatter}; +#[derive(Default, Clone)] pub struct PrivateKey { pub key_type: KeyType, value: Vec, @@ -91,3 +92,9 @@ impl Display for PrivateKey { write!(f, "{}", self.as_string()) } } + +impl Debug for PrivateKey { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_string()) + } +} diff --git a/crates/antelope/tests/client.rs b/crates/antelope/tests/client.rs index 7908b95..1f09cb8 100644 --- a/crates/antelope/tests/client.rs +++ b/crates/antelope/tests/client.rs @@ -14,7 +14,7 @@ use utils::mock_provider::MockProvider; #[test] fn chain_get_info() { let mock_provider = MockProvider {}; - let client = APIClient::custom_provider(Box::new(mock_provider)); + let client = APIClient::custom_provider(mock_provider); //let client = APIClient::default_provider(String::from("https://telos.caleos.io")); let info = client.unwrap().v1_chain.get_info().unwrap(); assert_eq!(info.head_block_producer, name!("bp.boid")); @@ -32,7 +32,7 @@ fn chain_get_info() { #[test] fn chain_send_transaction() { let mock_provider = MockProvider {}; - let client = APIClient::custom_provider(Box::new(mock_provider)).unwrap(); + let client = APIClient::custom_provider(mock_provider).unwrap(); //let client = APIClient::default_provider(String::from("https://testnet.telos.caleos.io")).unwrap(); let info = client.v1_chain.get_info().unwrap(); let transaction = @@ -93,7 +93,7 @@ pub fn chain_get_table_rows() { } let mock_provider = MockProvider {}; - let client = APIClient::custom_provider(Box::new(mock_provider)).unwrap(); + let client = APIClient::custom_provider(mock_provider).unwrap(); //let client = APIClient::default_provider(String::from("https://testnet.telos.caleos.io")).unwrap(); let res1 = client diff --git a/crates/antelope/tests/utils/mock_provider.rs b/crates/antelope/tests/utils/mock_provider.rs index 4c2b722..40f14b4 100644 --- a/crates/antelope/tests/utils/mock_provider.rs +++ b/crates/antelope/tests/utils/mock_provider.rs @@ -9,9 +9,11 @@ use antelope::chain::transaction::{SignedTransaction, Transaction}; use antelope::chain::{Decoder, Encoder, Packer}; use antelope::name; use antelope_client_macros::StructPacker; +use std::fmt::{Debug, Formatter}; use std::fs; use std::path::PathBuf; +#[derive(Default)] pub struct MockProvider {} impl MockProvider { @@ -34,6 +36,12 @@ impl MockProvider { } } +impl Debug for MockProvider { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "MockProvider") + } +} + impl Provider for MockProvider { fn post(&self, path: String, body: Option) -> Result { self.call(HTTPMethod::POST, path, body)