Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed May 24, 2024
1 parent 609a02c commit 3a5b6aa
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 564 deletions.
61 changes: 10 additions & 51 deletions stellar_rust_sdk/src/assets/all_assets_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{models::*, BuildQueryParametersExt};
use crate::{models::*, BuildQueryParametersExt, Paginatable};
use stellar_rust_sdk_derive::Pagination;

/// Represents a request for listing all assets in the Stellar Horizon API.
///
Expand All @@ -17,6 +18,8 @@ use crate::{models::*, BuildQueryParametersExt};
/// # use stellar_rs::assets::prelude::{AllAssetsRequest, AllAssetsResponse};
/// # use stellar_rs::models::*;
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
Expand All @@ -29,13 +32,13 @@ use crate::{models::*, BuildQueryParametersExt};
/// .set_limit(20)?
/// .set_order(Order::Desc);
///
/// let response = horizon_client.get_all_assets(&request).await;
/// let response = horizon_client.get_all_assets(&request.unwrap()).await;
/// # Ok({})
/// # }
///
/// ```
///
#[derive(Default)]
#[derive(Default, Pagination)]
pub struct AllAssetsRequest {
/// The code of the asset to filter by. This is typically the identifier
/// assigned to custom assets on the Stellar network.
Expand All @@ -51,7 +54,7 @@ pub struct AllAssetsRequest {

/// Specifies the maximum number of records to be returned in a single response.
/// The range for this parameter is from 1 to 200. The default value is set to 10.
limit: Option<u32>,
limit: Option<u8>,

/// Determines the [`Order`] of the records in the response. Valid options are [`Order::Asc`] (ascending)
/// and [`Order::Desc`] (descending). If not specified, it defaults to ascending.
Expand Down Expand Up @@ -129,50 +132,6 @@ impl AllAssetsRequest {
..self
})
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
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 maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
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 of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> AllAssetsRequest {
AllAssetsRequest {
order: Some(order),
..self
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -226,7 +185,7 @@ mod tests {
let request = AllAssetsRequest::new().set_cursor(0);
assert_eq!(
request.err().unwrap(),
"cursor must be greater than or equal to 1".to_string()
"Cursor must be greater than or equal to 1.".to_string()
);
}

Expand All @@ -241,7 +200,7 @@ mod tests {
let request = AllAssetsRequest::new().set_limit(0);
assert_eq!(
request.err().unwrap(),
"limit must be between 1 and 200".to_string()
"Limit must be between 1 and 200.".to_string()
);
}

Expand All @@ -250,7 +209,7 @@ mod tests {
let request = AllAssetsRequest::new().set_limit(201);
assert_eq!(
request.err().unwrap(),
"limit must be between 1 and 200".to_string()
"Limit must be between 1 and 200.".to_string()
);
}
}
3 changes: 1 addition & 2 deletions stellar_rust_sdk/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ pub mod prelude {

#[cfg(test)]
pub mod test {

use super::prelude::*;
use crate::horizon_client::HorizonClient;
use crate::{horizon_client::HorizonClient, Paginatable};

#[tokio::test]
async fn test_get_all_assets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{models::*, BuildQueryParametersExt};
use crate::{models::*, BuildQueryParametersExt, Paginatable};
use stellar_rust_sdk_derive::Pagination;

/// Represents a request to list all claimable balances from the Stellar Horizon API.
///
Expand All @@ -16,8 +17,9 @@ use crate::{models::*, BuildQueryParametersExt};
///
/// # Example
/// ```
/// use stellar_rs::claimable_balances::all_claimable_balances_request::AllClaimableBalancesRequest;
/// use stellar_rs::models::{Asset, Order, IssuedAsset};
/// # use stellar_rs::claimable_balances::all_claimable_balances_request::AllClaimableBalancesRequest;
/// # use stellar_rs::models::{Asset, Order, IssuedAsset};
/// # use crate::stellar_rs::Paginatable;
///
/// let request = AllClaimableBalancesRequest::new()
/// .set_sponsor("GDQJUTQYK2MQX2VGDR2FYWLIYAQIEGXTQVTFEMGH2BEWFG4BRUY4CKI7".to_string()).unwrap() // Optional sponsor filter
Expand All @@ -30,7 +32,7 @@ use crate::{models::*, BuildQueryParametersExt};
/// // Use with HorizonClient::get_all_claimable_balances
/// ```
///
#[derive(Default)]
#[derive(Default, Pagination)]
pub struct AllClaimableBalancesRequest {
/// Optional. Representing the account ID of the sponsor. When set, the response will
/// only include claimable balances sponsored by the specified account.
Expand All @@ -50,7 +52,7 @@ pub struct AllClaimableBalancesRequest {

/// Specifies the maximum number of records to be returned in a single response.
/// The range for this parameter is from 1 to 200. The default value is set to 10.
limit: Option<u32>,
limit: Option<u8>,

/// Determines the [`Order`] of the records in the response. Valid options are [`Order::Asc`] (ascending)
/// and [`Order::Desc`] (descending). If not specified, it defaults to ascending.
Expand Down Expand Up @@ -129,50 +131,6 @@ impl AllClaimableBalancesRequest {
..self
})
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
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 maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
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 of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> AllClaimableBalancesRequest {
AllClaimableBalancesRequest {
order: Some(order),
..self
}
}
}

#[cfg(test)]
Expand All @@ -192,7 +150,7 @@ mod tests {
let request = AllClaimableBalancesRequest::new().set_cursor(0);
assert_eq!(
request.err().unwrap(),
"cursor must be greater than or equal to 1".to_string()
"Cursor must be greater than or equal to 1.".to_string()
);
}

Expand All @@ -207,7 +165,7 @@ mod tests {
let request = AllClaimableBalancesRequest::new().set_limit(0);
assert_eq!(
request.err().unwrap(),
"limit must be between 1 and 200".to_string()
"Limit must be between 1 and 200.".to_string()
);
}

Expand All @@ -216,7 +174,7 @@ mod tests {
let request = AllClaimableBalancesRequest::new().set_limit(201);
assert_eq!(
request.err().unwrap(),
"limit must be between 1 and 200".to_string()
"Limit must be between 1 and 200.".to_string()
);
}
}
6 changes: 2 additions & 4 deletions stellar_rust_sdk/src/claimable_balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ fn parse_epoch(epoch_str: &str) -> DateTime<Utc> {
}

pub mod prelude {
pub use super::all_claimable_balances_request::*;
pub use super::response::*;
pub use super::single_claimable_balance_request::*;
pub use super::{all_claimable_balances_request::*, response::*, single_claimable_balance_request::*};
}

#[cfg(test)]
mod tests {
use super::parse_epoch;
use super::prelude::*;
use crate::horizon_client::HorizonClient;
use crate::{horizon_client::HorizonClient, Paginatable};
use chrono::DateTime;
use chrono::{TimeZone, Utc};
use lazy_static::lazy_static;
Expand Down
56 changes: 7 additions & 49 deletions stellar_rust_sdk/src/effects/all_effects_request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{models::*, BuildQueryParametersExt};

use crate::{models::*, BuildQueryParametersExt, Paginatable};
use stellar_rust_sdk_derive::Pagination;
/// Represents a request to fetch effect data from the Stellar Horizon API.
///
/// `AllEffectsRequest` is a struct used to construct queries for retrieving information about effects
Expand All @@ -13,8 +13,10 @@ use crate::{models::*, BuildQueryParametersExt};
///
/// # Example
/// ```rust
/// use stellar_rs::effects::all_effects_request::AllEffectsRequest;
/// use stellar_rs::models::*;
/// # use stellar_rs::effects::all_effects_request::AllEffectsRequest;
/// # use stellar_rs::models::*;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use crate::stellar_rs::Paginatable;
///
/// let request = AllEffectsRequest::new()
/// .set_cursor(1234).unwrap()
Expand All @@ -24,7 +26,7 @@ use crate::{models::*, BuildQueryParametersExt};
/// // The request can now be used with a Horizon client to fetch effects.
/// ```
///
#[derive(Default)]
#[derive(Default, Pagination)]
pub struct AllEffectsRequest {
/// A pointer to a specific location in a collection of responses, derived from the
/// `paging_token` value of a record. Used for pagination control in the API response.
Expand All @@ -44,50 +46,6 @@ impl AllEffectsRequest {
pub fn new() -> Self {
AllEffectsRequest::default()
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
pub fn set_cursor(self, cursor: u32) -> Result<AllEffectsRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

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

/// Sets the maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
pub fn set_limit(self, limit: u8) -> Result<AllEffectsRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

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

/// Sets the order of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> AllEffectsRequest {
AllEffectsRequest {
order: Some(order),
..self
}
}
}

impl Request for AllEffectsRequest {
Expand Down
Loading

0 comments on commit 3a5b6aa

Please sign in to comment.