Skip to content

Commit cc7b018

Browse files
authored
Merge pull request #13 from Baseflow/constant-strings
re-use static strings for paths
2 parents e581260 + ba1ca5c commit cc7b018

File tree

13 files changed

+92
-143
lines changed

13 files changed

+92
-143
lines changed

src/accounts/accounts_request.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::models::*;
1+
use crate::{BuildQueryParametersExt, models::*};
22

33
use super::super::AssetType;
44
use super::super::Order;
@@ -43,41 +43,25 @@ impl Request for AccountsRequest {
4343
}
4444
}
4545

46-
fn get_path(&self) -> &str {
47-
"/accounts"
48-
}
49-
5046
fn get_query_parameters(&self) -> String {
51-
let mut query = String::new();
52-
if let Some(sponsor) = &self.sponsor {
53-
query.push_str(&format!("sponsor={}&", sponsor));
54-
}
55-
if let Some(signer) = &self.signer {
56-
query.push_str(&format!("signer={}&", signer));
57-
}
58-
if let Some(asset) = &self.asset {
59-
query.push_str(&format!("asset={}&", asset));
60-
}
61-
if let Some(cursor) = &self.cursor {
62-
query.push_str(&format!("cursor={}&", cursor));
63-
}
64-
if let Some(limit) = &self.limit {
65-
query.push_str(&format!("limit={}&", limit));
66-
}
67-
if let Some(order) = &self.order {
68-
query.push_str(&format!("order={}&", order));
69-
}
70-
if let Some(liquidity_pool) = &self.liquidity_pool {
71-
query.push_str(&format!("liquidity_pool={}&", liquidity_pool));
72-
}
73-
query.trim_end_matches('&').to_string()
47+
vec![
48+
self.sponsor.as_ref().map(|s| format!("sponsor={}", s)),
49+
self.signer.as_ref().map(|s| format!("signer={}", s)),
50+
self.asset.as_ref().map(|a| format!("asset={}", a)),
51+
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
52+
self.limit.as_ref().map(|l| format!("limit={}", l)),
53+
self.order.as_ref().map(|o| format!("order={}", o)),
54+
self.liquidity_pool
55+
.as_ref()
56+
.map(|lp| format!("liquidity_pool={}", lp)),
57+
].build_query_parameters()
7458
}
7559

