Skip to content

Commit

Permalink
Refactor AllAssetsRequest, SingleAccountRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Eline Jorritsma committed Nov 21, 2023
1 parent 495c0f7 commit a48cadb
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 159 deletions.
68 changes: 34 additions & 34 deletions src/accounts/single_account_request.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
use crate::models::{is_public_key, Request};

/// SingleAccountRequest is the request for the /accounts enpoint to get a single account.
/// [More Details](https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html "Accounts")
pub struct SingleAccountRequest {
#[derive(Default, Clone)]
pub struct AccountId(String);
#[derive(Default, Clone)]
pub struct NoAccountId;

/// SingleAccountRequest is the request for the /accounts endpoint to get a single account.
/// [More Details](https://developers.stellar.org/api/horizon/resources/retrieve-an-account "Accounts")
#[derive(Default)]
pub struct SingleAccountRequest<I> {
/// Account ID of the sponsor. Every account in the response will either be sponsored by the given account ID or have a subentry (trustline, offer, or data entry) which is sponsored by the given account ID.
account_id: Option<String>,
account_id: I,
}

impl Request for SingleAccountRequest {
fn new() -> Self {
SingleAccountRequest { account_id: None }
impl SingleAccountRequest<NoAccountId> {
pub fn new() -> Self {
SingleAccountRequest::default()
}
}

impl<I> SingleAccountRequest<I> {
/// Sets the account ID of the account to get.
/// # Arguments
/// * `account_id` - The account ID of the account to get.
/// # Returns
/// The request object
/// [SingleAccountRequest](struct.SingleAccountRequest.html)
pub fn set_account_id(self, account_id: String) -> Result<SingleAccountRequest<AccountId>, String> {
if let Err(e) = is_public_key(&account_id) {
return Err(e.to_string());
}

Ok(SingleAccountRequest {
account_id: AccountId(account_id)
})
}
}

impl Request for SingleAccountRequest<AccountId> {
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 {
query.push_str(&format!("{}", account_id));
}
query.push_str(&format!("{}", self.account_id.0));

query.trim_end_matches('&').to_string()
}
Expand All @@ -33,28 +57,4 @@ impl Request for SingleAccountRequest {
self.get_query_parameters()
)
}

fn validate(&self) -> Result<(), String> {
if let Some(account_id) = &self.account_id {
let is_valid = is_public_key(account_id);
if is_valid.is_err() {
return Err(is_valid.unwrap_err());
}
}

Ok(())
}
}

impl SingleAccountRequest {
/// Sets the account ID of the account to get.
/// # Arguments
/// * `account_id` - The account ID of the account to get.
/// # Returns
/// The request object
/// [SingleAccountRequest](struct.SingleAccountRequest.html)
pub fn set_account_id(&mut self, account_id: String) -> &mut Self {
self.account_id = Some(account_id);
self
}
}
104 changes: 48 additions & 56 deletions src/assets/all_assets_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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"
// [More Details] https://developers.stellar.org/api/horizon/resources/list-all-assets "Assets"
#[derive(Default)]
pub struct AllAssetsRequest {

/// The assets identifying code. For example, if the asset is a credit issued on the Stellar network,
/// the code will be the asset’s code. If the asset is a native asset, the code will be XLM.
asset_code: Option<String>,
Expand All @@ -24,17 +24,13 @@ pub struct AllAssetsRequest {
order: Option<Order>,
}

impl Request for AllAssetsRequest {
fn new() -> Self {
AllAssetsRequest {
asset_code: None,
asset_issuer: None,
cursor: None,
limit: None,
order: None,
}
impl AllAssetsRequest {
pub fn new() -> AllAssetsRequest {
AllAssetsRequest::default()
}
}

impl Request for AllAssetsRequest {
fn get_path(&self) -> &str {
"/assets"
}
Expand All @@ -60,36 +56,6 @@ impl Request for AllAssetsRequest {
query.trim_end_matches('&').to_string()
}

fn validate(&self) -> Result<(), String> {
if let Some(asset_code) = &self.asset_code {
// TODO: implement full asset code regex
if asset_code.len() > 12 {
return Err("asset_code must be 12 characters or less".to_string());
}
}

if let Some(asset_issuer) = &self.asset_issuer {
// TODO: implement full asset issuer regex
if asset_issuer.len() != 56 {
return Err("asset_issuer must be 56 characters".to_string());
}
}

if let Some(limit) = &self.limit {
if *limit < 1 || *limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}
}

if let Some(cursor) = &self.cursor {
if *cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}
}

Ok(())
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}?{}",
Expand All @@ -107,9 +73,15 @@ impl AllAssetsRequest {
/// # Returns
/// The request object
/// [AllAssetsRequest](struct.AllAssetsRequest.html)
pub fn set_asset_code(&mut self, asset_code: &str) -> &mut Self {
self.asset_code = Some(asset_code.to_owned());
self
pub fn set_asset_code(self, asset_code: &str) -> Result<AllAssetsRequest, String> {
if asset_code.len() > 12 {
return Err("asset_code must be 12 characters or less".to_string());
}

Ok(AllAssetsRequest {
asset_code: Some(asset_code.to_string()),
..self
})
}

/// Sets the asset issuer
Expand All @@ -118,9 +90,15 @@ impl AllAssetsRequest {
/// # Returns
/// The request object
/// [AllAssetsRequest](struct.AllAssetsRequest.html)
pub fn set_asset_issuer(&mut self, asset_issuer: &str) -> &mut Self {
self.asset_issuer = Some(asset_issuer.to_owned());
self
pub fn set_asset_issuer(self, asset_issuer: &str) -> Result<AllAssetsRequest, String> {
if asset_issuer.len() != 56 {
return Err("asset_issuer must be 56 characters".to_string());
}

Ok(AllAssetsRequest {
asset_issuer: Some(asset_issuer.to_string()),
..self
})
}

/// Sets the cursor
Expand All @@ -129,9 +107,15 @@ impl AllAssetsRequest {
/// # Returns
/// The request object
/// [AllAssetsRequest](struct.AllAssetsRequest.html)
pub fn set_cursor(&mut self, cursor: u32) -> &mut Self {
self.cursor = Some(cursor);
self
pub fn set_cursor(self, cursor: u32) -> Result<AllAssetsRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(AllAssetsRequest {
cursor: Some(cursor),
..self
})
}

/// Sets the limit
Expand All @@ -140,9 +124,15 @@ impl AllAssetsRequest {
/// # Returns
/// The request object
/// [AllAssetsRequest](struct.AllAssetsRequest.html)
pub fn set_limit(&mut self, limit: u32) -> &mut Self {
self.limit = Some(limit);
self
pub fn set_limit(self, limit: u32) -> Result<AllAssetsRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(AllAssetsRequest {
limit: Some(limit),
..self
})
}

/// Sets the order
Expand All @@ -151,8 +141,10 @@ impl AllAssetsRequest {
/// # Returns
/// The request object
/// [AllAssetsRequest](struct.AllAssetsRequest.html)
pub fn set_order(&mut self, order: Order) -> &mut Self {
self.order = Some(order);
self
pub fn set_order(self, order: Order) -> AllAssetsRequest {
AllAssetsRequest {
order: Some(order),
..self
}
}
}
100 changes: 54 additions & 46 deletions src/claimable_balances/all_claimable_balances_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,6 @@ impl AllClaimableBalancesRequest {
}
}

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()
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}?{}",
base_url,
self.get_path(),
self.get_query_parameters()
)
}
}

impl AllClaimableBalancesRequest {
/// Sets the sponsor for the request
/// # Arguments
Expand Down Expand Up @@ -132,11 +93,15 @@ impl AllClaimableBalancesRequest {
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_cursor(self, cursor: u32) -> AllClaimableBalancesRequest {
AllClaimableBalancesRequest {
pub fn set_cursor(self, cursor: u32) -> Result<AllClaimableBalancesRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(AllClaimableBalancesRequest {
cursor: Some(cursor),
..self
}
})
}

/// Sets the limit for the request
Expand All @@ -146,11 +111,15 @@ impl AllClaimableBalancesRequest {
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_limit(self, limit: u32) -> AllClaimableBalancesRequest {
AllClaimableBalancesRequest {
pub fn set_limit(self, limit: u32) -> Result<AllClaimableBalancesRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(AllClaimableBalancesRequest {
limit: Some(limit),
..self
}
})
}

/// Sets the order for the request
Expand All @@ -166,4 +135,43 @@ impl AllClaimableBalancesRequest {
..self
}
}
}
}

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()
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}?{}",
base_url,
self.get_path(),
self.get_query_parameters()
)
}
}
Loading

0 comments on commit a48cadb

Please sign in to comment.