From 9010318054c85ed1f471fa239a24256b9950fdbd Mon Sep 17 00:00:00 2001 From: Thomas Luijken Date: Sat, 25 Nov 2023 19:54:59 +0100 Subject: [PATCH] re-use static strings for paths --- src/accounts/accounts_request.rs | 13 ++++++------- src/accounts/mod.rs | 2 ++ src/accounts/single_account_request.rs | 8 ++------ src/assets/all_assets_request.rs | 10 +++------- src/assets/mod.rs | 4 +++- .../all_claimable_balances_request.rs | 10 +++------- src/claimable_balances/mod.rs | 4 +++- .../single_claimable_balance_request.rs | 8 ++------ src/ledgers/ledgers_request.rs | 14 ++++++-------- src/ledgers/mod.rs | 4 +++- src/ledgers/single_ledger_request.rs | 13 ++++--------- src/models/mod.rs | 7 ------- 12 files changed, 37 insertions(+), 60 deletions(-) diff --git a/src/accounts/accounts_request.rs b/src/accounts/accounts_request.rs index dd78c17..c67ccd1 100644 --- a/src/accounts/accounts_request.rs +++ b/src/accounts/accounts_request.rs @@ -43,10 +43,6 @@ impl Request for AccountsRequest { } } - fn get_path(&self) -> &str { - "/accounts" - } - fn get_query_parameters(&self) -> String { let mut query = String::new(); if let Some(sponsor) = &self.sponsor { @@ -75,9 +71,9 @@ impl Request for AccountsRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}?{}", + "{}/{}?{}", base_url, - self.get_path(), + super::ACCOUNTS_PATH, self.get_query_parameters() ) } @@ -194,7 +190,10 @@ mod tests { #[test] fn test_accounts_request() { let request = AccountsRequest::new(); - assert_eq!(request.get_path(), "/accounts"); + assert_eq!( + request.build_url("https://horizon-testnet.stellar.org"), + "https://horizon-testnet.stellar.org/accounts?" + ); } #[test] diff --git a/src/accounts/mod.rs b/src/accounts/mod.rs index 88d834b..e49592b 100644 --- a/src/accounts/mod.rs +++ b/src/accounts/mod.rs @@ -3,6 +3,8 @@ pub mod accounts_response; pub mod single_account_request; pub mod single_account_response; +static ACCOUNTS_PATH: &str = "accounts"; + pub mod prelude { pub use super::accounts_request::*; pub use super::accounts_response::*; diff --git a/src/accounts/single_account_request.rs b/src/accounts/single_account_request.rs index a36ce64..70268fc 100644 --- a/src/accounts/single_account_request.rs +++ b/src/accounts/single_account_request.rs @@ -12,10 +12,6 @@ impl Request for SingleAccountRequest { SingleAccountRequest { account_id: None } } - fn get_path(&self) -> &str { - "/accounts/" - } - fn get_query_parameters(&self) -> String { let mut query = String::new(); if let Some(account_id) = &self.account_id { @@ -27,9 +23,9 @@ impl Request for SingleAccountRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}{}", + "{}/{}/{}", base_url, - self.get_path(), + super::ACCOUNTS_PATH, self.get_query_parameters() ) } diff --git a/src/assets/all_assets_request.rs b/src/assets/all_assets_request.rs index d523aee..f6c50d9 100644 --- a/src/assets/all_assets_request.rs +++ b/src/assets/all_assets_request.rs @@ -1,7 +1,7 @@ use crate::models::Request; - use super::super::Order; + // AllAssetsRequest is the request for the /assets endpoint // [More Details] https://www.stellar.org/developers/horizon/reference/endpoints/assets-all.html "Assets" pub struct AllAssetsRequest { @@ -35,10 +35,6 @@ impl Request for AllAssetsRequest { } } - fn get_path(&self) -> &str { - "/assets" - } - fn get_query_parameters(&self) -> String { let mut query = String::new(); if let Some(asset_code) = &self.asset_code { @@ -92,9 +88,9 @@ impl Request for AllAssetsRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}?{}", + "{}/{}?{}", base_url, - self.get_path(), + super::ASSET_PATH, self.get_query_parameters() ) } diff --git a/src/assets/mod.rs b/src/assets/mod.rs index c002cce..5ea3f3c 100644 --- a/src/assets/mod.rs +++ b/src/assets/mod.rs @@ -1,7 +1,9 @@ pub mod all_assets_request; pub mod all_assets_response; +static ASSET_PATH: &str = "assets"; + pub mod prelude { pub use super::all_assets_request::*; pub use super::all_assets_response::*; -} \ No newline at end of file +} diff --git a/src/claimable_balances/all_claimable_balances_request.rs b/src/claimable_balances/all_claimable_balances_request.rs index 183279e..1d6a65a 100644 --- a/src/claimable_balances/all_claimable_balances_request.rs +++ b/src/claimable_balances/all_claimable_balances_request.rs @@ -40,10 +40,6 @@ impl Request for AllClaimableBalancesRequest { } } - fn get_path(&self) -> &str { - "/claimable_balances/" - } - fn get_query_parameters(&self) -> String { let mut query = String::new(); if let Some(sponsor) = &self.sponsor { @@ -70,9 +66,9 @@ impl Request for AllClaimableBalancesRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}?{}", + "{}/{}/?{}", base_url, - self.get_path(), + super::CLAIMABLE_BALANCES_PATH, self.get_query_parameters() ) } @@ -168,4 +164,4 @@ impl AllClaimableBalancesRequest { self.order = Some(order); self } -} \ No newline at end of file +} diff --git a/src/claimable_balances/mod.rs b/src/claimable_balances/mod.rs index 5e007e0..802fa6d 100644 --- a/src/claimable_balances/mod.rs +++ b/src/claimable_balances/mod.rs @@ -3,9 +3,11 @@ pub mod all_claimable_balances_response; pub mod single_claimable_balance_request; pub mod single_claimable_balance_response; +static CLAIMABLE_BALANCES_PATH: &str = "claimable_balances"; + pub mod prelude { pub use super::all_claimable_balances_request::*; pub use super::all_claimable_balances_response::*; pub use super::single_claimable_balance_request::*; pub use super::single_claimable_balance_response::*; -} \ No newline at end of file +} diff --git a/src/claimable_balances/single_claimable_balance_request.rs b/src/claimable_balances/single_claimable_balance_request.rs index f40d6a2..43751d5 100644 --- a/src/claimable_balances/single_claimable_balance_request.rs +++ b/src/claimable_balances/single_claimable_balance_request.rs @@ -16,10 +16,6 @@ impl Request for SingleClaimableBalanceRequest { } } - fn get_path(&self) -> &str { - "/claimable_balances/" - } - fn get_query_parameters(&self) -> String { let mut query = String::new(); if let Some(claimable_balance_id) = &self.claimable_balance_id { @@ -30,9 +26,9 @@ impl Request for SingleClaimableBalanceRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}{}", + "{}/{}/{}", base_url, - self.get_path(), + super::CLAIMABLE_BALANCES_PATH, self.get_query_parameters() ) } diff --git a/src/ledgers/ledgers_request.rs b/src/ledgers/ledgers_request.rs index 5caf15f..370956e 100644 --- a/src/ledgers/ledgers_request.rs +++ b/src/ledgers/ledgers_request.rs @@ -20,10 +20,6 @@ impl Request for LedgersRequest { } } - fn get_path(&self) -> &str { - "/ledgers" - } - fn get_query_parameters(&self) -> String { let mut query_parameters = vec![]; @@ -63,9 +59,9 @@ impl Request for LedgersRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}?{}", + "{}/{}?{}", base_url, - self.get_path(), + super::LEDGERS_PATH, self.get_query_parameters() ) } @@ -109,7 +105,9 @@ mod tests { #[test] fn test_ledgers_request() { let request = LedgersRequest::new(); - - assert_eq!(request.get_path(), "/ledgers"); + assert_eq!( + request.build_url("https://horizon-testnet.stellar.org"), + "https://horizon-testnet.stellar.org/ledgers?" + ); } } diff --git a/src/ledgers/mod.rs b/src/ledgers/mod.rs index 03b0686..42ffbb3 100644 --- a/src/ledgers/mod.rs +++ b/src/ledgers/mod.rs @@ -3,9 +3,11 @@ pub mod ledgers_response; pub mod single_ledger_request; pub mod single_ledger_response; +static LEDGERS_PATH: &str = "ledgers"; + pub mod prelude { pub use super::ledgers_request::*; pub use super::ledgers_response::*; pub use super::single_ledger_request::*; pub use super::single_ledger_response::*; -} \ No newline at end of file +} diff --git a/src/ledgers/single_ledger_request.rs b/src/ledgers/single_ledger_request.rs index 1960fa5..1fbac26 100644 --- a/src/ledgers/single_ledger_request.rs +++ b/src/ledgers/single_ledger_request.rs @@ -10,10 +10,6 @@ impl Request for SingleLedgerRequest { Self { sequence: 0 } } - fn get_path(&self) -> &str { - "/ledgers" - } - fn get_query_parameters(&self) -> String { format!("{}", self.sequence) } @@ -28,9 +24,9 @@ impl Request for SingleLedgerRequest { fn build_url(&self, base_url: &str) -> String { format!( - "{}{}/{}", + "{}/{}/{}", base_url, - self.get_path(), + super::LEDGERS_PATH, self.get_query_parameters() ) } @@ -56,7 +52,6 @@ mod tests { #[test] fn test_ledgers_request() { let request = SingleLedgerRequest::new(); - - assert_eq!(request.get_path(), "/ledgers"); + assert_eq!(request.build_url("https://horizon-testnet.stellar.org"), "https://horizon-testnet.stellar.org/ledgers/0"); } -} \ No newline at end of file +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 46a0808..dd594d0 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -7,13 +7,6 @@ pub trait Request { /// [Request](trait.Request.html) fn new() -> Self; - /// Gets the relative URL for the request - /// # Arguments - /// * `self` - The request object - /// # Returns - /// The relative URL for the request - fn get_path(&self) -> &str; - /// Gets the query parameters for the request /// # Arguments /// * `self` - The request object