7660
fn build_url(&self, base_url: &str) -> String {
7761
format!(
78-
"{}{}?{}",
62+
"{}/{}{}",
7963
base_url,
80-
self.get_path(),
64+
super::ACCOUNTS_PATH,
8165
self.get_query_parameters()
8266
)
8367
}
@@ -194,7 +178,10 @@ mod tests {
194178
#[test]
195179
fn test_accounts_request() {
196180
let request = AccountsRequest::new();
197-
assert_eq!(request.get_path(), "/accounts");
181+
assert_eq!(
182+
request.build_url("https://horizon-testnet.stellar.org"),
183+
"https://horizon-testnet.stellar.org/accounts"
184+
);
198185
}
199186

200187
#[test]

src/accounts/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pub mod accounts_response;
33
pub mod single_account_request;
44
pub mod single_account_response;
55

6+
static ACCOUNTS_PATH: &str = "accounts";
7+
68
pub mod prelude {
79
pub use super::accounts_request::*;
810
pub use super::accounts_response::*;

src/accounts/single_account_request.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ impl Request for SingleAccountRequest {
1212
SingleAccountRequest { account_id: None }
1313
}
1414

15-
fn get_path(&self) -> &str {
16-
"/accounts/"
17-
}
18-
1915
fn get_query_parameters(&self) -> String {
2016
let mut query = String::new();
2117
if let Some(account_id) = &self.account_id {
@@ -27,9 +23,9 @@ impl Request for SingleAccountRequest {
2723

2824
fn build_url(&self, base_url: &str) -> String {
2925
format!(
30-
"{}{}{}",
26+
"{}/{}/{}",
3127
base_url,
32-
self.get_path(),
28+
super::ACCOUNTS_PATH,
3329
self.get_query_parameters()
3430
)
3531
}

src/assets/all_assets_request.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::models::Request;
2-
1+
use crate::{models::Request, BuildQueryParametersExt};
32
use super::super::Order;
43

4+
5+
56
// AllAssetsRequest is the request for the /assets endpoint
67
// [More Details] https://www.stellar.org/developers/horizon/reference/endpoints/assets-all.html "Assets"
78
pub struct AllAssetsRequest {
@@ -35,29 +36,14 @@ impl Request for AllAssetsRequest {
3536
}
3637
}
3738

38-
fn get_path(&self) -> &str {
39-
"/assets"
40-
}
41-
4239
fn get_query_parameters(&self) -> String {
43-
let mut query = String::new();
44-
if let Some(asset_code) = &self.asset_code {
45-
query.push_str(&format!("asset_code={}&", asset_code));
46-
}
47-
if let Some(asset_issuer) = &self.asset_issuer {
48-
query.push_str(&format!("asset_issuer={}&", asset_issuer));
49-
}
50-
if let Some(cursor) = &self.cursor {
51-
query.push_str(&format!("cursor={}&", cursor));
52-
}
53-
if let Some(limit) = &self.limit {
54-
query.push_str(&format!("limit={}&", limit));
55-
}
56-
if let Some(order) = &self.order {
57-
query.push_str(&format!("order={}&", order));
58-
}
59-
60-
query.trim_end_matches('&').to_string()
40+
vec![
41+
self.asset_code.as_ref().map(|ac| format!("asset_code={}", ac)),
42+
self.asset_issuer.as_ref().map(|ai| format!("asset_issuer={}", ai)),
43+
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
44+
self.limit.as_ref().map(|l| format!("limit={}", l)),
45+
self.order.as_ref().map(|o| format!("order={}", o)),
46+
].build_query_parameters()
6147
}
6248

6349
fn validate(&self) -> Result<(), String> {
@@ -92,9 +78,9 @@ impl Request for AllAssetsRequest {
9278

9379
fn build_url(&self, base_url: &str) -> String {
9480
format!(
95-
"{}{}?{}",
81+
"{}/{}{}",
9682
base_url,
97-
self.get_path(),
83+
super::ASSET_PATH,
9884
self.get_query_parameters()
9985
)
10086
}

src/assets/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
pub mod all_assets_request;
22
pub mod all_assets_response;
33

4+
static ASSET_PATH: &str = "assets";
5+
46
pub mod prelude {
57
pub use super::all_assets_request::*;
68
pub use super::all_assets_response::*;
7-
}
9+
}

src/claimable_balances/all_claimable_balances_request.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::models::*;
1+
use crate::{BuildQueryParametersExt, models::*};
22

33
use super::super::Order;
44
use super::super::AssetType;
@@ -40,39 +40,22 @@ impl Request for AllClaimableBalancesRequest {
4040
}
4141
}
4242

43-
fn get_path(&self) -> &str {
44-
"/claimable_balances/"
45-
}
46-
4743
fn get_query_parameters(&self) -> String {
48-
let mut query = String::new();
49-
if let Some(sponsor) = &self.sponsor {
50-
query.push_str(&format!("sponsor={}&", sponsor));
51-
}
52-
if let Some(asset) = &self.asset {
53-
query.push_str(&format!("asset={}&", asset));
54-
}
55-
if let Some(claimant) = &self.claimant {
56-
query.push_str(&format!("claimant={}&", claimant));
57-
}
58-
if let Some(cursor) = &self.cursor {
59-
query.push_str(&format!("cursor={}&", cursor));
60-
}
61-
if let Some(limit) = &self.limit {
62-
query.push_str(&format!("limit={}&", limit));
63-
}
64-
if let Some(order) = &self.order {
65-
query.push_str(&format!("order={}&", order));
66-
}
67-
68-
query.trim_end_matches('&').to_string()
44+
vec![
45+
self.sponsor.as_ref().map(|s| format!("sponsor={}", s)),
46+
self.asset.as_ref().map(|a| format!("asset={}", a)),
47+
self.claimant.as_ref().map(|c| format!("claimant={}", c)),
48+
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
49+
self.limit.as_ref().map(|l| format!("limit={}", l)),
50+
self.order.as_ref().map(|o| format!("order={}", o)),
51+
].build_query_parameters()
6952
}
7053

7154
fn build_url(&self, base_url: &str) -> String {
7255
format!(
73-
"{}{}?{}",
56+
"{}/{}/{}",
7457
base_url,
75-
self.get_path(),
58+
super::CLAIMABLE_BALANCES_PATH,
7659
self.get_query_parameters()
7760
)
7861
}
@@ -168,4 +151,4 @@ impl AllClaimableBalancesRequest {
168151
self.order = Some(order);
169152
self
170153
}
171-
}
154+
}

src/claimable_balances/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ pub mod all_claimable_balances_response;
33
pub mod single_claimable_balance_request;
44
pub mod single_claimable_balance_response;
55

6+
static CLAIMABLE_BALANCES_PATH: &str = "claimable_balances";
7+
68
pub mod prelude {
79
pub use super::all_claimable_balances_request::*;
810
pub use super::all_claimable_balances_response::*;
911
pub use super::single_claimable_balance_request::*;
1012
pub use super::single_claimable_balance_response::*;
11-
}
13+
}

src/claimable_balances/single_claimable_balance_request.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@ impl Request for SingleClaimableBalanceRequest {
1616
}
1717
}
1818

19-
fn get_path(&self) -> &str {
20-
"/claimable_balances/"
21-
}
22-
2319
fn get_query_parameters(&self) -> String {
2420
let mut query = String::new();
2521
if let Some(claimable_balance_id) = &self.claimable_balance_id {
2622
query.push_str(&format!("{}", claimable_balance_id));
2723
}
28-
query
24+
format!("/{}", query)
2925
}
3026

3127
fn build_url(&self, base_url: &str) -> String {
3228
format!(
33-
"{}{}{}",
29+
"{}/{}{}",
3430
base_url,
35-
self.get_path(),
31+
super::CLAIMABLE_BALANCES_PATH,
3632
self.get_query_parameters()
3733
)
3834
}

src/ledgers/ledgers_request.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::models::*;
1+
use crate::{BuildQueryParametersExt, models::*};
22

33
use super::super::Order;
44

@@ -20,26 +20,12 @@ impl Request for LedgersRequest {
2020
}
2121
}
2222

