Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-use static strings for paths #13

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions src/accounts/accounts_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::*;
use crate::{BuildQueryParametersExt, models::*};

use super::super::AssetType;
use super::super::Order;
Expand Down Expand Up @@ -43,41 +43,25 @@ 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 {
query.push_str(&format!("sponsor={}&", sponsor));
}
if let Some(signer) = &self.signer {
query.push_str(&format!("signer={}&", signer));
}
if let Some(asset) = &self.asset {
query.push_str(&format!("asset={}&", asset));
}
if let Some(cursor) = &self.cursor {
query.push_str(&format!("cursor={}&", cursor));
}
if let Some(limit) = &self.limit {
query.push_str(&format!("limit={}&", limit));
}
if let Some(order) = &self.order {
query.push_str(&format!("order={}&", order));
}
if let Some(liquidity_pool) = &self.liquidity_pool {
query.push_str(&format!("liquidity_pool={}&", liquidity_pool));
}
query.trim_end_matches('&').to_string()
vec![
self.sponsor.as_ref().map(|s| format!("sponsor={}", s)),
self.signer.as_ref().map(|s| format!("signer={}", s)),
self.asset.as_ref().map(|a| format!("asset={}", a)),
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
self.liquidity_pool
.as_ref()
.map(|lp| format!("liquidity_pool={}", lp)),
].build_query_parameters()
}

fn build_url(&self, base_url: &str) -> String {
format!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this function is largely the same accross endpoints.
Some format with "{}/{}/{}" and some with "{}/{}{}".
Maybe (in another PR) we can unify this one as well :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree on that!

"{}{}?{}",
"{}/{}{}",
base_url,
self.get_path(),
super::ACCOUNTS_PATH,
self.get_query_parameters()
)
}
Expand Down Expand Up @@ -194,7 +178,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
38 changes: 12 additions & 26 deletions src/assets/all_assets_request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::models::Request;

use crate::{models::Request, BuildQueryParametersExt};
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,29 +36,14 @@ 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 {
query.push_str(&format!("asset_code={}&", asset_code));
}
if let Some(asset_issuer) = &self.asset_issuer {
query.push_str(&format!("asset_issuer={}&", asset_issuer));
}
if let Some(cursor) = &self.cursor {
query.push_str(&format!("cursor={}&", cursor));
}
if let Some(limit) = &self.limit {
query.push_str(&format!("limit={}&", limit));
}
if let Some(order) = &self.order {
query.push_str(&format!("order={}&", order));
}

query.trim_end_matches('&').to_string()
vec![
self.asset_code.as_ref().map(|ac| format!("asset_code={}", ac)),
self.asset_issuer.as_ref().map(|ai| format!("asset_issuer={}", ai)),
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
].build_query_parameters()
}

fn validate(&self) -> Result<(), String> {
Expand Down Expand Up @@ -92,9 +78,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::*;
}
}
41 changes: 12 additions & 29 deletions src/claimable_balances/all_claimable_balances_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::*;
use crate::{BuildQueryParametersExt, models::*};

use super::super::Order;
use super::super::AssetType;
Expand Down Expand Up @@ -40,39 +40,22 @@ 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 {
query.push_str(&format!("sponsor={}&", sponsor));
}
if let Some(asset) = &self.asset {
query.push_str(&format!("asset={}&", asset));
}
if let Some(claimant) = &self.claimant {
query.push_str(&format!("claimant={}&", claimant));
}
if let Some(cursor) = &self.cursor {
query.push_str(&format!("cursor={}&", cursor));
}
if let Some(limit) = &self.limit {
query.push_str(&format!("limit={}&", limit));
}
if let Some(order) = &self.order {
query.push_str(&format!("order={}&", order));
}

query.trim_end_matches('&').to_string()
vec![
self.sponsor.as_ref().map(|s| format!("sponsor={}", s)),
self.asset.as_ref().map(|a| format!("asset={}", a)),
self.claimant.as_ref().map(|c| format!("claimant={}", c)),
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
].build_query_parameters()
}

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 +151,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::*;
}
}
10 changes: 3 additions & 7 deletions src/claimable_balances/single_claimable_balance_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@ 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 {
query.push_str(&format!("{}", claimable_balance_id));
}
query
format!("/{}", query)
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}{}",
"{}/{}{}",
base_url,
self.get_path(),
super::CLAIMABLE_BALANCES_PATH,
self.get_query_parameters()
)
}
Expand Down
36 changes: 12 additions & 24 deletions src/ledgers/ledgers_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::*;
use crate::{BuildQueryParametersExt, models::*};

use super::super::Order;

Expand All @@ -20,26 +20,12 @@ impl Request for LedgersRequest {
}
}

fn get_path(&self) -> &str {
"/ledgers"
}

fn get_query_parameters(&self) -> String {
let mut query_parameters = vec![];

if let Some(cursor) = &self.cursor {
query_parameters.push(format!("cursor={}", cursor));
}

if let Some(limit) = &self.limit {
query_parameters.push(format!("limit={}", limit));
}

if let Some(order) = &self.order {
query_parameters.push(format!("order={}", order));
}

query_parameters.join("&")
vec![
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
].build_query_parameters()
}

fn validate(&self) -> Result<(), String> {
Expand All @@ -63,9 +49,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 +95,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";
tluijken marked this conversation as resolved.
Show resolved Hide resolved

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::*;
}
}
15 changes: 5 additions & 10 deletions src/ledgers/single_ledger_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ impl Request for SingleLedgerRequest {
Self { sequence: 0 }
}

fn get_path(&self) -> &str {
"/ledgers"
}

fn get_query_parameters(&self) -> String {
format!("{}", self.sequence)
format!("/{}", self.sequence)
}

fn validate(&self) -> Result<(), String> {
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");
tluijken marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading
Loading