Skip to content

Commit

Permalink
re-use static strings for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Luijken committed Nov 25, 2023
1 parent e581260 commit 9010318
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 60 deletions.
13 changes: 6 additions & 7 deletions src/accounts/accounts_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
8 changes: 2 additions & 6 deletions src/accounts/single_account_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
)
}
Expand Down
10 changes: 3 additions & 7 deletions src/assets/all_assets_request.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
}
}
10 changes: 3 additions & 7 deletions src/claimable_balances/all_claimable_balances_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
)
}
Expand Down Expand Up @@ -168,4 +164,4 @@ impl AllClaimableBalancesRequest {
self.order = Some(order);
self
}
}
}
4 changes: 3 additions & 1 deletion src/claimable_balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
}
}
8 changes: 2 additions & 6 deletions src/claimable_balances/single_claimable_balance_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
)
}
Expand Down
14 changes: 6 additions & 8 deletions src/ledgers/ledgers_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];

Expand Down Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -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?"
);
}
}
4 changes: 3 additions & 1 deletion src/ledgers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
}
}
13 changes: 4 additions & 9 deletions src/ledgers/single_ledger_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
)
}
Expand All @@ -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");
}
}
}
7 changes: 0 additions & 7 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9010318

Please sign in to comment.