23-
fn get_path(&self) -> &str {
24-
"/ledgers"
25-
}
26-
2723
fn get_query_parameters(&self) -> String {
28-
let mut query_parameters = vec![];
29-
30-
if let Some(cursor) = &self.cursor {
31-
query_parameters.push(format!("cursor={}", cursor));
32-
}
33-
34-
if let Some(limit) = &self.limit {
35-
query_parameters.push(format!("limit={}", limit));
36-
}
37-
38-
if let Some(order) = &self.order {
39-
query_parameters.push(format!("order={}", order));
40-
}
41-
42-
query_parameters.join("&")
24+
vec![
25+
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
26+
self.limit.as_ref().map(|l| format!("limit={}", l)),
27+
self.order.as_ref().map(|o| format!("order={}", o)),
28+
].build_query_parameters()
4329
}
4430

4531
fn validate(&self) -> Result<(), String> {
@@ -63,9 +49,9 @@ impl Request for LedgersRequest {
6349

6450
fn build_url(&self, base_url: &str) -> String {
6551
format!(
66-
"{}{}?{}",
52+
"{}/{}{}",
6753
base_url,
68-
self.get_path(),
54+
super::LEDGERS_PATH,
6955
self.get_query_parameters()
7056
)
7157
}
@@ -109,7 +95,9 @@ mod tests {
10995
#[test]
11096
fn test_ledgers_request() {
11197
let request = LedgersRequest::new();
112-
113-
assert_eq!(request.get_path(), "/ledgers");
98+
assert_eq!(
99+
request.build_url("https://horizon-testnet.stellar.org"),
100+
"https://horizon-testnet.stellar.org/ledgers"
101+
);
114102
}
115103
}

src/ledgers/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ pub mod ledgers_response;
33
pub mod single_ledger_request;
44
pub mod single_ledger_response;
55

6+
static LEDGERS_PATH: &str = "ledgers";
7+
68
pub mod prelude {
79
pub use super::ledgers_request::*;
810
pub use super::ledgers_response::*;
911
pub use super::single_ledger_request::*;
1012
pub use super::single_ledger_response::*;
11-
}
13+
}

src/ledgers/single_ledger_request.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ impl Request for SingleLedgerRequest {
1010
Self { sequence: 0 }
1111
}
1212

13-
fn get_path(&self) -> &str {
14-
"/ledgers"
15-
}
16-
1713
fn get_query_parameters(&self) -> String {
18-
format!("{}", self.sequence)
14+
format!("/{}", self.sequence)
1915
}
2016

2117
fn validate(&self) -> Result<(), String> {
@@ -28,9 +24,9 @@ impl Request for SingleLedgerRequest {
2824

2925
fn build_url(&self, base_url: &str) -> String {
3026
format!(
31-
"{}{}/{}",
27+
"{}/{}{}",
3228
base_url,
33-
self.get_path(),
29+
super::LEDGERS_PATH,
3430
self.get_query_parameters()
3531
)
3632
}
@@ -56,7 +52,6 @@ mod tests {
5652
#[test]
5753
fn test_ledgers_request() {
5854
let request = SingleLedgerRequest::new();
59-
60-
assert_eq!(request.get_path(), "/ledgers");
55+
assert_eq!(request.build_url("https://horizon-testnet.stellar.org"), "https://horizon-testnet.stellar.org/ledgers/0");
6156
}
62-
}
57+
}

0 commit comments

Comments
 (0)